Skip to content

Commit

Permalink
Add Day 06 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
duff committed Dec 24, 2021
1 parent 385ace9 commit 13cf73f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
line_length: 130
line_length: 150
]
29 changes: 29 additions & 0 deletions lib/advent_2021/day_06.ex
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/lib/advent_2021/day_06_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/lib/advent_2021/input/day_06.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 13cf73f

Please sign in to comment.