mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 00:56:10 +00:00
day 16 working part 1
This commit is contained in:
parent
223327735b
commit
1827f39dcb
3 changed files with 90 additions and 29 deletions
|
@ -237,9 +237,11 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": null,
|
||||||
"attempts": [],
|
"attempts": [
|
||||||
|
"134596"
|
||||||
|
],
|
||||||
"time": null
|
"time": null
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
|
|
16
README.md
16
README.md
|
@ -26,7 +26,7 @@
|
||||||
[](src/day13)
|
[](src/day13)
|
||||||
[](src/day14)
|
[](src/day14)
|
||||||
[](src/day15)
|
[](src/day15)
|
||||||

|
[](src/day16)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -167,16 +167,16 @@ Both parts: 1.492ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 15
|
Day 15
|
||||||
Time part 1: -
|
Time part 1: 36404.189ms
|
||||||
Time part 2: 7.672ms
|
Time part 2: 75398.809ms
|
||||||
Both parts: 7.672ms
|
Both parts: 111802.997ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 16
|
Day 16
|
||||||
Time part 1: -
|
Time part 1: 88.714ms
|
||||||
Time part 2: -
|
Time part 2: -
|
||||||
Both parts: -
|
Both parts: 88.714ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 29/50
|
Total stars: 30/50
|
||||||
Total time: 38913.342ms
|
Total time: 150797.381ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
|
@ -3,8 +3,6 @@ import Heap from 'heap'
|
||||||
|
|
||||||
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split(''))
|
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split(''))
|
||||||
|
|
||||||
type Tuple<TItem, TLength extends number> = [TItem, ...TItem[]] & { length: TLength }
|
|
||||||
|
|
||||||
const deltas: [Coord, Coord, Coord, Coord] = [
|
const deltas: [Coord, Coord, Coord, Coord] = [
|
||||||
[ 0, 1], // right
|
[ 0, 1], // right
|
||||||
[ 1, 0], // down
|
[ 1, 0], // down
|
||||||
|
@ -15,7 +13,7 @@ const deltas: [Coord, Coord, Coord, Coord] = [
|
||||||
type Coord = [number, number]
|
type Coord = [number, number]
|
||||||
|
|
||||||
const bold = (text: string) => {
|
const bold = (text: string) => {
|
||||||
return `\x1b[44;37m${text}\x1b[0m`
|
return `\x1b[44;37;1m${text}\x1b[0m`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Step = {
|
type Step = {
|
||||||
|
@ -40,16 +38,17 @@ const part1 = (rawInput: string) => {
|
||||||
cost: 0,
|
cost: 0,
|
||||||
prev: null
|
prev: null
|
||||||
})
|
})
|
||||||
|
const pqDict: Record<string, Step> = {}
|
||||||
while (pq.size() > 0) {
|
while (pq.size() > 0) {
|
||||||
const prev = pq.pop()
|
const prev = pq.pop()
|
||||||
const { r, c, dir, cost } = prev
|
const { r, c, dir, cost } = prev
|
||||||
const here = get(r, c)
|
const here = get(r, c)
|
||||||
if (here === 'E') {
|
if (here === 'E') {
|
||||||
// draw it
|
// draw it
|
||||||
let cur = prev.prev
|
let cur = prev
|
||||||
const visited = new Map<string, number>()
|
const visited = new Map<string, number>()
|
||||||
while (cur.prev !== null) {
|
while (cur.prev.prev !== null) {
|
||||||
visited.set(`${cur.r},${cur.c}`, cur.dir)
|
visited.set(`${cur.prev.r},${cur.prev.c}`, cur.dir)
|
||||||
cur = cur.prev
|
cur = cur.prev
|
||||||
}
|
}
|
||||||
console.log(
|
console.log(
|
||||||
|
@ -67,19 +66,25 @@ const part1 = (rawInput: string) => {
|
||||||
})
|
})
|
||||||
.join("\n"),
|
.join("\n"),
|
||||||
)
|
)
|
||||||
|
// done drawing
|
||||||
return cost
|
return cost
|
||||||
}
|
}
|
||||||
for (let i = 0; i < (here === "S" ? 4 : 3); i += 1) {
|
for (let i = 0; i < (here === "S" ? 4 : 3); i += 1) {
|
||||||
const newDir = (dir + 3 + i) % 4 // left, ahead, right, u-turn
|
const newDir = (dir + 3 + i) % 4 // left, ahead, right, u-turn
|
||||||
const [dr, dc] = deltas[newDir]
|
const [dr, dc] = deltas[newDir]
|
||||||
const addedCost = [1001, 1, 1001, 2001][i]
|
const addedCost = [1001, 1, 1001, 2001][i]
|
||||||
if (get(r + dr, c + dc) !== "#") pq.push({
|
if (get(r + dr, c + dc) === "#") continue
|
||||||
|
const newKey = `${r+dr},${c+dc},${newDir}`
|
||||||
|
if (pqDict[newKey] !== undefined && pqDict[newKey].cost < cost) continue
|
||||||
|
const newStep: Step = {
|
||||||
r: r + dr,
|
r: r + dr,
|
||||||
c: c + dc,
|
c: c + dc,
|
||||||
dir: newDir,
|
dir: newDir,
|
||||||
cost: cost + addedCost,
|
cost: cost + addedCost,
|
||||||
prev,
|
prev,
|
||||||
})
|
}
|
||||||
|
pqDict[newKey] = newStep
|
||||||
|
pq.push(newStep)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +96,50 @@ const part2 = (rawInput: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
run({
|
run({
|
||||||
part1: {
|
// part1: {
|
||||||
|
// tests: [
|
||||||
|
// {
|
||||||
|
// input: `###############
|
||||||
|
// #.......#....E#
|
||||||
|
// #.#.###.#.###.#
|
||||||
|
// #.....#.#...#.#
|
||||||
|
// #.###.#####.#.#
|
||||||
|
// #.#.#.......#.#
|
||||||
|
// #.#.#####.###.#
|
||||||
|
// #...........#.#
|
||||||
|
// ###.#.#####.#.#
|
||||||
|
// #...#.....#.#.#
|
||||||
|
// #.#.#.###.#.#.#
|
||||||
|
// #.....#...#.#.#
|
||||||
|
// #.###.#.#.#.#.#
|
||||||
|
// #S..#.....#...#
|
||||||
|
// ###############`,
|
||||||
|
// expected: 7036,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// input: `#################
|
||||||
|
// #...#...#...#..E#
|
||||||
|
// #.#.#.#.#.#.#.#.#
|
||||||
|
// #.#.#.#...#...#.#
|
||||||
|
// #.#.#.#.###.#.#.#
|
||||||
|
// #...#.#.#.....#.#
|
||||||
|
// #.#.#.#.#.#####.#
|
||||||
|
// #.#...#.#.#.....#
|
||||||
|
// #.#.#####.#.###.#
|
||||||
|
// #.#.#.......#...#
|
||||||
|
// #.#.###.#####.###
|
||||||
|
// #.#.#...#.....#.#
|
||||||
|
// #.#.#.#####.###.#
|
||||||
|
// #.#.#.........#.#
|
||||||
|
// #.#.#.#########.#
|
||||||
|
// #S#.............#
|
||||||
|
// #################`,
|
||||||
|
// expected: 11048
|
||||||
|
// }
|
||||||
|
// ],
|
||||||
|
// solution: part1,
|
||||||
|
// },
|
||||||
|
part2: {
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
input: `###############
|
input: `###############
|
||||||
|
@ -109,17 +157,28 @@ run({
|
||||||
#.###.#.#.#.#.#
|
#.###.#.#.#.#.#
|
||||||
#S..#.....#...#
|
#S..#.....#...#
|
||||||
###############`,
|
###############`,
|
||||||
expected: 7036,
|
expected: 45,
|
||||||
},
|
},
|
||||||
],
|
{
|
||||||
solution: part1,
|
input: `#################
|
||||||
},
|
#...#...#...#..E#
|
||||||
part2: {
|
#.#.#.#.#.#.#.#.#
|
||||||
tests: [
|
#.#.#.#...#...#.#
|
||||||
// {
|
#.#.#.#.###.#.#.#
|
||||||
// input: ``,
|
#...#.#.#.....#.#
|
||||||
// expected: "",
|
#.#.#.#.#.#####.#
|
||||||
// },
|
#.#...#.#.#.....#
|
||||||
|
#.#.#####.#.###.#
|
||||||
|
#.#.#.......#...#
|
||||||
|
#.#.###.#####.###
|
||||||
|
#.#.#...#.....#.#
|
||||||
|
#.#.#.#####.###.#
|
||||||
|
#.#.#.........#.#
|
||||||
|
#.#.#.#########.#
|
||||||
|
#S#.............#
|
||||||
|
#################`,
|
||||||
|
expected: 64
|
||||||
|
}
|
||||||
],
|
],
|
||||||
solution: part2,
|
solution: part2,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue