-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |