-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11-2.rb
183 lines (169 loc) · 6.77 KB
/
day11-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
data = File.open("day11.txt").readlines.map(&:chomp)
first_step = data.map {|row| row.split('').map {|col| col = '#' if col != '.'}}
def occupied_or_empty(seats, symbol)
seats_to_change = []
# check if to change the seat condition and label it
seats.each_with_index do |row, index|
seats[index].each_with_index do |col, index2|
# seats at four corners will always be taken
if (index == 0 && index2 == 0) || (index == 0 && index2 == seats[index].length - 1) || ((index == seats.length - 1) && index2 == 0) || ((index == seats.length - 1) && (index2 == seats[index].length - 1))
next
elsif col != nil
if symbol == "L"
temp_seat = seat_circle(index, index2, seats)
elsif symbol == "#"
temp_seat = seat_circle2(index, index2, seats)
end
seats_to_change.push([index, index2]) if temp_seat == "c"
end
end
end
# change the labeled seat
seats_to_change.each do |seat|
seats[seat[0]][seat[1]] = symbol
end
return seats
end
def seat_circle(row, col, seats)
more_than_5 = 0
if row == 0
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, 1, seats) == "#"
elsif row == (seats.length - 1)
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, 1, seats) == "#"
elsif col == 0
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, 1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, 1, seats) == "#"
elsif col == (seats[0].length - 1)
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, -1, seats) == "#"
else
more_than_5 += 1 if see_diagonal(row, col, -1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, 1, seats) == "#"
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, 1, seats) == "#"
end
more_than_5 >= 5 ? (return "c") : (return "no")
end
def seat_circle2(row, col, seats)
more_than_5 = 0
if row == 0
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, 1, seats) == "#"
elsif row == (seats.length - 1)
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, 1, seats) == "#"
elsif col == 0
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, 1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, 1, seats) == "#"
elsif col == (seats[0].length - 1)
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, -1, seats) == "#"
else
more_than_5 += 1 if see_diagonal(row, col, -1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, -1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, -1, 1, seats) == "#"
more_than_5 += 1 if see_col(row, col, -1, seats) == "#"
more_than_5 += 1 if see_col(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, -1, seats) == "#"
more_than_5 += 1 if see_row(row, col, 1, seats) == "#"
more_than_5 += 1 if see_diagonal(row, col, 1, 1, seats) == "#"
end
more_than_5 < 1 ? (return "c") : (return "no")
end
def see_row(row, col, direction, seats)
row += direction
while row >= 0 && row < seats.length && seats[row][col].nil?
row += direction
end
if row < 0
return seats[row+1][col]
elsif row >= seats.length
return seats[row-1][col]
else
return seats[row][col]
end
end
def see_col(row, col, direction, seats)
col += direction
while col >= 0 && col < seats[0].length && seats[row][col].nil?
col += direction
end
if col < 0
return seats[row][col+1]
elsif col >= seats[0].length
return seats[row][col-1]
else
return seats[row][col]
end
end
def see_diagonal(row, col, r_direction, c_direction, seats)
row += r_direction
col += c_direction
while col >= 0 && col < seats[0].length && row >= 0 && row < seats.length && seats[row][col].nil?
row += r_direction
col += c_direction
end
if r_direction == 1 && c_direction == 1 && (col >= seats[0].length || row >= seats.length)
return seats[row-1][col-1]
elsif r_direction == 1 && c_direction == -1 && (row >= seats.length || col < 0)
return seats[row-1][col+1]
elsif r_direction == -1 && c_direction == 1 && (col >= seats[0].length || row < 0)
return seats[row+1][col-1]
elsif r_direction == -1 && c_direction == -1 && (col < 0 || row < 0)
return seats[row+1][col+1]
else
return seats[row][col]
end
end
empty = occupied_or_empty(first_step, "L")
check_point = false
until check_point
occupied = occupied_or_empty(empty, "#")
temp1 = occupied.map(&:clone)
empty = occupied_or_empty(occupied, "L")
break_point = true
occupied.length.times do |row|
if temp1[row] != empty[row]
break_point = false
break
end
end
check_point = true if break_point == true
end
occupied_seats = 0
empty.each do |row|
row.each do |col|
occupied_seats += 1 if col == "#"
end
end
p occupied_seats