-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.jl
38 lines (34 loc) · 1 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
const matching = Dict(')' => '(', ']' => '[', '}' => '{', '>' => '<')
const illegal_score = Dict(')' => 3, ']' => 57, '}' => 1197, '>' => 25137)
const autocomplete_score = Dict('(' => 1, '[' => 2, '{' => 3, '<' => 4)
score1 = 0
score2s = []
for l ∈ readlines("data/day10.txt")
stack = []
illegal = false
for c ∈ l
if c ∈ "([{<"
push!(stack, c)
else
if isempty(stack) || stack[end] != matching[c]
global score1 += illegal_score[c]
illegal = true
break
else
pop!(stack)
end
end
end
if !illegal
score2 = 0
for c ∈ reverse(stack)
score2 = score2 * 5 + autocomplete_score[c]
end
push!(score2s, score2)
end
end
# Part 1 - What is the total syntax error score for those errors?
println("part1 = ", score1)
# Part 2 - Find the completion string for each incomplete line, score the completion strings, and sort the scores. What is the middle score?
sort!(score2s)
println("part2 = ", score2s[(length(score2s)+1) ÷ 2])