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 bc61ce6 commit 3d40e6a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
74 changes: 65 additions & 9 deletions 2024/ts/day-11/code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Solution for day 11 of advent of code 2024

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

Expand All @@ -11,41 +11,97 @@ const processInput = (day: number) => {
};

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

const loopTarget = 25;

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

while (index < stones.length) {
const stone = stones[index];
if (stone === 0) {
stones[index] = 1;
nextStones.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);
stones[index] = firstHalf;
stones.splice(index + 1, 0, secondHalf);
index += 2;
nextStones.push(firstHalf);
nextStones.push(secondHalf);
index += 1;
continue;
}

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

type LinkedList = {
value: number;
next: LinkedList | null;
}

const toLinkedList = (input: number[]): LinkedList => {
let head: LinkedList = { value: input[0], next: null };
let current = head;
for (let index = 1; index < input.length; index++) {
current.next = { value: input[index], next: null };
current = current.next;
}
return head;
}

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

const head = toLinkedList(stones);
let count = stones.length;

const loopTarget = 25;

for (let loop = 1; loop <= loopTarget; loop++) {
let current: LinkedList | null = head;

while (current !== null) {
if (current.value === 0) {
current.value = 1;
current = current.next;
continue;
}
const stoneStr = current.value.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);
current.value = firstHalf;
const newStone = { value: secondHalf, next: current.next };
current.next = newStone;
current = newStone.next;
count += 1;
continue;
}

current.value = current.value * 2024;
current = current.next;
}

dlog(`Loop ${loop} - ${count}`);
}
return count;
};

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

const loopTarget = 75;
const loopTarget = 10;

for (let loop = 1; loop <= loopTarget; loop++) {
const newStones = [];
Expand Down
2 changes: 1 addition & 1 deletion 2024/ts/day-11/input.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125 17
27 10647 103 9 0 5524 4594227 902936

0 comments on commit 3d40e6a

Please sign in to comment.