-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18_part1.rb
29 lines (26 loc) · 890 Bytes
/
day18_part1.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
def process(start, row_count)
rows = [start]
while (rows.length < row_count)
old_row = rows.last
new_row = [false] * old_row.length
new_row.each_with_index do |tile, i|
left,center,right = get_tiles(old_row, i)
trap = ((left && center && !right) || (center && right && !left) || (left && !center && !right) || (!left && !center && right))
new_row[i] = trap
end
rows << new_row
end
puts rows.flatten.count{|r| !r}
end
def get_tiles(old_row, i)
left = i > 0 ? old_row[i-1] : false
center = old_row[i]
right = (i < (old_row.length - 1)) ? old_row[i + 1] : false
[left,center,right]
end
input =".^^.^.^^^^"
values = input.chars.map{|c|c =="^"}
process(values, 10)
input = ".^^^^^.^^.^^^.^...^..^^.^.^..^^^^^^^^^^..^...^^.^..^^^^..^^^^...^.^.^^^^^^^^....^..^^^^^^.^^^.^^^.^^"
values = input.chars.map{|c|c =="^"}
process(values, 40)