-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.jl
33 lines (28 loc) · 817 Bytes
/
Day05.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using LinearAlgebra, StatsBase
# Input data
data = map(
x -> map(
rawpoint -> (raw = split(rawpoint, ","); Tuple(map(v -> parse(Int64, v), raw))),
split(x, " -> "),
),
readlines("Day05.input"),
);
# Part 1
allpoints =
(line) -> (filter(
p -> (norm(line[1] .- p) + norm(line[2] .- p) ≈ norm(line[1] .- line[2])),
vec(
collect(
Iterators.product(
range(extrema(first.(line))...),
range(extrema(last.(line))...),
),
),
),
));
getans = (data) -> count(>(1), values(countmap(Iterators.flatten(map(allpoints, data)))));
ans = getans(filter((line) -> any(line[1] .== line[2]), data));
println("Part 1: $ans")
# Part 2
ans = getans(data);
println("Part 2: $ans")