-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.jl
49 lines (42 loc) · 1.05 KB
/
13.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
using SparseArrays
using OffsetArrays
using Colors
function slurp_file(filename)
coords, folds = split(read(filename, String), "\n\n")
coords = [parse.(Int, t) for t in split.(split(coords, "\n"), ",")]
folds = split(folds, "\n")
Set(coords), folds
end
coords, folds = slurp_file("13.txt")
function fold(grid, fold_line, idx)
result = Set{Vector{Int}}()
for p in grid
p[idx] = p[idx] > fold_line ? fold_line - (p[idx] - fold_line) : p[idx]
push!(result, p)
end
result
end
function solve(filename)
coords, folds = slurp_file(filename)
for f in folds
axis, line = match(r"fold along ([xy])=(\d+)", f)
line = parse(Int, line)
idx = axis == "x" ? 1 : 2
@show axis, line
coords = fold(coords, line, idx)
end
coords
end
function draw(coords)
result = zeros(50, 50)
result = OffsetArray(result, 0:49, 0:49)
for idx in coords
y, x = idx
result[x,y] = 20
end
Gray.(result)
end
soln = solve("13.txt")
max(last.(soln)...)
soln
draw(soln)