Day 5 cleanup

trunk
HeNine 3 years ago
parent fcdc7e1f24
commit e68049b284

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

Loading…
Cancel
Save