1
0
Fork 0
advent-of-code/2021/lib/advent_of_code/day02.ex

53 lines
1.1 KiB
Elixir
Raw Normal View History

2022-09-21 09:19:53 -04:00
defmodule AdventOfCode.Day02 do
@moduledoc """
Day 2
"""
end
defmodule AdventOfCode.Day02.Part1 do
@moduledoc """
Day 2, Part 1
"""
alias AdventOfCode.PuzzleSolver
use PuzzleSolver
import AdventOfCode.Day02, warn: false
@impl PuzzleSolver
def solve(input_stream) do
input_stream
|> Stream.map(&String.trim/1)
|> Stream.map(&parse_action/1)
|> Enum.reduce({0, 0}, &apply_action/2)
|> product()
end
defp parse_action("forward " <> v), do: {:forward, String.to_integer(v)}
defp parse_action("down " <> v), do: {:down, String.to_integer(v)}
defp parse_action("up " <> v), do: {:up, String.to_integer(v)}
defp apply_action({:forward, v}, {horiz, depth}), do: {horiz + v, depth}
defp apply_action({:down, v}, {horiz, depth}), do: {horiz, depth + v}
defp apply_action({:up, v}, {horiz, depth}), do: {horiz, depth - v}
defp product({x, y}), do: x * y
end
defmodule AdventOfCode.Day02.Part2 do
@moduledoc """
Day 2, Part 2
"""
alias AdventOfCode.PuzzleSolver
use PuzzleSolver
import AdventOfCode.Day02, warn: false
@impl PuzzleSolver
def solve(_input_stream) do
:ok |> to_string()
end
end