Skip to content

Commit

Permalink
2023.04
Browse files Browse the repository at this point in the history
Q1: 20407
Q1 completed in 0.988ms
Q2: 23806951
Q2 completed in 1.419ms
  • Loading branch information
kzlsakal committed Dec 4, 2023
1 parent 07e92d5 commit 01a8b3f
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 0 deletions.
64 changes: 64 additions & 0 deletions challenges/2023/04.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Code.compile_file(Path.join([__ENV__.file], ["../../../utils/utils.exs"]))

defmodule Challenge do
@moduledoc false

def first_result(input) do
input
|> parse_cards()
|> Enum.reduce(0, fn {[winning, own], _idx}, acc ->
won = get_num_of_won_numbers(winning, own)
2 |> :math.pow(won - 1) |> trunc() |> Kernel.+(acc)
end)
end

def second_result(input) do
cards = parse_cards(input)
number_of_cards = length(cards)
copies = Enum.reduce(cards, %{}, fn {_card, idx}, acc -> Map.put(acc, idx, 1) end)

cards
|> Enum.reduce(copies, fn {[winning, own], idx}, current_copies ->
case get_num_of_won_numbers(winning, own) do
won when won >= 1 ->
num_of_copies = Map.get(current_copies, idx)

Enum.reduce(1..won, current_copies, fn idx_diff, acc ->
case idx + idx_diff do
copy_idx when copy_idx < number_of_cards ->
Map.put(acc, copy_idx, Map.get(acc, copy_idx) + num_of_copies)

_ ->
acc
end
end)

_ ->
current_copies
end
end)
|> Enum.reduce(0, &(elem(&1, 1) + &2))
end

defp get_num_of_won_numbers(winning_nums, own_nums) do
Enum.reduce(own_nums, 0, fn num, acc ->
if num in winning_nums, do: acc + 1, else: acc
end)
end

defp parse_cards(input) do
input
|> Enum.with_index()
|> Enum.map(fn {card, card_idx} ->
card
|> String.split(": ")
|> List.last()
|> String.split(" | ")
|> Enum.map(&String.split(&1, " ", trim: true))
|> then(&{&1, card_idx})
end)
end
end

Utils.run(&Challenge.first_result/1, __ENV__.file, "Q1")
Utils.run(&Challenge.second_result/1, __ENV__.file, "Q2")
52 changes: 52 additions & 0 deletions challenges/2023/04.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Part 1
const solvePuzzle1 = (input) => {
const lines = input.split('\n').map((line) => {
const [winning, own] = line
.split(': ')[1]
.split(' | ')
.map((nums) => nums.split(' ').filter((n) => n !== ''));

let hit = 0;

own.forEach((num) => {
if (winning.includes(num)) {
hit += 1;
}
});

const cardValue = Math.pow(2, hit - 1);
return cardValue >= 1 ? cardValue : 0;
});
return lines.reduce((acc, num) => acc + num, 0);
};

// Part 2
const solvePuzzle2 = (input) => {
const cards = input.split('\n').map((line) =>
line
.split(': ')[1]
.split(' | ')
.map((nums) => nums.split(' ').filter((n) => n !== ''))
);

const copies = cards.map((_) => 1);

cards.forEach(([winning, own], idx) => {
let hit = 0;

own.forEach((num) => {
if (winning.includes(num)) {
hit += 1;
}
});

for (let j = 0; j < hit && idx + j + 1 < copies.length; j++) {
copies[idx + j + 1] += copies[idx];
}
});

return copies.reduce((acc, num) => acc + num, 0);
};

require(__dirname + '/../../utils/test.js').test(__filename, __dirname, solvePuzzle1, '1');
require(__dirname + '/../../utils/test.js').test(__filename, __dirname, solvePuzzle2, '2');
Loading

0 comments on commit 01a8b3f

Please sign in to comment.