Skip to content

Commit

Permalink
AoC 2024 - Day 11 (part I solution)
Browse files Browse the repository at this point in the history
  • Loading branch information
sweko committed Dec 11, 2024
1 parent b770c79 commit bc61ce6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
105 changes: 105 additions & 0 deletions 2024/ts/day-11/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Solution for day 11 of advent of code 2024

import { readInputLines, readInput } from "../system/aoc-helper";
import "../utils/array-helpers";
import { Puzzle } from "../model/puzzle";

const processInput = (day: number) => {
const line = readInput(day);
const stones = line.split(" ").map((item) => parseInt(item, 10));
return stones;
};

const partOne = (input: number[], debug: boolean) => {
const stones = input.slice();

const loopTarget = 25;

for (let loop = 1; loop <= loopTarget; loop++) {
let index = 0;
while (index < stones.length) {
const stone = stones[index];
if (stone === 0) {
stones[index] = 1;
index += 1;
continue;
}
const stoneStr = stone.toString();
if (stoneStr.length % 2 === 0) {
const firstHalf = parseInt(stoneStr.slice(0, stoneStr.length / 2), 10);
const secondHalf = parseInt(stoneStr.slice(stoneStr.length / 2), 10);
stones[index] = firstHalf;
stones.splice(index + 1, 0, secondHalf);
index += 2;
continue;
}

stones[index] = stones[index] * 2024;
index += 1;
}
//console.log(`Loop ${loop} - ${stones}`);
}
return stones.length;
};

const partTwo = (input: number[], debug: boolean) => {
let stones = input.slice();

const loopTarget = 75;

for (let loop = 1; loop <= loopTarget; loop++) {
const newStones = [];
let index = 0;

while (index < stones.length) {
const stone = stones[index];
if (stone === 0) {
newStones.push(1);
index += 1;
continue;
}
const stoneStr = stone.toString();
if (stoneStr.length % 2 === 0) {
const firstHalf = parseInt(stoneStr.slice(0, stoneStr.length / 2), 10);
const secondHalf = parseInt(stoneStr.slice(stoneStr.length / 2), 10);
newStones.push(firstHalf);
newStones.push(secondHalf);
index += 1;
continue;
}

newStones.push(stones[index] * 2024);
index += 1;
}
stones = newStones;
console.log(`Loop ${loop} - ${stones.length}`);
}
return stones.length;
};

const resultOne = (_: any, result: number) => {
return `Result part one is ${result}`;
};

const resultTwo = (_: any, result: number) => {
return `Result part two is ${result}`;
};

const showInput = (input: number[]) => {
console.log(input);
};

const test = (_: number[]) => {
console.log("----Test-----");
};

export const solution: Puzzle<number[], number> = {
day: 11,
input: () => processInput(11),
partOne,
partTwo,
resultOne: resultOne,
resultTwo: resultTwo,
showInput,
test,
}
1 change: 1 addition & 0 deletions 2024/ts/day-11/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17

0 comments on commit bc61ce6

Please sign in to comment.