This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_02.ex
47 lines (40 loc) · 1.51 KB
/
day_02.ex
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
defmodule AdventOfCode.Day02 do
import AdventOfCode.Utils
@type simple_state :: {integer(), integer()}
@type advanced_state :: {integer(), integer(), integer()}
@spec part1([binary()]) :: integer()
def part1(args) do
parse_args(args)
|> Enum.reduce({0, 0}, &simple_reducer/2)
|> Kernel.then(fn {a, b} -> a * b end)
end
@spec simple_reducer({String.t(), integer()}, simple_state()) :: simple_state()
defp simple_reducer({instruction, count}, {distance, depth}) do
case instruction do
"forward" -> {distance + count, depth}
"down" -> {distance, depth + count}
"up" -> {distance, depth - count}
end
end
@spec part2([binary()]) :: integer()
def part2(args) do
parse_args(args)
|> Enum.reduce({0, 0, 0}, &advanced_reducer/2)
|> Kernel.then(fn {distance, depth, _} -> distance * depth end)
end
@spec advanced_reducer({String.t(), integer()}, advanced_state()) :: advanced_state()
defp advanced_reducer({instruction, count}, {distance, depth, aim}) do
case instruction do
"forward" -> {distance + count, depth + aim * count, aim}
"down" -> {distance, depth, aim + count}
"up" -> {distance, depth, aim - count}
end
end
@spec parse_args([binary()]) :: [{String.t(), integer()}]
def parse_args(args), do: Enum.map(args, &parse_instruction/1)
@spec parse_instruction(binary()) :: {String.t(), integer()}
def parse_instruction(line) do
[instruction, count] = String.split(line, " ")
{instruction, parse_int!(count)}
end
end