Skip to content

Commit

Permalink
naive solution
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 11, 2024
1 parent 081f706 commit 0b5b7d9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/2024/day11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function next(stone) {
if (stone === 0) {
return [1];
}
if (`${stone}`.length % 2 === 0) {
const s = `${stone}`;
return [
parseInt(s.slice(0, s.length / 2)),
parseInt(s.slice(s.length / 2)),
];
}
return [stone * 2024];
}

const memory = new Map();
function do25(stone) {
if (memory.has(stone)) return memory.get(stone);
let stones = [stone];
for (let j = 0; j < 25; j++) stones = stones.flatMap(next);
memory.set(stone, stones);
return stones;
}

// function doit(stone, times = 25) {
// if (times === 0) return 1;

// const key = `${stone},${times}`;
// if (memory.has(key)) return memory.get(key);
// let sum = 0;
// let stones = next(stone);
// stones.forEach(stone => {
// sum += doit(stone, times - 1);
// });
// memory.set(key, sum);
// return sum;
// }

export function part1(input) {
let stones = input.split(" ").map(Number);
let sum = 0;
stones.forEach(stone => {
sum += do25(stone).length;
});
return sum;
}

export function part2(input) {
let stones = input.split(" ").map(Number);
let sum = 0;
stones.forEach(stone => {
do25(stone).forEach(stone => {
do25(stone).forEach(stone => {
sum += do25(stone).length;
});
});
});
return sum;
}
26 changes: 26 additions & 0 deletions src/2024/day11.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { part1, part2 } from "./day11.js";
import readInput from "../utils/read-input.js";

const input = readInput(import.meta.url);

describe("day11 2024", () => {
describe("part1", () => {
test("it should work for part 1 examples", () => {
expect(part1(["125 17"].join("\n"))).toEqual(55312);
});

test("it should work for part 1 input", () => {
expect(part1(input)).toEqual(197157);
});
});

describe("part2", () => {
test("it should work for part 2 examples", () => {
// expect(part2(paste)).toEqual(paste);
});

test("it should work for part 2 input", () => {
expect(part2(input)).toEqual(234430066982597);
});
});
});
2 changes: 2 additions & 0 deletions src/2024/day11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0 44 175060 3442 593 54398 9 8101095

0 comments on commit 0b5b7d9

Please sign in to comment.