-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17a.rb
executable file
·87 lines (68 loc) · 1.74 KB
/
day17a.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
#!/usr/bin/env ruby
input = File
.readlines(ARGV[0])
.map(&:strip)
board = {}
board[0] = {}
input
.each_with_index do |line, y|
board[0][y] = {} unless board[0][y]
line
.chars
.each_with_index
.select { |c, _| c == '#' }
.each do |item, x|
board[0][y][x] = true
end
end
def get_neighbours(b, bx, by, bz)
zs = nil
if (bz == 0)
zs = [1, 0, 1]
else
zs = (-1..1)
end
count = 0
zs.each do |z|
next unless b[z+bz]
(-1..1).each do |y|
next unless b[z+bz][y+by]
(-1..1).each do |x|
next unless b[z+bz][y+by][x+bx]
unless (z == 0 && x == 0 && y == 0)
count += 1
return count if count > 3
end
end
end
end
return count
end
width = input.first.size
height = input.size
count = 0
(1..6).each do |cycle|
new_board = {}
(0..cycle).each do |z|
new_board[z] = {} unless new_board[z]
(-cycle..height+cycle).each do |y|
(-cycle..width+cycle).each do |x|
new_board[z][y] = {} unless new_board[z][y]
neighbours = get_neighbours(board, x, y, z)
if board[z] && board[z][y] && board[z][y][x]
if neighbours.between? 2, 3
new_board[z][y][x] = true
count += z > 0 ? 2 : 1 if cycle == 6
end
else
if neighbours == 3
new_board[z][y][x] = true
count += z > 0 ? 2 : 1 if cycle == 6
end
end
end
end
end
board = new_board
end
p count