-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14_b.rb
33 lines (27 loc) · 1.04 KB
/
day14_b.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
grid = []
while line=gets&.chomp
grid << line.chars
end
# north west south east
def transform(grid)
grid
.transpose.map { |row| row.chunk { |cell| cell == '#' }.flat_map { |is_obstacle, cells| is_obstacle ? cells : cells.sort.reverse } }.transpose
.map { |row| row.chunk { |cell| cell == '#' }.flat_map { |is_obstacle, cells| is_obstacle ? cells : cells.sort.reverse } }
.transpose.map { |row| row.chunk { |cell| cell == '#' }.flat_map { |is_obstacle, cells| is_obstacle ? cells : cells.sort } }.transpose
.map { |row| row.reverse.chunk { |cell| cell == '#' }.flat_map { |is_obstacle, cells| is_obstacle ? cells : cells.sort.reverse }.reverse }
end
hm = {}
for i in 0...1000000000
grid = transform(grid)
if hm[grid] != nil then
cycle = i - hm[grid]
rem = (1000000000-i)%cycle
# puts [cycle, i, hm[grid]].to_s
for i in 0...rem-1
grid = transform(grid)
end
puts grid.each_with_index.map { |row, idx| row.map { |cell| cell == 'O' ? (grid.length-idx) : 0 } }.flatten.sum
break
end
hm[grid] = i
end