-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.jl
49 lines (40 loc) · 1.07 KB
/
03.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
"""
Day 3: Rucksack Reorganization
https://adventofcode.com/2022/day/3
"""
function part1(lines)
priorities = Vector{Int}()
for line in lines
first_compartment = line[1:length(line)÷2]
second_compartment = line[length(line)÷2+1:end]
letter = intersect(first_compartment, second_compartment)[1]
priority = Int(letter)
if priority > 96
priority -= 96
else
priority -= 38
end
push!(priorities, priority)
end
return sum(priorities)
end
function part2(lines)
priorities = Vector{Int}()
for i in 1:3:length(lines)
letter = intersect(lines[i], intersect(lines[i+1], lines[i+2]))[1]
priority = Int(letter)
if priority > 96
priority -= 96
else
priority -= 38
end
push!(priorities, priority)
end
return sum(priorities)
end
example = readlines("examples/03.txt")
input = readlines("inputs/03.txt")
@assert part1(example) == 157
println(part1(input))
@assert part2(example) == 70
println(part2(input))