Skip to content

Commit

Permalink
2023, Day 25, kinda solved
Browse files Browse the repository at this point in the history
  • Loading branch information
sweko committed Dec 25, 2023
1 parent 3ac3d53 commit b73aa2f
Show file tree
Hide file tree
Showing 7 changed files with 1,507 additions and 86 deletions.
110 changes: 55 additions & 55 deletions 2023/ts/day-17/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,61 +268,61 @@ const getRightPosition = (costs: number[][], prices: PriceList[][], position: Tr


const partOne = (input: number[][], debug: boolean) => {

const startPosition = { x: 0, y: 0, direction: 'right', distance: -1, price: 0 } as const;

const prices: PriceList[][] = Array(input.length).fill(0).map(_ => Array(input[0].length).fill(0).map(_ => getDefaultPriceList()));
prices[0][0] = {
"up-0": 0,
"up-1": 0,
"up-2": 0,
"down-0": 0,
"down-1": 0,
"down-2": 0,
"left-0": 0,
"left-1": 0,
"left-2": 0,
"right-0": 0,
"right-1": 0,
"right-2": 0,
}

const queue: TravelingPoint[] = [startPosition];

let callCount = 0;

while (queue.length > 0) {
callCount += 1;
if (callCount % 10_000 === 0) {
console.log(`At ${callCount} calls, queue is ${queue.length}`);
}
const position = queue.shift()!;

const upPosition = getUpPosition(input, prices, position);
if (upPosition) {
queue.push(upPosition);
}

const downPosition = getDownPosition(input, prices, position);
if (downPosition) {
queue.push(downPosition);
}

const leftPosition = getLeftPosition(input, prices, position);
if (leftPosition) {
queue.push(leftPosition);
}

const rightPosition = getRightPosition(input, prices, position);
if (rightPosition) {
queue.push(rightPosition);
}
}

console.log(callCount);
const finishPrices = prices[prices.length-1][prices[0].length-1];
const minPrice = Math.min(...Object.values(finishPrices));
return minPrice;
return -1;
// const startPosition = { x: 0, y: 0, direction: 'right', distance: -1, price: 0 } as const;

// const prices: PriceList[][] = Array(input.length).fill(0).map(_ => Array(input[0].length).fill(0).map(_ => getDefaultPriceList()));
// prices[0][0] = {
// "up-0": 0,
// "up-1": 0,
// "up-2": 0,
// "down-0": 0,
// "down-1": 0,
// "down-2": 0,
// "left-0": 0,
// "left-1": 0,
// "left-2": 0,
// "right-0": 0,
// "right-1": 0,
// "right-2": 0,
// }

// const queue: TravelingPoint[] = [startPosition];

// let callCount = 0;

// while (queue.length > 0) {
// callCount += 1;
// if (callCount % 10_000 === 0) {
// console.log(`At ${callCount} calls, queue is ${queue.length}`);
// }
// const position = queue.shift()!;

// const upPosition = getUpPosition(input, prices, position);
// if (upPosition) {
// queue.push(upPosition);
// }

// const downPosition = getDownPosition(input, prices, position);
// if (downPosition) {
// queue.push(downPosition);
// }

// const leftPosition = getLeftPosition(input, prices, position);
// if (leftPosition) {
// queue.push(leftPosition);
// }

// const rightPosition = getRightPosition(input, prices, position);
// if (rightPosition) {
// queue.push(rightPosition);
// }
// }

// console.log(callCount);
// const finishPrices = prices[prices.length-1][prices[0].length-1];
// const minPrice = Math.min(...Object.values(finishPrices));
// return minPrice;
};

const partTwo = (input: number[][], debug: boolean) => {
Expand Down
102 changes: 89 additions & 13 deletions 2023/ts/day-21/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ const processInput = (day: number) => {

type DistancePoint = Point & { steps: number };

const floodFill = (input: Plot[][], startX: number, startY: number, stepsLeft: number) => {
const floodFillOne = (garden: Plot[][], startX: number, startY: number, stepsLeft: number) => {
const allowed = ['.', 'S'];

const queue: DistancePoint[] = [];
queue.push({ x: startX, y: startY, steps: stepsLeft });

let runs = 0;
while (queue.length > 0) {
runs += 1;
const { x, y, steps } = queue.shift()!;
if (x < 0 || y < 0 || x >= input[0].length || y >= input.length) {
if (x < 0 || y < 0 || x >= garden[0].length || y >= garden.length) {
continue;
}
if (!allowed.includes(input[y][x])) {
if (!allowed.includes(garden[y][x])) {
continue;
}
input[y][x] = 'O';
garden[y][x] = 'O';
if (steps === 0) {
continue;
}
Expand All @@ -43,10 +44,11 @@ const floodFill = (input: Plot[][], startX: number, startY: number, stepsLeft: n
queue.push({ x, y: y - 1, steps: steps - 1 });
queue.push({ x, y: y + 1, steps: steps - 1 });
}
console.log(`Runs: ${runs}`);
};

const partOne = (input: Plot[][], debug: boolean) => {
const stepsLeft = 64;
const stepsLeft = 6;

let start = { x: 0, y: 0 };

Expand All @@ -60,10 +62,12 @@ const partOne = (input: Plot[][], debug: boolean) => {
}
}

floodFill(input, start.x, start.y, stepsLeft);
const garden = input.map(row => row.slice());

floodFillOne(garden, start.x, start.y, stepsLeft);
// printMatrix(input);

const plots = input.flatMap((row, rindex) => row.map((element, cindex) => ({ element, rindex, cindex })));
const plots = garden.flatMap((row, rindex) => row.map((element, cindex) => ({ element, rindex, cindex })));
const finalPlots = plots
.filter(plot => plot.element === 'O')
.filter(plot => {
Expand All @@ -74,11 +78,83 @@ const partOne = (input: Plot[][], debug: boolean) => {
return finalPlots.length;
};

const floodFillTwo = (garden: Plot[][], startX: number, startY: number, stepsLeft: number) => {
const allowed = ['.', 'S'];

const queue: DistancePoint[] = [];
queue.push({ x: startX, y: startY, steps: stepsLeft });
const result = new Map<string, number>();
let runs = 0;
while (queue.length > 0) {
runs += 1;
if (runs % 10_000 === 0) {
console.log(`Runs: ${runs}, queue: ${queue.length}`);
}
const { x, y, steps } = queue.shift()!;

let xlookup = x % garden[0].length;
if (xlookup < 0) {
xlookup += garden[0].length;
}
let ylookup = y % garden.length;
if (ylookup < 0) {
ylookup += garden.length;
}

// console.log(`Checking ${xlookup}, ${ylookup}`);
// console.log(garden[ylookup][xlookup]);
if (garden[ylookup][xlookup] === "#") {
continue;
}
const key = `(${x},${y})`;
const distance = Math.abs(y - startY) + Math.abs(x - startX);
result.set(key, distance);
if (steps === 0) {
continue;
}
queue.push({ x: x - 1, y, steps: steps - 1 });
queue.push({ x: x + 1, y, steps: steps - 1 });
queue.push({ x, y: y - 1, steps: steps - 1 });
queue.push({ x, y: y + 1, steps: steps - 1 });
}
console.log(`Runs: ${runs}`);
return result;
};


const partTwo = (input: Plot[][], debug: boolean) => {
if (debug) {
console.log("-------Debug-----");
const stepsLeft = 50;

let start = { x: 0, y: 0 };

for (let rindex = 0; rindex < input.length; rindex++) {
const row = input[rindex];
for (let cindex = 0; cindex < row.length; cindex++) {
const element = row[cindex];
if (element === 'S') {
start = { x: cindex, y: rindex };
}
}
}
return input.length;

const places = floodFillTwo(input, start.x, start.y, stepsLeft);

const finalPlots = [...places.values()].filter(distance => {
return (distance % 2) === (stepsLeft % 2);
}).length;

return finalPlots;
// printMatrix(input);

// const plots = input.flatMap((row, rindex) => row.map((element, cindex) => ({ element, rindex, cindex })));
// const finalPlots = plots
// .filter(plot => plot.element === 'O')
// .filter(plot => {
// const distance = Math.abs(plot.rindex - start.y) + Math.abs(plot.cindex - start.x);
// return (distance % 2) === (stepsLeft % 2);
// });

// return finalPlots.length;
};

const resultOne = (_: any, result: number) => {
Expand All @@ -101,9 +177,9 @@ export const solution: Puzzle<Plot[][], number> = {
day: 21,
input: () => processInput(21),
partOne,
partTwo,
// partTwo,
resultOne: resultOne,
resultTwo: resultTwo,
// resultTwo: resultTwo,
showInput,
test,
}
10 changes: 5 additions & 5 deletions 2023/ts/day-24/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ const partOne = (input: Particle[], debug: boolean) => {
for (let sindex = findex + 1; sindex < params.length; sindex++) {
const second = params[sindex];
if (first.slope === second.slope) {
console.log(`Parallel lines ${findex} and ${sindex}`);
// console.log(`Parallel lines ${findex} and ${sindex}`);
continue;
}
const intx = (second.intercept - first.intercept) / (first.slope - second.slope);
const inty = first.slope * intx + first.intercept;
if (!first.isFuture(inty)) {
console.log(`The point is in the past for ${findex}`);
// console.log(`The point is in the past for ${findex}`);
continue;
}
if (!second.isFuture(inty)) {
console.log(`The point is in the past for ${sindex}`);
// console.log(`The point is in the past for ${sindex}`);
continue;
}
console.log(`Intersection between ${findex} and ${sindex} is at ${intx},${inty}`);
// console.log(`Intersection between ${findex} and ${sindex} is at ${intx},${inty}`);
if (intx >= min && intx <= max && inty >= min && inty <= max) {
console.log(` Intersection is in the box`);
// console.log(` Intersection is in the box`);
intersections += 1;
}
}
Expand Down
Loading

0 comments on commit b73aa2f

Please sign in to comment.