Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Dec 15, 2024
1 parent 74e56d1 commit 268b21e
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 125 deletions.
63 changes: 18 additions & 45 deletions 2024/d06/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs'

import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'
import type { Vec2 } from '@magiczne/advent-of-code-ts-core/types'
import { Direction, Vec2 } from '@magiczne/advent-of-code-ts-core/math'

interface Input {
map: Array<Array<'.' | '#'>>
Expand All @@ -11,42 +11,30 @@ interface Input {
const rotate = (speed: Vec2): Vec2 => {
// From going downwards to going left
if (speed.x === 0 && speed.y === 1) {
return {
x: -1,
y: 0,
}
return Direction.left
}

// From going left to going upwards
if (speed.x === -1 && speed.y === 0) {
return {
x: 0,
y: -1,
}
return Direction.up
}

// From going upwards to going right
if (speed.x === 0 && speed.y === -1) {
return {
x: 1,
y: 0,
}
return Direction.right
}

// From going right to going downwards
if (speed.x === 1 && speed.y === 0) {
return {
x: 0,
y: 1,
}
return Direction.down
}

throw new Error('WTF')
}

const part1 = (input: Input): number => {
const guardPos: Vec2 = { x: input.guard.x, y: input.guard.y }
const speed: Vec2 = { x: 0, y: -1 }
const guardPos = input.guard.clone()
const speed = Direction.up

const visited = new Set<string>()
visited.add(`${guardPos.y}_${guardPos.x}`)
Expand All @@ -57,25 +45,21 @@ const part1 = (input: Input): number => {
}

if (input.map[guardPos.y + speed.y][guardPos.x + speed.x] === '#') {
const { x, y } = rotate(speed)

speed.x = x
speed.y = y
speed.updateInPlace(rotate(speed))

continue
}

guardPos.x += speed.x
guardPos.y += speed.y
guardPos.addInPlace(speed)
visited.add(`${guardPos.y}_${guardPos.x}`)
}

return visited.size
}

const part2 = (input: Input): number => {
const guardPos: Vec2 = { x: input.guard.x, y: input.guard.y }
const speed: Vec2 = { x: 0, y: -1 }
const guardPos = input.guard.clone()
const speed = Direction.up

const visited = new Set<string>()
visited.add(`${guardPos.y}_${guardPos.x}`)
Expand All @@ -86,16 +70,12 @@ const part2 = (input: Input): number => {
}

if (input.map[guardPos.y + speed.y][guardPos.x + speed.x] === '#') {
const { x, y } = rotate(speed)

speed.x = x
speed.y = y
speed.updateInPlace(rotate(speed))

continue
}

guardPos.x += speed.x
guardPos.y += speed.y
guardPos.addInPlace(speed)
visited.add(`${guardPos.y}_${guardPos.x}`)
}

Expand All @@ -109,8 +89,8 @@ const part2 = (input: Input): number => {
newMap[pathPart[0]][pathPart[1]] = '#'

// walk and check cycles
const guardPos: Vec2 = { x: input.guard.x, y: input.guard.y }
const speed: Vec2 = { x: 0, y: -1 }
const guardPos = input.guard.clone()
const speed = Direction.up
const visited = new Set<string>()
visited.add(`${guardPos.y}_${guardPos.x}_${speed.y}_${speed.x}`)

Expand All @@ -120,16 +100,12 @@ const part2 = (input: Input): number => {
}

if (newMap[guardPos.y + speed.y][guardPos.x + speed.x] === '#') {
const { x, y } = rotate(speed)

speed.x = x
speed.y = y
speed.updateInPlace(rotate(speed))

continue
}

guardPos.x += speed.x
guardPos.y += speed.y
guardPos.addInPlace(speed)

if (visited.has(`${guardPos.y}_${guardPos.x}_${speed.y}_${speed.x}`)) {
cycles++
Expand Down Expand Up @@ -164,10 +140,7 @@ const reader = (file: string): Input => {

return {
map: map as Array<Array<'.' | '#'>>,
guard: {
x: guardX,
y: guardY,
},
guard: new Vec2({ x: guardX, y: guardY }),
}
}

Expand Down
34 changes: 23 additions & 11 deletions 2024/d08/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs'

import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'
import { combinationsWithoutRepetition } from '@magiczne/advent-of-code-ts-core/combinatorics'
import type { Vec2 } from '@magiczne/advent-of-code-ts-core/types'
import { Vec2 } from '@magiczne/advent-of-code-ts-core/math'

type Antenna = Vec2 & { type: 'Antenna'; code: string }
type Antinode = { type: 'Antinode'; code: string }
Expand Down Expand Up @@ -47,18 +47,24 @@ const part1 = (input: Input): number => {
const distanceX = Math.abs(firstNode.x - secondNode.x)
const distanceY = Math.abs(firstNode.y - secondNode.y)

const secondAntinodeDir: Vec2 = { x: firstNode.x < secondNode.x ? -1 : 1, y: firstNode.y < secondNode.y ? -1 : 1 }
const firstAntinodeDir: Vec2 = { x: firstNode.x < secondNode.x ? 1 : -1, y: firstNode.y < secondNode.y ? 1 : -1 }
const secondAntinodeDir = new Vec2({
x: firstNode.x < secondNode.x ? -1 : 1,
y: firstNode.y < secondNode.y ? -1 : 1,
})
const firstAntinodeDir = new Vec2({
x: firstNode.x < secondNode.x ? 1 : -1,
y: firstNode.y < secondNode.y ? 1 : -1,
})

const antinodes: ReadonlyArray<Vec2> = [
{
new Vec2({
x: firstNode.x - firstAntinodeDir.x * distanceX,
y: firstNode.y - firstAntinodeDir.y * distanceY,
},
{
}),
new Vec2({
x: secondNode.x - secondAntinodeDir.x * distanceX,
y: secondNode.y - secondAntinodeDir.y * distanceY,
},
}),
]

antinodes.forEach(antinode => {
Expand Down Expand Up @@ -92,8 +98,14 @@ const part2 = (input: Input): number => {
const distanceX = Math.abs(firstNode.x - secondNode.x)
const distanceY = Math.abs(firstNode.y - secondNode.y)

const secondAntinodeDir: Vec2 = { x: firstNode.x < secondNode.x ? -1 : 1, y: firstNode.y < secondNode.y ? -1 : 1 }
const firstAntinodeDir: Vec2 = { x: firstNode.x < secondNode.x ? 1 : -1, y: firstNode.y < secondNode.y ? 1 : -1 }
const secondAntinodeDir = new Vec2({
x: firstNode.x < secondNode.x ? -1 : 1,
y: firstNode.y < secondNode.y ? -1 : 1,
})
const firstAntinodeDir = new Vec2({
x: firstNode.x < secondNode.x ? 1 : -1,
y: firstNode.y < secondNode.y ? 1 : -1,
})

let antinodes: Array<Vec2> = []

Expand All @@ -106,7 +118,7 @@ const part2 = (input: Input): number => {
break
}

antinodes.push({ x, y })
antinodes.push(new Vec2({ x, y }))
}

// Generate antinodes in second direction
Expand All @@ -118,7 +130,7 @@ const part2 = (input: Input): number => {
break
}

antinodes.push({ x, y })
antinodes.push(new Vec2({ x, y }))
}

antinodes.forEach(antinode => {
Expand Down
12 changes: 6 additions & 6 deletions 2024/d10/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs'
import { unique } from 'radash'

import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'
import type { Vec2 } from '@magiczne/advent-of-code-ts-core/types'
import { Vec2 } from '@magiczne/advent-of-code-ts-core/math'

interface Input {
map: ReadonlyArray<ReadonlyArray<number>>
Expand All @@ -12,10 +12,10 @@ interface Input {
// Find every neighbor that is larger than one from current position
const findIncrementedNeighbors = (map: ReadonlyArray<ReadonlyArray<number>>, position: Vec2): ReadonlyArray<Vec2> => {
return [
{ x: position.x, y: position.y - 1 },
{ x: position.x, y: position.y + 1 },
{ x: position.x - 1, y: position.y },
{ x: position.x + 1, y: position.y },
new Vec2({ x: position.x, y: position.y - 1 }),
new Vec2({ x: position.x, y: position.y + 1 }),
new Vec2({ x: position.x - 1, y: position.y }),
new Vec2({ x: position.x + 1, y: position.y }),
].filter(item => {
return map[item.y]?.[item.x] === map[position.y][position.x] + 1
})
Expand Down Expand Up @@ -85,7 +85,7 @@ const reader = (file: string): Input => {
return acc
}

return [...acc, { x, y }]
return [...acc, new Vec2({ x, y })]
}, [])
})

Expand Down
39 changes: 13 additions & 26 deletions 2024/d12/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from 'node:fs'

import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'
import { Direction, Vec2 } from '@magiczne/advent-of-code-ts-core/math'
import { ObjectSet } from '@magiczne/advent-of-code-ts-core/structures'
import type { Vec2 } from '@magiczne/advent-of-code-ts-core/types'

type Garden = Array<Array<string>>

Expand All @@ -17,19 +17,9 @@ interface Wall {
direction: Vec2
}

const directions: ReadonlyArray<Vec2> = [
{ x: -1, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: -1 },
{ x: 0, y: 1 },
]

const neighbors = (pos: Vec2): ReadonlyArray<Vec2> => {
return directions.map(direction => {
return {
x: pos.x + direction.x,
y: pos.y + direction.y,
}
return Direction.cardnial.map(direction => {
return direction.add(pos)
})
}

Expand All @@ -49,11 +39,8 @@ const floodFill = (garden: Garden, position: Vec2) => {
if (garden[neighbor.y]?.[neighbor.x] !== crop && !visited.has(neighbor)) {
wallSegments.push({
point: pos,
direction: {
// If I had direction here... but i have only neighbors xD
x: neighbor.x - pos.x,
y: neighbor.y - pos.y,
},
// If I had direction here... but i have only neighbors xD
direction: neighbor.sub(pos),
})
}

Expand Down Expand Up @@ -115,9 +102,9 @@ const floodFill = (garden: Garden, position: Vec2) => {
foundWall.end.y = Math.max(foundWall.end.y, wallSegment.point.y)
} else {
walls.push({
direction: { ...wallSegment.direction },
start: { ...wallSegment.point },
end: { ...wallSegment.point },
direction: wallSegment.direction.clone(),
start: wallSegment.point.clone(),
end: wallSegment.point.clone(),
})
}

Expand All @@ -142,9 +129,9 @@ const floodFill = (garden: Garden, position: Vec2) => {
foundWall.end.x = Math.max(foundWall.end.x, wallSegment.point.x)
} else {
walls.push({
direction: { ...wallSegment.direction },
start: { ...wallSegment.point },
end: { ...wallSegment.point },
direction: wallSegment.direction.clone(),
start: wallSegment.point.clone(),
end: wallSegment.point.clone(),
})
}

Expand All @@ -168,7 +155,7 @@ const part1 = (data: Garden): number => {
for (let y = 0; y < floodedGarden.length; y++) {
for (let x = 0; x < floodedGarden.length; x++) {
if (floodedGarden[y][x] !== '.') {
const floodedRegion = floodFill(floodedGarden, { x, y })
const floodedRegion = floodFill(floodedGarden, new Vec2({ x, y }))

price += floodedRegion.perimeter * floodedRegion.points.length
}
Expand All @@ -185,7 +172,7 @@ const part2 = (data: Garden): number => {
for (let y = 0; y < floodedGarden.length; y++) {
for (let x = 0; x < floodedGarden.length; x++) {
if (floodedGarden[y][x] !== '.') {
const floodedRegion = floodFill(floodedGarden, { x, y })
const floodedRegion = floodFill(floodedGarden, new Vec2({ x, y }))

price += floodedRegion.sides * floodedRegion.points.length
}
Expand Down
18 changes: 10 additions & 8 deletions 2024/d13/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs'
import { Context, init as initZ3, IntNum } from 'z3-solver'

import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc'
import type { Vec2 } from '@magiczne/advent-of-code-ts-core/types'
import { Vec2 } from '@magiczne/advent-of-code-ts-core/math'

interface Machine {
buttonA: Vec2
Expand All @@ -11,9 +11,11 @@ interface Machine {
}

const { Context } = await initZ3()

// @ts-expect-error Something is wrong with z3-solver
const ctx: Context = new Context('aoc')

const optimizeMachine = async (machine: Machine, prizeCoefficient): Promise<number> => {
const optimizeMachine = async (machine: Machine, prizeCoefficient = 0): Promise<number> => {
const solver = new ctx.Optimize()
const x = ctx.Int.const('x')
const y = ctx.Int.const('y')
Expand Down Expand Up @@ -73,18 +75,18 @@ const reader = (file: string): ReadonlyArray<Machine> => {
const lines = machine.split('\n').map(line => line.match(/(\d+).+?(\d+)/))

return {
buttonA: {
buttonA: new Vec2({
x: parseInt(lines[0][1], 10),
y: parseInt(lines[0][2], 10),
},
buttonB: {
}),
buttonB: new Vec2({
x: parseInt(lines[1][1], 10),
y: parseInt(lines[1][2], 10),
},
prize: {
}),
prize: new Vec2({
x: parseInt(lines[2][1], 10),
y: parseInt(lines[2][2], 10),
},
}),
}
})
}
Expand Down
Loading

0 comments on commit 268b21e

Please sign in to comment.