-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_02.ex
55 lines (46 loc) · 1.68 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
48
49
50
51
52
53
54
55
defmodule AdventOfCode.Y2021.Day02 do
@moduledoc """
--- Day 2: Dive! ---
Problem Link: https://adventofcode.com/2021/day/2
Difficulty: xs
Tags: grid rust
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2021, 2)
def run(input \\ input()) do
input = parse(input)
{run_1(input), run_2(input)}
end
def run_1(input), do: input |> track_positions() |> then(&(&1.depth * &1.horizontal))
def run_2(input), do: input |> track_aims() |> then(&(&1.depth * &1.horizontal))
def parse(data) do
data
|> Transformers.lines()
|> Enum.map(fn line ->
[direction, value] = String.split(line, " ")
{String.to_existing_atom(direction), String.to_integer(value)}
end)
end
defp track_positions(directions) do
directions
|> Enum.reduce(%{horizontal: 0, depth: 0}, fn
{:forward, v}, %{horizontal: horizontal} = acc -> %{acc | horizontal: horizontal + v}
{:backward, v}, %{horizontal: horizontal} = acc -> %{acc | horizontal: horizontal - v}
{:up, v}, %{depth: depth} = acc -> %{acc | depth: depth - v}
{:down, v}, %{depth: depth} = acc -> %{acc | depth: depth + v}
end)
end
defp track_aims(directions) do
directions
|> Enum.reduce(%{horizontal: 0, depth: 0, aim: 0}, fn
{:forward, v}, %{horizontal: horizontal, depth: depth, aim: aim} = acc ->
%{acc | horizontal: horizontal + v, depth: depth + aim * v}
{:backward, v}, %{horizontal: horizontal} = acc ->
%{acc | horizontal: horizontal - v}
{:up, v}, %{aim: aim} = acc ->
%{acc | aim: aim - v}
{:down, v}, %{aim: aim} = acc ->
%{acc | aim: aim + v}
end)
end
end