You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
2.9 KiB
120 lines
2.9 KiB
#!/usr/bin/env julia
|
|
|
|
using Test
|
|
|
|
struct Move
|
|
direction :: Char
|
|
count :: Int
|
|
end
|
|
|
|
mutable struct Point{T}
|
|
x::T
|
|
y::T
|
|
end
|
|
|
|
function parse_move(line)
|
|
parts = split(line, limit=2)
|
|
return Move(parts[1][1], parse(Int, parts[2]))
|
|
end
|
|
|
|
function read_moves(io)
|
|
return [parse_move(line) for line in readlines(io)]
|
|
end
|
|
|
|
function apply_move(head, tail, direction)
|
|
if direction == 'U'
|
|
head.y += 1
|
|
elseif direction == 'D'
|
|
head.y -= 1
|
|
elseif direction == 'L'
|
|
head.x -= 1
|
|
elseif direction == 'R'
|
|
head.x += 1
|
|
elseif direction != 'O'
|
|
throw(DomainError("Unknown direction"))
|
|
end
|
|
x_diff = head.x - tail.x
|
|
y_diff = head.y - tail.y
|
|
if abs(x_diff) <= 1 && abs(y_diff) <= 1
|
|
return head, tail
|
|
end
|
|
if abs(x_diff) <= 1
|
|
tail.x += x_diff
|
|
tail.y += sign(y_diff) * (abs(y_diff) - 1)
|
|
elseif abs(y_diff) <= 1
|
|
tail.y += y_diff
|
|
tail.x += sign(x_diff) * (abs(x_diff) - 1)
|
|
elseif abs(x_diff) == 2 && abs(y_diff) == 2
|
|
# possible with more than 2 knots
|
|
tail.x += sign(x_diff) * (abs(x_diff) - 1)
|
|
tail.y += sign(y_diff) * (abs(y_diff) - 1)
|
|
else
|
|
throw(DomainError("invalid knots " * string(head) * " " * string(tail)))
|
|
end
|
|
return head, tail
|
|
end
|
|
|
|
function get_visited(moves; nknots::Int=2)
|
|
visited = Set{NTuple{2, Int}}()
|
|
knots = [Point(0, 0) for _ in 1:nknots]
|
|
push!(visited, (knots[nknots].x, knots[nknots].y))
|
|
for move in moves
|
|
for _ in 1:move.count
|
|
knots[1], knots[2] = apply_move(knots[1], knots[2], move.direction)
|
|
for i in 2:nknots
|
|
_, knots[i] = apply_move(knots[i-1], knots[i], 'O')
|
|
end
|
|
push!(visited, (knots[nknots].x, knots[nknots].y))
|
|
end
|
|
end
|
|
return visited
|
|
end
|
|
|
|
function visited_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)
|
|
for (x, y) in visited
|
|
vmap[nrows - (y - min_y), (x - min_x + 1)] = 1
|
|
end
|
|
return vmap
|
|
end
|
|
|
|
function test()
|
|
@testset "visited count 2 knot" verbose=true begin
|
|
@test length(get_visited(read_moves("example.txt"))) == 13
|
|
@test length(get_visited(read_moves("input.txt"))) == 6037
|
|
end
|
|
@testset "visited count 10 knot" verbose=true begin
|
|
@test length(get_visited(read_moves("example.txt"), nknots=10)) == 1
|
|
@test length(get_visited(read_moves("input.txt"), nknots=10)) == 2485
|
|
end
|
|
end
|
|
|
|
function main()
|
|
if size(ARGS, 1) == 0
|
|
test()
|
|
else
|
|
infile = ARGS[1]
|
|
println("infile = ", infile)
|
|
moves = read_moves(infile)
|
|
println("moves: ", moves)
|
|
visited = get_visited(moves)
|
|
println("visited: ", visited)
|
|
println("count : ", length(visited))
|
|
display(visited_map(visited))
|
|
println()
|
|
|
|
visited10 = get_visited(moves, nknots=10)
|
|
println("visited10: ", visited10)
|
|
println("count10 : ", length(visited10))
|
|
display(visited_map(visited10))
|
|
println()
|
|
|
|
end
|
|
end
|
|
|
|
main()
|