diff --git a/2021/README.md b/2021/README.md index 6d6285b..28c371f 100644 --- a/2021/README.md +++ b/2021/README.md @@ -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 ` 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. [1]: ./lib/advent_of_code/day01.ex + +[2]: ./lib/advent_of_code/day02.ex diff --git a/2021/lib/advent_of_code/day02.ex b/2021/lib/advent_of_code/day02.ex new file mode 100644 index 0000000..e263209 --- /dev/null +++ b/2021/lib/advent_of_code/day02.ex @@ -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 + diff --git a/2021/test/advent_of_code/day02/part_1_test.exs b/2021/test/advent_of_code/day02/part_1_test.exs new file mode 100644 index 0000000..5af6f6e --- /dev/null +++ b/2021/test/advent_of_code/day02/part_1_test.exs @@ -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 diff --git a/2021/test/advent_of_code/day02/part_2_test.exs b/2021/test/advent_of_code/day02/part_2_test.exs new file mode 100644 index 0000000..3095e6e --- /dev/null +++ b/2021/test/advent_of_code/day02/part_2_test.exs @@ -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 \ No newline at end of file