Skip to content

Commit

Permalink
updates for day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ghbuck committed Dec 10, 2024
1 parent ed81390 commit 849d98b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 111 deletions.
153 changes: 43 additions & 110 deletions src/2024/6/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Coordinates, RunParams, Solution } from 'utils/dataTypes/index.js'
import { Point, RunParams, Solution } from 'utils/dataTypes/index.js'
import { getInput } from 'utils/files/index.js'
import { isBetween } from 'utils/maths/index.js'
import { printAnswers } from 'utils/printing/index.js'
Expand All @@ -8,31 +8,20 @@ interface Direction {
y: -1 | 0 | 1
}

interface ObstructionInfo {
coordinates: Coordinates
newDirection: Direction
interface PositionInfo {
point: Point
direction: Direction
}

interface KnownObstructionInfo {
first: ObstructionInfo
second: ObstructionInfo
third: ObstructionInfo
}

interface RouteInfo {
numUniqueLocations: number
numAddedObstructions: number
}

const getStartingPosition = (map: string[][]): Coordinates => {
let caratPosition: Coordinates = {
const getStartingPosition = (map: string[][]): Point => {
let caratPosition: Point = {
x: 0,
y: 0,
}

for (
let rowIndex = 0;
rowIndex < map.length && caratPosition === undefined;
rowIndex < map.length;
++rowIndex
) {
const row = map[rowIndex]
Expand All @@ -43,153 +32,97 @@ const getStartingPosition = (map: string[][]): Coordinates => {
x: caratIndex,
y: rowIndex,
}

break
}
}

return caratPosition
}

const nextPositionOk = (
nextPosition: Coordinates,
maxPosition: Coordinates,
nextPosition: Point | undefined,
maxPosition: Point,
): boolean => {
return (
nextPosition !== undefined &&
isBetween(nextPosition.x, 0, maxPosition.x, true) &&
isBetween(nextPosition.y, 0, maxPosition.y, true)
)
}

const compareDirections = (dir1: Direction, dir2: Direction): boolean => {
return dir1.x === dir2.x && dir1.y === dir2.y
const getNextPosition = (currentPosition: Point, direction: Direction): Point => {
return {
x: currentPosition.x + direction.x,
y: currentPosition.y + direction.y,
}
}

const calculateLoopPositions = (
obstructionPositions: ObstructionInfo[],
): Set<string> => {
const addedObstructions = new Set<string>()

for (const position of obstructionPositions) {
let secondPosition: ObstructionInfo | undefined
let thirdPosition: ObstructionInfo | undefined

if (compareDirections(position.newDirection, { x: 1, y: 0 })) {
secondPosition = obstructionPositions.find(
(item: ObstructionInfo) =>
item.coordinates.y === position.coordinates.y + 1,
)
if (secondPosition !== undefined) {
thirdPosition = obstructionPositions.find(
(item: ObstructionInfo) =>
item.coordinates.y === position.coordinates.y + 1,
)
}
} else if (compareDirections(position.newDirection, { x: -1, y: 0 })) {
break
} else if (compareDirections(position.newDirection, { x: 0, y: 1 })) {
break
} else if (compareDirections(position.newDirection, { x: 0, y: -1 })) {
break
}
const changeDirection = (direction: Direction): Direction => {
let newDirection: Direction = direction

if (secondPosition !== undefined && thirdPosition !== undefined) {
const knownPositions: KnownObstructionInfo = {
first: position,
second: secondPosition,
third: thirdPosition,
}
console.log(knownPositions)
if (direction.x === 0) {
newDirection = {
x: direction.y < 0 ? 1 : -1,
y: 0,
}
} else if (direction.y === 0) {
newDirection = {
x: 0,
y: direction.x < 0 ? -1 : 1,
}
}

return addedObstructions
return newDirection
}

const processRoute = (map: string[][], maxPosition: Coordinates): RouteInfo => {
const processRoute = (map: string[][], maxPosition: Point): number => {
const uniquePositions = new Set<string>()
const obstructionPositions: ObstructionInfo[] = []

let caratPosition = getStartingPosition(map)
uniquePositions.add(`${caratPosition.x}, ${caratPosition.y}`)
uniquePositions.add(JSON.stringify(caratPosition))

let direction: Direction = {
x: 0,
y: -1,
}

let nextPosition: Coordinates = {
x: 0,
y: 0,
}
let nextPosition: Point = getNextPosition(caratPosition, direction)

while (nextPositionOk(nextPosition, maxPosition)) {
nextPosition = {
x: caratPosition.x + direction.x,
y: caratPosition.y + direction.y,
if (map[nextPosition.y][nextPosition.x] === '#') {
direction = changeDirection(direction)
nextPosition = getNextPosition(caratPosition, direction)
}

if (nextPositionOk(nextPosition, maxPosition)) {
if (map[nextPosition.y][nextPosition.x] === '#') {
if (direction.x === 0) {
direction = {
x: direction.y < 0 ? 1 : -1,
y: 0,
}
} else if (direction.y === 0) {
direction = {
x: 0,
y: direction.x < 0 ? -1 : 1,
}
}

obstructionPositions.push({
coordinates: {
x: nextPosition.x,
y: nextPosition.y,
},
newDirection: {
x: direction.x,
y: direction.y,
},
})

nextPosition = {
x: caratPosition.x + direction.x,
y: caratPosition.y + direction.y,
}
}
caratPosition = nextPosition
uniquePositions.add(JSON.stringify(caratPosition))

caratPosition = nextPosition
uniquePositions.add(`${caratPosition.x}, ${caratPosition.y}`)
}
nextPosition = getNextPosition(caratPosition, direction)
}

return {
numUniqueLocations: uniquePositions.size,
numAddedObstructions: calculateLoopPositions(obstructionPositions).size,
}
return uniquePositions.size
}

export const run = (params: RunParams) => {
const solution: Solution = {
part1: params.isTest ? 41 : 5212,
part2: params.isTest ? 6 : undefined,
part2: params.isTest ? 6 : 1767,
}

const map = getInput(params)
.split('\n')
.map((row: string) => row.split(''))

const maxPosition: Coordinates = {
const maxPosition: Point = {
x: map[0].length - 1,
y: map.length - 1,
}

const routeInfo = processRoute(map, maxPosition)

printAnswers({
params,
answer1: routeInfo.numUniqueLocations,
answer2: routeInfo.numAddedObstructions,
answer1: processRoute(map, maxPosition),
answer2: undefined,
solution,
})
}
2 changes: 1 addition & 1 deletion src/utils/dataTypes/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface AnswerParams {
solution: Solution
}

export interface Coordinates {
export interface Point {
x: number
y: number
}
19 changes: 19 additions & 0 deletions src/utils/maths/geometry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Point } from "utils/dataTypes/index.js"

export const findFourthPoint = (
p1: Point,
p2: Point,
p3: Point,
): Point => {
const midpoint = {
x: (p1.x + p2.x) / 2,
y: (p1.y + p2.y) / 2,
}

const p4 = {
x: 2 * midpoint.x - p3.x,
y: 2 * midpoint.y - p3.y,
}

return p4
}
1 change: 1 addition & 0 deletions src/utils/maths/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './comparisons.js'
export * from './geometry.js'

0 comments on commit 849d98b

Please sign in to comment.