Skip to content

Instantly share code, notes, and snippets.

@xzec
Created December 17, 2022 23:44
Show Gist options
  • Save xzec/76bcfd20688ff34407c95c2f4e8acb8c to your computer and use it in GitHub Desktop.
Save xzec/76bcfd20688ff34407c95c2f4e8acb8c to your computer and use it in GitHub Desktop.
const input = await Deno.readTextFile('input.txt')
const lines = input.split('\n')
type Direction = 'L' | 'U' | 'R' | 'D'
type Movement = [Direction, number]
type Vector2D = [x: number, y: number]
type Rope = Vector2D[]
const ROPE_LENGTH = 10
const rope: Rope = Array.from(Array(ROPE_LENGTH), () => [0, 0])
const head = rope[0]
const tail = rope.at(-1) as Vector2D
const tailPositions = new Set<string>(['0,0'])
const areTouching = (distance: Vector2D) =>
distance.every((coordinate) => Math.abs(coordinate) <= 1)
lines.forEach((movement) => {
const [direction, amount] = movement.split(' ') as Movement
for (let i = 0; i < +amount; i++) {
if (direction === 'L') head[0]--
if (direction === 'U') head[1]++
if (direction === 'R') head[0]++
if (direction === 'D') head[1]--
for (let j = 1; j < ROPE_LENGTH; j++) {
const knot = rope[j]
const prev = rope[rope.indexOf(knot) - 1]
const distance: Vector2D = [knot[0] - prev[0], knot[1] - prev[1]]
if (areTouching(distance)) continue
knot[0] -= Math.sign(distance[0])
knot[1] -= Math.sign(distance[1])
if (knot === tail) tailPositions.add(knot.join(','))
}
}
})
console.log(tailPositions.size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment