-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.lua
193 lines (162 loc) · 4.24 KB
/
day08.lua
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local read_input = require('lib.read_input')
local str = require('lib.split_str')
local Grid = require('lib.grid')
local Day08 = {}
function Day08:new(o, input)
o = o or {}
o.parent = self
setmetatable(o, self)
self.__index = self
o.input = (input and input.input) or {}
return o
end
local Part1 = Day08:new()
function Part1:new(input)
local o = {}
Day08.new(self, o, input)
return o
end
function Day08:not_seen_yet(pos)
return not self.seen_antinodes[pos.x] or not self.seen_antinodes[pos.x][pos.y]
end
function Day08:add_to_seen(pos)
self.seen_antinodes[pos.x] = self.seen_antinodes[pos.x] or {}
self.seen_antinodes[pos.x][pos.y] = true
end
local function antinode_positions(a, b, scalar)
scalar = scalar or 1
local xs = math.abs(a.x - b.x) * scalar
local ys = math.abs(a.y - b.y) * scalar
if a.x < b.x and a.y < b.y then
return {
x = (a.x - xs),
y = (a.y - ys)
}, {
x = (b.x + xs),
y = (b.y + ys)
}
elseif a.x < b.x and a.y > b.y then
return {
x = (a.x - xs),
y = (a.y + ys)
}, {
x = (b.x + xs),
y = (b.y - ys)
}
elseif a.x > b.x and a.y < b.y then
return {
x = (a.x + xs),
y = (a.y - ys)
}, {
x = (b.x - xs),
y = (b.y + ys)
}
elseif a.x > b.x and a.y > b.y then
return {
x = (a.x + xs),
y = (a.y + ys)
}, {
x = (b.x - xs),
y = (b.y - ys)
}
end
end
function Day08:solve()
if self.antinodes_count then
return self.antinodes_count
end
self.antinodes_count = 0
self.seen_antinodes = {}
for _, pos in pairs(self.input.positions) do
for i, p1 in pairs(pos) do
for j = i + 1, #pos do
local p2 = pos[j]
self:count_antinodes(p1, p2)
end
end
end
return self.antinodes_count
end
function Day08:valid_antinode_positions(p1, p2, scalar)
local a1, a2 = antinode_positions(p1, p2, scalar)
local pos = {}
if not self.input:is_out_of_bounds(a1) then
table.insert(pos, a1)
end
if not self.input:is_out_of_bounds(a2) then
table.insert(pos, a2)
end
return table.unpack(pos)
end
function Part1:count_antinodes(p1, p2)
local a1, a2 = self:valid_antinode_positions(p1, p2)
if a1 and self:not_seen_yet(a1) then
self.antinodes_count = self.antinodes_count + 1
self:add_to_seen(a1)
end
if a2 and self:not_seen_yet(a2) then
self.antinodes_count = self.antinodes_count + 1
self:add_to_seen(a2)
end
end
local Part2 = Day08:new()
function Part2:new(input)
local o = {}
Day08.new(self, o, input)
return o
end
function Part2:count_antinodes(p1, p2)
local scalar = 0
repeat
local a1, a2 = self:valid_antinode_positions(p1, p2, scalar)
if a1 and self:not_seen_yet(a1) then
self.antinodes_count = self.antinodes_count + 1
self:add_to_seen(a1)
end
if a2 and self:not_seen_yet(a2) then
self.antinodes_count = self.antinodes_count + 1
self:add_to_seen(a2)
end
scalar = scalar + 1
until not a1 and not a2
end
local Input = {}
function Input:new(o)
o = o or {}
o.parent = self
setmetatable(o, self)
self.__index = self
local input = read_input("............\
........0...\
.....0......\
.......0....\
....0.......\
......A.....\
............\
............\
........A...\
.........A..\
............\
............")
local lines = str.split(input, "\n")
local state = {
antennas = {},
positions = {}
}
local grid = Grid:new(state, lines, function(g, x, y, line)
g.antennas[x] = g.antennas[x] or {}
g.antennas[x][y] = line:byte(y)
g.positions[line:byte(y)] = g.positions[line:byte(y)] or {}
table.insert(g.positions[line:byte(y)], {
x = x,
y = y
})
end)
o.input = grid
return o
end
local input = Input:new()
local p1 = Part1:new(input)
print("Part 1: " .. p1:solve())
local p2 = Part2:new(input)
print("Part 2: " .. p2:solve())