Skip to content

Commit

Permalink
2021.06
Browse files Browse the repository at this point in the history
  • Loading branch information
Doruk Gurleyen committed Dec 6, 2021
1 parent a765b75 commit 86ac86e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
28 changes: 28 additions & 0 deletions challenges/2021/06.exs
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")
30 changes: 30 additions & 0 deletions challenges/2021/06.js
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);
1 change: 1 addition & 0 deletions challenges/2021/inputs/06.txt
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

0 comments on commit 86ac86e

Please sign in to comment.