From 13cf73f4da54210941f857510eb2ce94a7f90751 Mon Sep 17 00:00:00 2001 From: Duff OMelia Date: Fri, 24 Dec 2021 00:00:05 -0500 Subject: [PATCH] Add Day 06 part 1 --- .formatter.exs | 2 +- lib/advent_2021/day_06.ex | 29 +++++++++++++++++++++++++++ test/lib/advent_2021/day_06_test.exs | 25 +++++++++++++++++++++++ test/lib/advent_2021/input/day_06.txt | 1 + 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 lib/advent_2021/day_06.ex create mode 100644 test/lib/advent_2021/day_06_test.exs create mode 100644 test/lib/advent_2021/input/day_06.txt diff --git a/.formatter.exs b/.formatter.exs index 9d22ac2..181e438 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -1,5 +1,5 @@ # Used by "mix format" [ inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], - line_length: 130 + line_length: 150 ] diff --git a/lib/advent_2021/day_06.ex b/lib/advent_2021/day_06.ex new file mode 100644 index 0000000..cc04a4d --- /dev/null +++ b/lib/advent_2021/day_06.ex @@ -0,0 +1,29 @@ +defmodule Advent2021.Day06 do + def fish_count(input, days) do + input + |> retrieve_school + |> go_for_days(days) + |> Enum.count() + end + + def go_for_days(school, 0), do: school + + def go_for_days(school, days) do + go_for_days(increment_day(school), days - 1) + end + + def increment_day(school) do + school + |> Enum.map(&transform_fish/1) + |> List.flatten() + end + + defp transform_fish(0), do: [6, 8] + defp transform_fish(fish), do: fish - 1 + + defp retrieve_school(input) do + input + |> String.split(",", trim: true) + |> Enum.map(&String.to_integer/1) + end +end diff --git a/test/lib/advent_2021/day_06_test.exs b/test/lib/advent_2021/day_06_test.exs new file mode 100644 index 0000000..b5d6084 --- /dev/null +++ b/test/lib/advent_2021/day_06_test.exs @@ -0,0 +1,25 @@ +defmodule Advent2021.Day06Test do + use ExUnit.Case, async: true + + alias Advent2021.Day06 + + test "increment_day" do + assert Day06.increment_day([3, 4, 3, 1, 2]) == [2, 3, 2, 0, 1] + assert Day06.increment_day([2, 3, 2, 0, 1]) == [1, 2, 1, 6, 8, 0] + assert Day06.increment_day([6, 0, 6, 4, 5, 6, 0, 1, 1, 2, 6, 7, 8, 8, 8]) == [5, 6, 8, 5, 3, 4, 5, 6, 8, 0, 0, 1, 5, 6, 7, 7, 7] + end + + test "fish_count" do + assert Day06.fish_count(input(), 18) == 26 + assert Day06.fish_count(input(), 80) == 5934 + end + + test "fish_count real" do + input = File.read!("test/lib/advent_2021/input/day_06.txt") |> String.trim() + assert Day06.fish_count(input, 80) == 373_378 + end + + defp input do + "3,4,3,1,2" + end +end diff --git a/test/lib/advent_2021/input/day_06.txt b/test/lib/advent_2021/input/day_06.txt new file mode 100644 index 0000000..445de95 --- /dev/null +++ b/test/lib/advent_2021/input/day_06.txt @@ -0,0 +1 @@ +1,1,3,1,3,2,1,3,1,1,3,1,1,2,1,3,1,1,3,5,1,1,1,3,1,2,1,1,1,1,4,4,1,2,1,2,1,1,1,5,3,2,1,5,2,5,3,3,2,2,5,4,1,1,4,4,1,1,1,1,1,1,5,1,2,4,3,2,2,2,2,1,4,1,1,5,1,3,4,4,1,1,3,3,5,5,3,1,3,3,3,1,4,2,2,1,3,4,1,4,3,3,2,3,1,1,1,5,3,1,4,2,2,3,1,3,1,2,3,3,1,4,2,2,4,1,3,1,1,1,1,1,2,1,3,3,1,2,1,1,3,4,1,1,1,1,5,1,1,5,1,1,1,4,1,5,3,1,1,3,2,1,1,3,1,1,1,5,4,3,3,5,1,3,4,3,3,1,4,4,1,2,1,1,2,1,1,1,2,1,1,1,1,1,5,1,1,2,1,5,2,1,1,2,3,2,3,1,3,1,1,1,5,1,1,2,1,1,1,1,3,4,5,3,1,4,1,1,4,1,4,1,1,1,4,5,1,1,1,4,1,3,2,2,1,1,2,3,1,4,3,5,1,5,1,1,4,5,5,1,1,3,3,1,1,1,1,5,5,3,3,2,4,1,1,1,1,1,5,1,1,2,5,5,4,2,4,4,1,1,3,3,1,5,1,1,1,1,1,1