day 2 part 1
This commit is contained in:
parent
0430a32396
commit
c53304fa73
4 changed files with 84 additions and 3 deletions
|
@ -14,7 +14,7 @@
|
|||
|
||||
| S | M | T | W | T | F | S |
|
||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
||||
| | | | [1] | 2 | 3 | 4 |
|
||||
| | | | [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 |
|
||||
|
@ -31,12 +31,14 @@ Run `mix help <task>` for details.
|
|||
|
||||
## [`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)
|
||||
|
||||
Case template defining an `assert_solution/2` helper.
|
||||
Case template defining an `assert_solution[2] helper.
|
||||
|
||||
<!-- links -->
|
||||
|
||||
[1]: ./lib/advent_of_code/day01.ex
|
||||
|
||||
[2]: ./lib/advent_of_code/day02.ex
|
||||
|
|
52
2021/lib/advent_of_code/day02.ex
Normal file
52
2021/lib/advent_of_code/day02.ex
Normal 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
|
||||
|
16
2021/test/advent_of_code/day02/part_1_test.exs
Normal file
16
2021/test/advent_of_code/day02/part_1_test.exs
Normal 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
|
11
2021/test/advent_of_code/day02/part_2_test.exs
Normal file
11
2021/test/advent_of_code/day02/part_2_test.exs
Normal 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
|
Loading…
Reference in a new issue