-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rb
174 lines (162 loc) · 3.76 KB
/
main.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
require 'set'
DATA = File.read('data.txt').strip.each_line.map(&:strip)
def get_arr_xy arr, x, y
return nil if x < 0 or y < 0
arr.fetch(y, [])[x]
end
def sum_arr a, b
a.zip(b).map{ |s| s.reduce(:+) }
end
ADJACENT = [[0,1], [1, 0], [0, -1], [-1, 0]]
def next_state arr
5.times.map do |y|
5.times.map do |x|
bugs = ADJACENT.count do |dxy|
'#' == get_arr_xy(arr, *(sum_arr dxy, [x, y]))
end
case get_arr_xy(arr, x, y)
when '#'
bugs == 1 ? '#' : '.'
when '.'
([1,2].include? bugs) ? '#' : '.'
else
raise :wtf
end
end.join
end
end
def bd_rating arr
5.times.map do |y|
5.times.map do |x|
get_arr_xy(arr, x, y) == '#' ? 2**(y*5 + x) : 0
end.sum
end.sum
end
def part1
cache = Set.new
cur = DATA
n = 0
loop do
if cache.include? cur
return bd_rating cur
end
cache.add cur
cur = next_state cur
n += 1
end
end
def get_arr_xyz arr, x, y, z
return nil if x < 0 or y < 0 or z < 0
arr.fetch(z, []).fetch(y, [])[x]
end
def xyz_adjacent x, y
ret = []
if x == 0
ret += [[1, 2, -1], [1, y, 0]]
end
if y == 0
ret += [[2, 1, -1], [x, 1, 0]]
end
if x == 4
ret += [[3, 2, -1], [3, y, 0]]
end
if y == 4
ret += [[2, 3, -1], [x, 3, 0]]
end
if x == 1 and y == 2
ret += [
[0, 0, 1],
[0, 1, 1],
[0, 2, 1],
[0, 3, 1],
[0, 4, 1],
[0, 2, 0],
[1, 1, 0],
[1, 3, 0],
]
elsif x == 2 and y == 3
ret += [
[0, 4, 1],
[1, 4, 1],
[2, 4, 1],
[3, 4, 1],
[4, 4, 1],
[1, 3, 0],
[2, 4, 0],
[3, 3, 0],
]
elsif x == 2 and y == 1
ret += [
[0, 0, 1],
[1, 0, 1],
[2, 0, 1],
[3, 0, 1],
[4, 0, 1],
[1, 1, 0],
[2, 0, 0],
[3, 1, 0],
]
elsif x == 3 and y == 2
ret += [
[4, 0, 1],
[4, 1, 1],
[4, 2, 1],
[4, 3, 1],
[4, 4, 1],
[3, 1, 0],
[4, 2, 0],
[3, 3, 0],
]
else
if x > 0 and x < 4
ret += [[x-1, y, 0], [x+1, y, 0]]
end
if y > 0 and y < 4
ret += [[x, y-1, 0], [x, y+1, 0]]
end
end
ret
end
def next_state_recursive arr
z_count = arr.size + 2
ret = z_count.times.map do |z|
5.times.map do |y|
5.times.map do |x|
bugs = xyz_adjacent(x, y).count do |dxyz|
dxyz[-1] += z - 1
'#' == get_arr_xyz(arr, *dxyz)
end
r = get_arr_xyz(arr, x, y, z - 1)
r = '.' unless r
val = case r
when '#'
bugs == 1 ? '#' : '.'
when '.'
([1,2].include? bugs) ? '#' : '.'
when '?'
raise :wtf unless [2, 2] == [x, y]
else
raise :wtf
end
val = '?' if [2, 2] == [x, y]
val
end.join
end
end
while ret.first.join == ('.' * 12 + '?' + '.' * 12) do
ret.shift
end
while ret.last.join == ('.' * 12 + '?' + '.' * 12) do
ret.pop
end
ret
end
def part2
arr = [DATA]
200.times do
arr = next_state_recursive arr
end
arr.flatten.join.chars.count{ |c| c == '#' }
end
puts 'Part 1: %s' % part1
puts 'Part 2: %s' % part2