1
0
Fork 0

day 2 part 1

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 0430a32396
commit c53304fa73
4 changed files with 84 additions and 3 deletions

View file

@ -14,7 +14,7 @@
| S | M | T | W | T | F | S | | S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | | [1] | 2 | 3 | 4 | | | | | [1] | [2] | 3 | 4 |
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
@ -31,12 +31,14 @@ Run `mix help <task>` for details.
## [`AdventOfCode.PuzzleSolver`](./lib/advent_of_code/puzzle_solver.ex) ## [`AdventOfCode.PuzzleSolver`](./lib/advent_of_code/puzzle_solver.ex)
A behaviour for a solution to a puzzle. Must define a `solve/2` callback. A behaviour for a solution to a puzzle. Must define a `solve[2] callback.
## [`AdventOfCode.PuzzleCase`](./test/support/puzzle_case.ex) ## [`AdventOfCode.PuzzleCase`](./test/support/puzzle_case.ex)
Case template defining an `assert_solution/2` helper. Case template defining an `assert_solution[2] helper.
<!-- links --> <!-- links -->
[1]: ./lib/advent_of_code/day01.ex [1]: ./lib/advent_of_code/day01.ex
[2]: ./lib/advent_of_code/day02.ex

View file

@ -0,0 +1,52 @@
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

View file

@ -0,0 +1,16 @@
defmodule AdventOfCode.Day02.Part1Test do
use AdventOfCode.PuzzleCase, module: AdventOfCode.Day02.Part1
test "returns the product of horizontal position and depth" do
input = ~S"""
forward 5
down 5
forward 8
up 3
down 8
forward 2
"""
assert_solution(input, 150)
end
end

View file

@ -0,0 +1,11 @@
defmodule AdventOfCode.Day02.Part2Test do
use AdventOfCode.PuzzleCase, module: AdventOfCode.Day02.Part2
test "returns :ok" do
input = ~S"""
input
"""
assert_solution(input, "ok")
end
end