-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.jl
70 lines (60 loc) · 1.71 KB
/
day10.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function part1(adapters)
adapters |>
sort |>
diff |>
counter |>
values |>
prod
end
function counter(data::AbstractArray{T}) where T
c = Dict{T, Int}()
for d in data
if haskey(c, d)
c[d] += 1
else
c[d] = 1
end
end
c
end
"""
part2 works by knowing that the number of paths to get to any individual adapter is the
sum of the possible paths to each of the three potentially reachable adapters before it
"""
function part2(adapters)
n = length(adapters)
different_paths = zeros(Int, n)
different_paths[1] = 1
for i=1:n
for j=1:3
# If we're over the end, skip
if i+j > n
continue
end
# How big is the gap from item i to item i+j?
item_diff = adapters[i+j]-adapters[i]
# If it's > 3, not reachable.
# If it's <= 3, we can reach the item at i+j from i.
# Add the number of ways to get to item i to the number at i+j
if item_diff <= 3
different_paths[i+j] += different_paths[i]
end
end
end
return last(different_paths)
# This is really just a big accumulation?
# Could this be replaced with a few simpler functions?
end
function main()
adapters = "inputs/day10.txt" |>
readlines |>
x -> parse.(Int, x) |>
x -> vcat([0, maximum(x) + 3], x) |>
sort
part1_solution = part1(adapters)
part2_solution = part2(adapters)
(part1_solution, part2_solution)
end
@time (part1_solution, part2_solution) = main()
@show part1_solution
@show part2_solution