-
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
4 changed files
with
117 additions
and
2 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,80 @@ | ||
import { loadInput } from "utils"; | ||
|
||
const defaultWidth = 101; | ||
const defaultHeight = 103; | ||
|
||
export function part1( | ||
input: Array<string>, | ||
width = defaultWidth, | ||
height = defaultHeight, | ||
) { | ||
const simulate100 = simulate(100, width, height); | ||
const positions = input.map(parseInput).map(simulate100); | ||
const halfWidth = Math.floor(width / 2); | ||
const halfHeight = Math.floor(height / 2); | ||
let q1 = 0, q2 = 0, q3 = 0, q4 = 0; | ||
for (const { x, y } of positions) { | ||
if (x < halfWidth && y < halfHeight) { | ||
q1++; | ||
} else if (x > halfWidth && y < halfHeight) { | ||
q2++; | ||
} else if (x < halfWidth && y > halfHeight) { | ||
q3++; | ||
} else if (x > halfWidth && y > halfHeight) { | ||
q4++; | ||
} | ||
} | ||
return q1 * q2 * q3 * q4; | ||
} | ||
|
||
export function part2(input: Array<string>) { | ||
const robots = input.map(parseInput); | ||
return simulateUntilUnique(robots, defaultWidth, defaultHeight); | ||
} | ||
|
||
type Robot = { | ||
x: number; | ||
y: number; | ||
vx: number; | ||
vy: number; | ||
}; | ||
|
||
function simulate(seconds: number, width: number, height: number) { | ||
return (robot: Robot) => { | ||
const x = (((robot.x + robot.vx * seconds) % width) + width) % width; | ||
const y = (((robot.y + robot.vy * seconds) % height) + height) % height; | ||
return { x, y }; | ||
}; | ||
} | ||
|
||
function simulateUntilUnique( | ||
robots: Array<Robot>, | ||
width: number, | ||
height: number, | ||
) { | ||
outer: for (let s = 1;; s++) { | ||
const positions = new Set<string>(); | ||
const simulateS = simulate(s, width, height); | ||
for (const robot of robots) { | ||
const { x, y } = simulateS(robot); | ||
const key = `${x},${y}`; | ||
if (positions.has(key)) { | ||
continue outer; | ||
} | ||
positions.add(key); | ||
} | ||
return s; | ||
} | ||
} | ||
|
||
function parseInput(line: string): Robot { | ||
const [l, r] = line.split(" v="); | ||
const [x, y] = l.split("=")[1].split(",").map(Number); | ||
const [vx, vy] = r.split(",").map(Number); | ||
return { x, y, vx, vy }; | ||
} | ||
|
||
if (import.meta.main) { | ||
console.log(part1(await loadInput(14))); | ||
console.log(part2(await loadInput(14))); | ||
} | ||
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,35 @@ | ||
import { expect } from "@std/expect"; | ||
import { describe, it } from "@std/testing/bdd"; | ||
import { part1, part2 } from "./main.ts"; | ||
import { loadInput } from "utils"; | ||
|
||
describe("day 14 example", () => { | ||
const input = [ | ||
"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", | ||
]; | ||
|
||
it("part 1", () => { | ||
expect(part1(input, 11, 7)).toBe(12); | ||
}); | ||
}); | ||
|
||
describe("day 14 solution", () => { | ||
it("part 1", async () => { | ||
expect(part1(await loadInput(14))).toBe(229839456); | ||
}); | ||
|
||
it("part 2", async () => { | ||
expect(part2(await loadInput(14))).toBe(7138); | ||
}); | ||
}); |
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
Submodule inputs
updated
from 8857b5 to e376c2