-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.jl
241 lines (209 loc) · 6.07 KB
/
17.jl
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using Revise
includet("IntComputer.jl"); using .IntComputer
using Combinatorics
# Part 1
parse_output(out) = transpose(reshape(out[1:end-1],findfirst(==(Int('\n')),out),:))[1:end,1:end-1]
code = intcode_from_file("17.input")
out = run!(code)
parsed = parse_output(out)
is_intersection(parsed,r,c) = parsed[r,c] && parsed[r-1,c] && parsed[r,c-1] && parsed[r+1,c] && parsed[r,c+1]
_parsed = parsed .== Int('#')
intersections = [(r,c) for (r,c) in Iterators.product(2:size(parsed,1)-1,2:size(parsed,2)-1) if is_intersection(_parsed,r,c)]
mapreduce(+,intersections) do inter
*(inter[1]-1,inter[2]-1)
end
# Part 2
const Point = Tuple{Int,Int}
const NORTH = 0
const EAST = 1
const SOUTH = 2
const WEST = 3
const direction_mapping = Dict(
Int('^') => NORTH,
Int('v') => SOUTH,
Int('<') => WEST,
Int('>') => EAST)
const reverse_direction_mapping = Dict(
NORTH=>Int('^'),
SOUTH=>Int('v'),
WEST=>Int('<'),
EAST=>Int('>'))
const unit_vectors_direction = Dict(
NORTH=>(-1,0),
SOUTH=>(1,0),
WEST =>(0,-1),
EAST =>(0,1),
)
mutable struct Robot
map::Matrix{Int}
location::Point
orientation::Int
log::Vector{Union{Symbol,Int}}
function Robot(map::Matrix{Int})
location = findfirst(x->x in keys(direction_mapping),map)
this = new(copy(map), location.I, direction_mapping[map[location]], Union{Symbol,Int}[])
this.map[location] = Int('#')
return this
end
end
function turn(current_orientation::Int,dir::Int)
orientation = ( current_orientation + dir ) % 4
if orientation < 0
orientation += 4
end
return orientation
end
function turnLeft!(r::Robot)
push!(r.log,:L)
r.orientation = turn(r.orientation,-1)
nothing
end
function turnRight!(r::Robot)
push!(r.log,:R)
r.orientation = turn(r.orientation,1)
nothing
end
function move!(r::Robot)
new_location = r.location .+ unit_vectors_direction[r.orientation]
@assert r.map[CartesianIndex(new_location)] == Int('#') "Don't jump, please"
if length(r.log) > 0 && r.log[end] isa Int
r.log[end] += 1
else
push!(r.log,1)
end
r.location = new_location
nothing
end
function viz(r::Robot)
new_map = copy(r.map)
new_map[CartesianIndex(r.location)] = reverse_direction_mapping[r.orientation]
for r in eachrow(new_map)
print.(Char.(r))
println()
end
end
abyss_front(r::Robot) = try
return r.map[CartesianIndex(r.location .+ unit_vectors_direction[r.orientation])] != Int('#')
catch e
return true
end
abyss_left(r::Robot) = try
r.map[CartesianIndex(r.location .+ unit_vectors_direction[turn(r.orientation,-1)])] != Int('#')
catch e
return true
end
abyss_right(r::Robot) = try
r.map[CartesianIndex(r.location .+ unit_vectors_direction[turn(r.orientation,1)])] != Int('#')
catch e
return true
end
function visit_all!(r::Robot)
while !(abyss_front(r) && abyss_left(r) && abyss_right(r))
while !abyss_front(r)
move!(r)
end
if abyss_left(r) && !abyss_right(robot)
turnRight!(robot)
elseif !abyss_left(r) && abyss_right(robot)
turnLeft!(robot)
end
end
end
function counter_seq_occurrence(log,seq)
c = 0
for i in 1:length(log)-length(seq)
if log[i:i+length(seq)-1] == seq
c += 1
end
end
return c
end
function analyze(log, start=Union{Symbol,Int}[], sequences=Dict{Tuple,Int}())
from, to = extrema(filter(x->x isa Int,log))
seq = Union{Symbol,Int}[start...,:R,0]
for len in from:to
seq[end] = len
seq[end-1] = :R
c = counter_seq_occurrence(log,seq)
if c > 0 && 2*length(seq)-1 <= 20
sequences[Tuple(seq)] = c*(length(seq)-1)
analyze(log, seq, sequences)
end
seq[end-1] = :L
c = counter_seq_occurrence(log,seq)
if c > 0 && 2*length(seq)-1 <= 20
sequences[Tuple(seq)] = c*(length(seq)-1)
analyze(log, seq, sequences)
end
end
return sequences
end
function replace_combination!(log,combination,rep)
while true
for i in 1:length(log)-length(combination)+1
rng = i:i+length(combination)-1
if all(log[rng] .== combination)
splice!(log,rng,[rep])
break
end
i == length(log)-length(combination)+1 && return nothing
end
end
end
function find_optimum(log,comb)
best_val = length(log)
best_comb = Vector{Any}(undef,3)
for (c1,c2,c3) in combinations(collect(keys(comb)),3)
_log = copy(log)
replace_combination!(_log,c1,:A)
replace_combination!(_log,c2,:B)
replace_combination!(_log,c3,:C)
if length(_log) < best_val
best_val = length(_log)
best_comb[1] = c1
best_comb[2] = c2
best_comb[3] = c3
# If everything was replaced an the length fits in memory, it quite likely is the solution
if all(isa.(_log,Symbol)) && 2*length(_log)-1 <= 20
break
end
end
end
return best_comb, best_val
end
function to_terminated_ascii(seq)
out = Int[]
for c in seq
if c isa Symbol
push!(out,Int(first(string(c))))
elseif c >= 10
c1 = c÷2
c2 = c-c1
push!(out,c1+48)
push!(out,Int(','))
push!(out,c2+48)
else
push!(out,c+48)
end
push!(out,Int(','))
end
out[end] = Int('\n')
return out
end
function part2(file,seq,combinations)
code = intcode_from_file(file)
commands = to_terminated_ascii(seq)
combs = to_terminated_ascii.(combinations)
code[1] = 2 # Wake up robot
append!.(Ref(commands),combs)
push!(commands,Int('n'))
push!(commands,Int('\n'))
run!(code,input=commands)
end
robot = Robot(parsed)
visit_all!(robot)
sequences = analyze(robot.log)
best_comb, best_val = find_optimum(robot.log,sequences)
main_code = copy(robot.log)
replace_combination!.(Ref(main_code),best_comb,[:A,:B,:C])
part2("17.input", main_code, best_comb)[end]