aoc2021/5.jl

68 lines
1.7 KiB
Julia
Raw Normal View History

2021-12-22 10:33:30 +00:00
include("utils.jl")
function parse_input(filename)
map(non_empty_lines(filename)) do line
collect(map(x->parse(Int, x.match), eachmatch(r"[0-9]+", line)))
end
end
function solution5_1()
max_x = 0
max_y = 0
lines = filter(parse_input("5.data")) do line
max_x = max(max_x, line[1], line[2])
max_y = max(max_y, line[2], line[4])
line[1] == line[3] || line[2] == line[4]
end
board = fill(0, max_x + 1 , max_y + 1)
for line in lines
if line[1] == line[3]
if line[2] <= line[4]
for i in line[2]:line[4]
board[line[1] + 1, i + 1] += 1
end
else
for i in line[4]:line[2]
board[line[1] + 1, i + 1] += 1
end
end
else
if line[1] <= line[3]
for i in line[1]:line[3]
board[i + 1, line[2] + 1] += 1
end
else
for i in line[3]:line[1]
board[i + 1, line[2] + 1] += 1
end
end
end
end
length(filter(>(1), board))
end
function solution5_2()
max_x = 0
max_y = 0
lines = filter(parse_input("5.data")) do line
max_x = max(max_x, line[1], line[2])
max_y = max(max_y, line[2], line[4])
true
end
board = fill(0, max_x + 1 , max_y + 1)
for line in lines
x = line[1]
y = line[2]
board[x + 1, y + 1] += 1
while x != line[3] || y != line[4]
x += sign(line[3]-line[1])
y += sign(line[4]-line[2])
board[x + 1, y + 1] += 1
end
end
length(filter(>(1), board))
# transpose(board)
end
solution5_2()