-
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
Doruk Gurleyen
committed
Dec 6, 2021
1 parent
a765b75
commit 86ac86e
Showing
3 changed files
with
59 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Code.compile_file(Path.join([__ENV__.file], ["../../../utils/utils.exs"])) | ||
|
||
defmodule Challenge do | ||
@moduledoc false | ||
|
||
def first_result(input), do: breed(input, 80) | ||
|
||
def second_result(input), do: breed(input, 256) | ||
|
||
defp breed(input, days) do | ||
state = | ||
input | ||
|> Enum.at(0) | ||
|> String.split(",", trim: true) | ||
|> Enum.map(&String.to_integer/1) | ||
|> Enum.reduce(List.duplicate(0, 9), fn val, acc -> | ||
List.update_at(acc, val - 1, &(&1 + 1)) | ||
end) | ||
|
||
Enum.reduce(1..(days - 1), state, fn _, [q, w, e, r, t, y, u, i, o] -> | ||
[w, e, r, t, y, u, i + q, o, q] | ||
end) | ||
|> Enum.sum() | ||
end | ||
end | ||
|
||
Utils.run(&Challenge.first_result/1, __ENV__.file, "Q1") | ||
Utils.run(&Challenge.second_result/1, __ENV__.file, "Q2") |
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,30 @@ | ||
// Part 1 | ||
const solvePuzzle1 = (input) => { | ||
const fish = parseInput(input); | ||
return breed(fish, 80); | ||
}; | ||
|
||
// Part 2 | ||
const solvePuzzle2 = (input) => { | ||
const fish = parseInput(input); | ||
return breed(fish, 256); | ||
}; | ||
|
||
const breed = (fish, days) => { | ||
let state = Array(9).fill(0); | ||
|
||
fish.forEach((f) => state[f - 1]++); | ||
|
||
for (let i = 0; i < days - 1; i++) { | ||
const newState = state.slice(1).concat(state[0]); | ||
newState[6] += state[0]; | ||
state = newState; | ||
} | ||
|
||
return state.reduce((a, b) => a + b, 0); | ||
}; | ||
|
||
const parseInput = (input) => input.split(",").map((n) => Number(n)); | ||
|
||
require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle1); | ||
require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle2); |
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 @@ | ||
4,2,4,1,5,1,2,2,4,1,1,2,2,2,4,4,1,2,1,1,4,1,2,1,2,2,2,2,5,2,2,3,1,4,4,4,1,2,3,4,4,5,4,3,5,1,2,5,1,1,5,5,1,4,4,5,1,3,1,4,5,5,5,4,1,2,3,4,2,1,2,1,2,2,1,5,5,1,1,1,1,5,2,2,2,4,2,4,2,4,2,1,2,1,2,4,2,4,1,3,5,5,2,4,4,2,2,2,2,3,3,2,1,1,1,1,4,3,2,5,4,3,5,3,1,5,5,2,4,1,1,2,1,3,5,1,5,3,1,3,1,4,5,1,1,3,2,1,1,1,5,2,1,2,4,2,3,3,2,3,5,1,5,1,2,1,5,2,4,1,2,4,4,1,5,1,1,5,2,2,5,5,3,1,2,2,1,1,4,1,5,4,5,5,2,2,1,1,2,5,4,3,2,2,5,4,2,5,4,4,2,3,1,1,1,5,5,4,5,3,2,5,3,4,5,1,4,1,1,3,4,4,1,1,5,1,4,1,2,1,4,1,1,3,1,5,2,5,1,5,2,5,2,5,4,1,1,4,4,2,3,1,5,2,5,1,5,2,1,1,1,2,1,1,1,4,4,5,4,4,1,4,2,2,2,5,3,2,4,4,5,5,1,1,1,1,3,1,2,1 |