aoc2021/19.jl

141 lines
4.0 KiB
Julia

include("utils.jl")
function parse_input(filename)
scanners = []
current = []
for line in non_empty_lines(filename)
if line[1] == '-' && line[2] == '-'
push!(scanners, current)
current = []
else
coord = collect(map(m->parse(Int, m.match), eachmatch(r"-?[0-9]+", line)))
push!(current, coord)
end
end
push!(scanners, current)
scanners[2:length(scanners)]
end
transforms = [
# up: z
a -> a,
a -> [-a[2], a[1], a[3]],
a -> [-a[1], -a[2], a[3]],
a -> [a[2], -a[1], a[3]],
# up: -z
a -> [-a[1], a[2], -a[3]],
a -> [a[2], a[1], -a[3]],
a -> [a[1], -a[2], -a[3]],
a -> [-a[2], -a[1], -a[3]],
# up: x
a -> [-a[3], a[2], a[1]],
a -> [a[2], a[3], a[1]],
a -> [a[3], -a[2], a[1]],
a -> [-a[2], -a[3], a[1]],
# up: -x
a -> [a[3], a[2], -a[1]],
a -> [-a[2], a[3], -a[1]],
a -> [-a[3], -a[2], -a[1]],
a -> [a[2], -a[3], -a[1]],
# up: y
a -> [a[1], -a[3], a[2]],
a -> [-a[3], -a[1], a[2]],
a -> [-a[1], a[3], a[2]],
a -> [a[3], a[1], a[2]],
# up: -y
a -> [a[1], a[3], -a[2]],
a -> [-a[3], a[1], -a[2]],
a -> [-a[1], -a[3], -a[2]],
a -> [a[3], -a[1], -a[2]],
]
function try_match(a, b)
for c in a
for d in b
t = c - d
if length(intersect(a, map(x->x+t, b))) > 11
println("pos $t")
#println(intersect(a, map(x->x+t, b)))
return true, collect(map(x->x+t, b))
end
end
end
false, nothing
end
function find_match(fixed, scanners)
for (f,j) in fixed
for i in 1:length(scanners)
s,k = scanners[i]
for t in transforms
td = collect(map(t, s))
matchp, transformed = try_match(f, td)
if matchp
println("f: $(j-1) s: $(k-1)")
return i, transformed
end
end
# println("f: $(j-1) s: $(k-1) x")
end
end
end
function solution19_1()
scanners = collect(parse_input("19.data"))
zipped = collect(zip(scanners, 1:length(scanners)))
fixed = [popfirst!(zipped)]
while length(zipped) > 0
index, transformed = find_match(fixed, zipped)
s = popat!(zipped, index)
# println(s[1])
# println(transformed)
push!(fixed, (transformed, s[2]))
l = length(fixed)
println("located $l")
end
length(reduce(union, map(x->x[1], fixed)))
end
function solution19_2()
positions = [[0, 0, 0],
[-28, 1245, -161],
[1163, 26, -170],
[-1254, 90, -158],
[-18, 1355, -1280],
[1146, -1193, -28],
[-2380, 15, -13],
[-1078, 63, 1010],
[-1105, 28, -1236],
[-12, 2400, -1318],
[-1075, 1190, -1385],
[-2348, 173, -1313],
[-2450, -1024, -26],
[-3547, 0, -79],
[-1233, -1211, 1144],
[-1092, 1288, 1125],
[-2449, 1277, -1238],
[-2290, -2266, -141],
[-4753, 30, -166],
[-3532, 97, 1132],
[-1224, 2573, 1061],
[-2428, 1249, 1069],
[-2269, -3556, -95],
[-1131, -2278, -172],
[-2337, -2411, 1197],
[-4722, 31, 1198],
[-6042, 159, -182],
[-3560, 131, 2245],
[-2385, -3457, 1117],
[-2369, -4811, -22],
[-1144, -3615, -89],
[-4732, -17, 2315],
[-5936, -1062, -10],
[-2339, -3488, 2394],
[-2334, -4684, 1017],
[-3472, -3589, 2361],
[-1256, -3495, 2220]]
reduce(max, map(a->sum(abs.(a)), map(a->a[1]-a[2], [(a,b) for a in positions for b in positions])))
end
solution19_2()