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