-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.jl
52 lines (50 loc) · 1.14 KB
/
day7.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
function solve(n::Int, ms; allow_concat::Bool=false)
s1 = Set{Int}()
s2 = Set{Int}()
push!(s1, 0)
for m in ms
r = 10^length("$m")
for s in s1
if n >= s * m
push!(s2, s * m)
end
if n >= s + m
push!(s2, s + m)
end
if allow_concat && n >= s * r + m
push!(s2, s * r + m)
end
end
empty!(s1)
s1, s2 = s2, s1
end
return n ∈ s1
end
function part1(input::String)
result = 0
for line in split(rstrip(input), '\n')
left, right = split(line, ": ")
n = parse(Int, left)
m = map(split(right, ' ')) do s
parse(Int, s)
end
if solve(n, m)
result += n
end
end
result
end
function part2(input::String)
result = 0
for line in split(rstrip(input), '\n')
left, right = split(line, ": ")
n = parse(Int, left)
m = map(split(right, ' ')) do s
parse(Int, s)
end
if solve(n, m; allow_concat=true)
result += n
end
end
result
end