-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2021_02.jl
73 lines (62 loc) · 1.26 KB
/
AoC2021_02.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
71
72
73
#! /usr/bin/env julia
#
include("aoc_main.jl")
if abspath(PROGRAM_FILE) == @__FILE__
include("aocd.jl")
using .Aocd
end
module AoC2021_02
include("aoc.jl")
const FORWARD = "forward"
const UP = "up"
const DOWN = "down"
function _parse(line)
tokens = split(line)
if tokens[1] ∉ Set([UP, DOWN, FORWARD])
throw(ArgumentError("Invalid input"))
end
return tokens[1], parse(Int, tokens[2])
end
function part1(input)
hor, ver = 0, 0
for line ∈ input
command, amount = _parse(line)
if command == UP
ver -= amount
elseif command == DOWN
ver += amount
else
hor += amount
end
end
return hor * ver
end
function part2(input)
hor, ver, aim = 0, 0, 0
for line ∈ input
command, amount = _parse(line)
if command == UP
aim -= amount
elseif command == DOWN
aim += amount
else
hor += amount
ver += aim * amount
end
end
return hor * ver
end
TEST = @aoc_splitlines("""
forward 5
down 5
forward 8
up 3
down 8
forward 2
""")
function samples()
@assert part1(TEST) == 150
@assert part2(TEST) == 900
end
end # module AoC2021_02
aoc_main(@__FILE__, ARGS, 2021, 2)