-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.lua
215 lines (176 loc) · 4.59 KB
/
day06.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
local read_input = require('lib.read_input')
local str = require('lib.split_str')
local copy = require('lib.copy')
local Grid = require('lib.grid')
local Day06 = {}
function Day06:new(o, input)
o = o or {}
o.parent = self
setmetatable(o, self)
self.__index = self
o.initial_state = (input and input.input) or {}
o.input = copy(o.initial_state)
return o
end
local NORTH = {
x = -1,
y = 0
}
local EAST = {
x = 0,
y = 1
}
local SOUTH = {
x = 1,
y = 0
}
local WEST = {
x = 0,
y = -1
}
function Day06:turn_guard()
local guard = self.input.guard
if guard.direction.x == NORTH.x and guard.direction.y == NORTH.y then
guard.direction = EAST
elseif guard.direction.x == EAST.x and guard.direction.y == EAST.y then
guard.direction = SOUTH
elseif guard.direction.x == SOUTH.x and guard.direction.y == SOUTH.y then
guard.direction = WEST
elseif guard.direction.x == WEST.x and guard.direction.y == WEST.y then
guard.direction = NORTH
end
end
function Day06:move_guard()
local guard = self.input.guard
for _ = 1, 4 do
local x = guard.x + guard.direction.x
local y = guard.y + guard.direction.y
if self.input:get_value(x, y) then
self:turn_guard()
else
guard.x = x
guard.y = y
return
end
end
print("Guard is boxed in and cannot move!")
end
function Day06:state_visited(res)
local g = self.input.guard
local gs = res.guard_states
return gs[g.x] and gs[g.x][g.y] and gs[g.x][g.y][g.direction.x] and gs[g.x][g.y][g.direction.x][g.direction.y]
end
function Day06:visit_state(res)
local g = self.input.guard
local gs = res.guard_states
gs[g.x] = gs[g.x] or {}
gs[g.x][g.y] = gs[g.x][g.y] or {}
gs[g.x][g.y][g.direction.x] = gs[g.x][g.y][g.direction.x] or {}
gs[g.x][g.y][g.direction.x][g.direction.y] = true
end
function Day06:position_visited(res)
local guard = self.input.guard
local pos = res.positions
return pos[guard.x] and pos[guard.x][guard.y]
end
function Day06:visit_position(res)
local guard = self.input.guard
local pos = res.positions
pos[guard.x] = pos[guard.x] or {}
pos[guard.x][guard.y] = true
end
function Day06:simulate_guard()
local res = {
count = 0,
positions = {},
guard_states = {},
is_loop = false
}
while not self.input:is_out_of_bounds(self.input.guard) do
if self:state_visited(res) then
res.is_loop = true
return res
end
self:visit_state(res)
if not self:position_visited(res) then
res.count = res.count + 1
end
self:visit_position(res)
self:move_guard()
end
return res
end
local Part1 = Day06:new()
function Part1:new(input)
local o = {}
Day06.new(self, o, input)
return o
end
function Part1:solve()
return self:simulate_guard().count
end
local Part2 = Day06:new()
function Part2:new(input)
local o = {}
Day06.new(self, o, input)
return o
end
function Part2:solve()
local guard = copy(self.input.guard)
local obstacle_candidates = self:simulate_guard().positions
local loops_found = 0
for x, ys in pairs(obstacle_candidates) do
for y, _ in pairs(ys) do
if x ~= guard.x or y ~= guard.y then
self.input.guard = copy(guard)
self.input:set_value(x, y, true)
if self:simulate_guard().is_loop then
loops_found = loops_found + 1
end
self.input:set_value(x, y, nil)
end
end
end
return loops_found
end
local Input = {}
function Input:new(o)
o = o or {}
o.parent = self
setmetatable(o, self)
self.__index = self
local input = read_input("....#.....\
.........#\
..........\
..#.......\
.......#..\
..........\
.#..^.....\
........#.\
#.........\
......#...")
local lines = str.split(input, "\n")
local state = {
guard = {}
}
local OBSTACLE = "#"
local GUARD = "^"
local grid = Grid:new(state, lines, function(g, x, y, line)
if line:byte(y) == OBSTACLE:byte(1) then
g:set_value(x, y, true)
elseif line:byte(y) == GUARD:byte(1) then
state.guard = {
direction = NORTH,
x = x,
y = y
}
end
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())