-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |