Skip to content

Commit

Permalink
Solve day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Jan 2, 2025
1 parent 2c78eed commit a01c81a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 2024/day14/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Offset } from '@constants';
import { Coordinate, mapToInt, lines, applyOffset } from '@helper';

export type Robot = { pos: Coordinate; velocity: Offset };
export const parse = (input: string) => {
const robots = lines(input, (l): Robot => {
const [_, pxStr, pyStr, vx, vy] =
/p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)/.exec(l)!;
const [px, py] = mapToInt([pxStr, pyStr]);
return {
pos: { row: py, col: px },
velocity: mapToInt([vx, vy]) as Offset,
};
});
const spaceWidth = Math.max(...robots.map(({ pos: { col } }) => col)) + 1;
const spaceHeight = Math.max(...robots.map(({ pos: { row } }) => row)) + 1;
return { robots, spaceWidth, spaceHeight };
};

export const newPosition = (
{ pos, velocity }: Robot,
spaceWidth: number,
spaceHeight: number,
) => {
const moved = applyOffset(pos, velocity);

return {
col: (moved.col + spaceWidth) % spaceWidth,
row: (moved.row + spaceHeight) % spaceHeight,
};
};
51 changes: 51 additions & 0 deletions 2024/day14/part1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Pair, isInRange, loadData, product, count } from '@helper';
import { Robot, newPosition, parse } from './lib';

const input = await loadData();
const { robots, spaceHeight, spaceWidth } = parse(input);

const moveRobot = (robot: Robot, count = 100): Robot => {
if (count === 0) {
return robot;
}

return {
pos: newPosition(moveRobot(robot, count - 1), spaceWidth, spaceHeight),
velocity: robot.velocity,
};
};

const movedRobots = robots.map((r) => moveRobot(r));

const quadrantWidth = (spaceWidth - 1) / 2;
const quadrantHeight = (spaceHeight - 1) / 2;
const quadrants: Pair<Pair<number>>[] = [
[
[0, quadrantWidth - 1],
[0, quadrantHeight - 1],
],
[
[quadrantWidth + 1, spaceWidth - 1],
[0, quadrantHeight - 1],
],

[
[0, quadrantWidth - 1],
[quadrantHeight + 1, spaceHeight - 1],
],
[
[quadrantWidth + 1, spaceWidth - 1],
[quadrantHeight + 1, spaceHeight - 1],
],
];

const quadrantCounts = quadrants.map((quadrant) => {
return count(movedRobots, ({ pos }) => {
return (
isInRange(quadrant[0][0], pos.col, quadrant[0][1]) &&
isInRange(quadrant[1][0], pos.row, quadrant[1][1])
);
});
});

console.log(product(quadrantCounts));
67 changes: 67 additions & 0 deletions 2024/day14/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
loadData,
newMatrix,
print,
setValueAtCoords,
showVisualization,
count,
} from '@helper';
import { Robot, parse, newPosition } from './lib';

const input = await loadData();
const { robots, spaceWidth, spaceHeight } = parse(input);

const countAdjacentRobots = (robots: Robot[], countOfRobots: number) => {
const groups = robots
.reduce((acc, { pos }) => {
if (!acc.has(pos.row)) {
acc.set(pos.row, []);
}
acc.get(pos.row)!.push(pos.col);
return acc;
}, new Map<number, number[]>())
.values();

return count(Array.from(groups), (cols) => {
const sortedCols = cols.toSorted((a, b) => a - b);

const consecutiveCounts = sortedCols.reduce((acc, curr, index) => {
if (index === 0 || curr !== sortedCols[index - 1] + 1) {
acc.push(1);
} else {
acc[acc.length - 1] += 1;
}
return acc;
}, [] as number[]);

return consecutiveCounts.some((countInSeq) => countInSeq >= countOfRobots);
});
};

const solve = (robots: Robot[], i = 0): [number, Robot[]] => {
// The tree is framed => at the top and bottom many next to each other
if (countAdjacentRobots(robots, 31) === 2) {
return [i, robots];
}

return solve(
robots.map((r) => ({
pos: newPosition(r, spaceWidth, spaceHeight),
velocity: r.velocity,
})),
i + 1,
);
};

const [i, newRobots] = solve(robots);

console.log(i);
if (showVisualization()) {
const visualized = setValueAtCoords(
newMatrix({ cols: spaceWidth, rows: spaceHeight }, ' '),
newRobots.map(({ pos }) => pos),
'#',
);

console.log(print(visualized));
}
4 changes: 4 additions & 0 deletions helpers/count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const count = <T>(
arr: T[],
predicate: (val: T, idx: number) => boolean,
) => arr.filter(predicate).length;
3 changes: 3 additions & 0 deletions helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export * from './matrix/newMatrix';
export * from './matrix/types';
export * from './matrix/utils';
export * from './sort';
export * from './count';
export * from './removeIndex';
export * from './deepEqual';
export * from './matrix/fill';
Expand Down Expand Up @@ -69,3 +70,5 @@ export {
words,
};
export const self = <T>(x: T) => x;

export type Pair<T> = [T, T]
12 changes: 12 additions & 0 deletions input/2024/day14.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3

0 comments on commit a01c81a

Please sign in to comment.