mirror of
https://github.com/seigler/aoc2024
synced 2025-07-25 08:16:09 +00:00
day 20
This commit is contained in:
parent
653e4a7b3e
commit
1c0830064d
5 changed files with 140 additions and 13 deletions
|
@ -300,16 +300,19 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "1327",
|
||||
"attempts": [
|
||||
"19031047",
|
||||
"1341"
|
||||
],
|
||||
"time": 396.636417
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "985737",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 409.185083
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -30,7 +30,7 @@
|
|||
[](src/day17)
|
||||
[](src/day18)
|
||||
[](src/day19)
|
||||

|
||||
[](src/day20)
|
||||

|
||||

|
||||

|
||||
|
@ -202,9 +202,9 @@ Both parts: 173.084ms
|
|||
|
||||
```
|
||||
Day 20
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 394.252ms
|
||||
Time part 2: 407.336ms
|
||||
Both parts: 801.588ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 38/50
|
||||
Total time: 176482.246ms
|
||||
Total stars: 40/50
|
||||
Total time: 177283.834ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day20/README.md
Normal file
9
src/day20/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 20 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/20)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
93
src/day20/index.ts
Normal file
93
src/day20/index.ts
Normal file
|
@ -0,0 +1,93 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const ints = (line: string) => [...line.matchAll(/\d+/g)].map(Number)
|
||||
const parseInput = (rawInput: string) =>
|
||||
rawInput.split("\n").map((line) => line.split(""))
|
||||
|
||||
const deltas: [Coord, Coord, Coord, Coord] = [
|
||||
[0, 1], // right
|
||||
[1, 0], // down
|
||||
[0, -1], // left
|
||||
[-1, 0], // up
|
||||
]
|
||||
|
||||
type Coord = [number, number]
|
||||
|
||||
function manhattan([ar, ac]: Coord, [br, bc]: Coord) {
|
||||
return Math.abs(ar - br) + Math.abs(ac - bc)
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string, mustSave = 100, maxCheat = 2) => {
|
||||
const input = parseInput(rawInput)
|
||||
let S: Coord
|
||||
let E: Coord
|
||||
input.forEach((row, r) => {
|
||||
row.forEach((cell, c) => {
|
||||
if (cell === "S") S = [r, c]
|
||||
if (cell === "E") E = [r, c]
|
||||
})
|
||||
})
|
||||
let [r, c] = S
|
||||
const path: Coord[] = [S]
|
||||
while (true) {
|
||||
const neighbors = deltas.map<Coord>(([dr, dc]) => [r + dr, c + dc])
|
||||
const next = neighbors.find(([nr, nc]) => {
|
||||
if (input[nr][nc] === "#") return false
|
||||
return path.find(([pr, pc]) => pr === nr && pc === nc) === undefined
|
||||
})
|
||||
if (next === undefined) break
|
||||
;[r, c] = next
|
||||
path.push(next)
|
||||
}
|
||||
let total = 0
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
for (let j = i + mustSave; j < path.length; j++) {
|
||||
const distance = manhattan(path[i], path[j])
|
||||
if (distance <= maxCheat && j - i - distance >= mustSave) {
|
||||
total++
|
||||
}
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
return part1(rawInput, 100, 20)
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
// {
|
||||
// input: ``,
|
||||
// expected: "",
|
||||
// },
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
// {
|
||||
// input: ``,
|
||||
// expected: "",
|
||||
// },
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
||||
|
||||
function bold(text: string) {
|
||||
return `\x1b[44;37;1m${text}\x1b[0m`
|
||||
}
|
||||
|
||||
function memoize<T extends unknown[], U>(
|
||||
fn: (...args: T) => U,
|
||||
): (...args: T) => U {
|
||||
const cache = {} // Saves the values
|
||||
return (...args) => {
|
||||
const key = args.join("\n")
|
||||
return cache[key] ?? (cache[key] = fn(...args))
|
||||
}
|
||||
}
|
|
@ -1,7 +1,17 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const ints = (line: string) => [...line.matchAll(/\d+/g)].map(Number)
|
||||
const parseInput = (rawInput: string) => rawInput
|
||||
|
||||
const deltas: [Coord, Coord, Coord, Coord] = [
|
||||
[ 0, 1], // right
|
||||
[ 1, 0], // down
|
||||
[ 0, -1], // left
|
||||
[-1, 0], // up
|
||||
]
|
||||
|
||||
type Coord = [number, number]
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
|
||||
|
@ -36,3 +46,15 @@ run({
|
|||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
||||
|
||||
function bold(text: string) {
|
||||
return `\x1b[44;37;1m${text}\x1b[0m`
|
||||
}
|
||||
|
||||
function memoize<T extends unknown[],U>(fn: (...args: T) => U): (...args: T) => U {
|
||||
const cache = {} // Saves the values
|
||||
return (...args) => {
|
||||
const key = args.join('\n')
|
||||
return cache[key] ?? (cache[key] = fn(...args))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue