Day 5 cleanup

trunk
HeNine 3 years ago
parent fcdc7e1f24
commit e68049b284

@ -1,24 +1,32 @@
function task0(file)
lines =
eachline(file) .|>
(x -> match(r"(?<x1>\d+),(?<y1>\d+) -> (?<x2>\d+),(?<y2>\d+)", x)) .|>
(p -> (x1 = parse(Int, getindex(p, "x1")) + 1,
y1 = parse(Int, getindex(p, "y1")) + 1,
x2 = parse(Int, getindex(p, "x2")) + 1,
y2 = parse(Int, getindex(p, "y2")) + 1)) |>
x -> filter(l -> l.x1 == l.x2 || l.y1 == l.y2, x)
function parseinput(file)
eachline(file) .|>
(x -> match(r"(?<x1>\d+),(?<y1>\d+) -> (?<x2>\d+),(?<y2>\d+)", x)) .|>
(p -> (x1 = parse(Int, getindex(p, "x1")) + 1,
y1 = parse(Int, getindex(p, "y1")) + 1,
x2 = parse(Int, getindex(p, "x2")) + 1,
y2 = parse(Int, getindex(p, "y2")) + 1)) .|>
(l -> (
x1 = l.y2 < l.y1 ? l.x2 : (l.y2 > l.y1 ? l.x1 : min(l.x1, l.x2)),
y1 = min(l.y1, l.y2),
x2 = l.y2 > l.y1 ? l.x2 : (l.y2 < l.y1 ? l.x1 : max(l.x1, l.x2)),
y2 = max(l.y1, l.y2)))
xmax = map(l -> max(l.x1, l.x2), lines) |> maximum
end
ymax = map(l -> max(l.y1, l.y2), lines) |> maximum
##
function task0(file)
lines = parseinput(file) |> x -> filter(l -> l.x1 == l.x2 || l.y1 == l.y2, x)
xmax = map(l -> l.x2, lines) |> maximum
ymax = map(l -> l.y2, lines) |> maximum
mappe = zeros(Int, (xmax, ymax))
for line in lines
mappe[min(line.y1, line.y2):max(line.y1, line.y2),
min(line.x1, line.x2):max(line.x1, line.x2)] .+= 1
let (x1, y1, x2, y2) = values(line)
mappe[y1:y2, x1:x2] .+= 1
end
end
sum(mappe .>= 2)
@ -28,42 +36,35 @@ end
println("Day 05 Task 1 Case 0")
open(task0, "day05\\data\\case00.txt") |> display
@assert open(task0, "day05\\data\\case00.txt") == 5
println("Day 05 Task 1 Case 1")
open(task0, "day05\\data\\case01.txt") |> println
@assert open(task0, "day05\\data\\case01.txt") == 5690
##
using LinearAlgebra
function task1(file)
lines =
eachline(file) .|>
(x -> match(r"(?<x1>\d+),(?<y1>\d+) -> (?<x2>\d+),(?<y2>\d+)", x)) .|>
(p -> (x1 = parse(Int, getindex(p, "x1")) + 1,
y1 = parse(Int, getindex(p, "y1")) + 1,
x2 = parse(Int, getindex(p, "x2")) + 1,
y2 = parse(Int, getindex(p, "y2")) + 1))
lines = parseinput(file)
xmax = map(l -> max(l.x1, l.x2), lines) |> maximum
ymax = map(l -> max(l.y1, l.y2), lines) |> maximum
ymax = map(l -> l.y2, lines) |> maximum
mappe = zeros(Int, (xmax, ymax))
for line in lines
if line.x1 == line.x2 || line.y1 == line.y2
mappe[min(line.y1, line.y2):max(line.y1, line.y2),
min(line.x1, line.x2):max(line.x1, line.x2)] .+= 1
else
mappe[min(line.y1, line.y2):max(line.y1, line.y2),
min(line.x1, line.x2):max(line.x1, line.x2)] +=
Matrix{Int}(I, abs(line.x1 - line.x2) + 1, abs(line.y1 - line.y2) + 1) |>
((line.x2 - line.x1) / (line.y2 - line.y1) > 0 ? identity : rotl90)
let (x1, y1, x2, y2) = values(line)
if x1 == x2 || y1 == y2
mappe[y1:y2, x1:x2] .+= 1
else
mappe[y1:y2, min(line.x1, line.x2):max(line.x1, line.x2)] +=
Matrix{Int}(I, abs(line.x1 - line.x2) + 1, abs(line.y1 - line.y2) + 1) |>
((line.x2 - line.x1) / (line.y2 - line.y1) > 0 ? identity : rotl90)
end
end
end
sum(mappe .>= 2)
@ -73,6 +74,8 @@ end
println("Day 05 Task 2 Case 0")
open(task1, "day05\\data\\case00.txt") |> display
@assert open(task1, "day05\\data\\case00.txt") == 12
println("Day 05 Task 2 Case 1")
open(task1, "day05\\data\\case01.txt") |> println
@assert open(task1, "day05\\data\\case01.txt") == 17741

Loading…
Cancel
Save