day9: better ascii map for debug

main
Bryce Allen 3 years ago
parent 516ba0c389
commit 1c77865ad3

@ -1,6 +1,7 @@
#!/usr/bin/env julia #!/usr/bin/env julia
using Test using Test
using Images
struct Move struct Move
direction :: Char direction :: Char
@ -70,16 +71,45 @@ function get_visited(moves; nknots::Int=2)
return visited return visited
end end
function visited_map(visited) function int_map(visited)
(min_x, max_x) = extrema([t[1] for t in visited]) (min_x, max_x) = extrema([t[1] for t in visited])
(min_y, max_y) = extrema([t[2] for t in visited]) (min_y, max_y) = extrema([t[2] for t in visited])
nrows = max_y - min_y + 1 nrows = max_y - min_y + 1
ncols = max_x - min_x + 1 ncols = max_x - min_x + 1
vmap = zeros(Int, nrows, ncols) imap = zeros(Int, nrows, ncols)
for (x, y) in visited 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 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 end
function test() function test()
@ -104,15 +134,14 @@ function main()
visited = get_visited(moves) visited = get_visited(moves)
println("visited: ", visited) println("visited: ", visited)
println("count : ", length(visited)) println("count : ", length(visited))
display(visited_map(visited)) println(ascii_map(visited))
println() save("visited.png", image_map(visited))
visited10 = get_visited(moves, nknots=10) visited10 = get_visited(moves, nknots=10)
println("visited10: ", visited10) println("visited10: ", visited10)
println("count10 : ", length(visited10)) println("count10 : ", length(visited10))
display(visited_map(visited10)) println(ascii_map(visited10))
println() save("visited10.png", image_map(visited10))
end end
end end

Loading…
Cancel
Save