day9: better ascii map for debug
This commit is contained in:
47
day9/day9.jl
47
day9/day9.jl
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env julia
|
||||
|
||||
using Test
|
||||
using Images
|
||||
|
||||
struct Move
|
||||
direction :: Char
|
||||
@@ -70,16 +71,45 @@ function get_visited(moves; nknots::Int=2)
|
||||
return visited
|
||||
end
|
||||
|
||||
function visited_map(visited)
|
||||
function int_map(visited)
|
||||
(min_x, max_x) = extrema([t[1] for t in visited])
|
||||
(min_y, max_y) = extrema([t[2] for t in visited])
|
||||
nrows = max_y - min_y + 1
|
||||
ncols = max_x - min_x + 1
|
||||
vmap = zeros(Int, nrows, ncols)
|
||||
imap = zeros(Int, nrows, ncols)
|
||||
for (x, y) in visited
|
||||
vmap[nrows - (y - min_y), (x - min_x + 1)] = 1
|
||||
imap[nrows - (y - min_y), (x - min_x + 1)] = 1
|
||||
end
|
||||
return vmap
|
||||
imap[nrows + min_y, 1 - min_x] = -1
|
||||
return imap
|
||||
end
|
||||
|
||||
function ascii_map(visited)
|
||||
map_char(n) = (n == -1 ? 's'
|
||||
: (n == 1 ? "#"[1] : '.'))
|
||||
srows = mapreduce(map_char, *, int_map(visited), dims=2; init="")
|
||||
return join(srows, "\n")
|
||||
end
|
||||
|
||||
function scale_pixels(img, scale::Int)
|
||||
(nrows, ncols) = size(img)
|
||||
new_img = Matrix{RGB}(undef, nrows * scale, ncols * scale)
|
||||
for i in 1:nrows
|
||||
for j in 1:ncols
|
||||
i2 = (i - 1) * scale + 1
|
||||
j2 = (j - 1) * scale + 1
|
||||
new_img[i2:i2+scale-1, j2:j2+scale-1] .= img[i, j]
|
||||
end
|
||||
end
|
||||
return new_img
|
||||
end
|
||||
|
||||
function image_map(visited; scale::Int=10)
|
||||
imap = int_map(visited)
|
||||
map_color(n) = (n == -1 ? RGB(0, 0.8, 0)
|
||||
: (n == 1 ? RGB(0.2, 0.2, 0.0) : RGB(0, 0, 0)))
|
||||
cmap = map(map_color, int_map(visited))
|
||||
return scale_pixels(cmap, scale)
|
||||
end
|
||||
|
||||
function test()
|
||||
@@ -104,15 +134,14 @@ function main()
|
||||
visited = get_visited(moves)
|
||||
println("visited: ", visited)
|
||||
println("count : ", length(visited))
|
||||
display(visited_map(visited))
|
||||
println()
|
||||
println(ascii_map(visited))
|
||||
save("visited.png", image_map(visited))
|
||||
|
||||
visited10 = get_visited(moves, nknots=10)
|
||||
println("visited10: ", visited10)
|
||||
println("count10 : ", length(visited10))
|
||||
display(visited_map(visited10))
|
||||
println()
|
||||
|
||||
println(ascii_map(visited10))
|
||||
save("visited10.png", image_map(visited10))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user