day 16 working part 1

This commit is contained in:
Joshua Seigler 2024-12-16 23:31:59 -05:00
parent 223327735b
commit 1827f39dcb
3 changed files with 90 additions and 29 deletions

View file

@ -237,9 +237,11 @@
},
{
"part1": {
"solved": false,
"solved": true,
"result": null,
"attempts": [],
"attempts": [
"134596"
],
"time": null
},
"part2": {

View file

@ -26,7 +26,7 @@
[![Day](https://badgen.net/badge/13/%E2%98%85%E2%98%85/green)](src/day13)
[![Day](https://badgen.net/badge/14/%E2%98%85%E2%98%86/yellow)](src/day14)
[![Day](https://badgen.net/badge/15/%E2%98%85%E2%98%85/green)](src/day15)
![Day](https://badgen.net/badge/16/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/16/%E2%98%85%E2%98%86/yellow)](src/day16)
![Day](https://badgen.net/badge/17/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/18/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/19/%E2%98%86%E2%98%86/gray)
@ -167,16 +167,16 @@ Both parts: 1.492ms
```
Day 15
Time part 1: -
Time part 2: 7.672ms
Both parts: 7.672ms
Time part 1: 36404.189ms
Time part 2: 75398.809ms
Both parts: 111802.997ms
```
```
Day 16
Time part 1: -
Time part 1: 88.714ms
Time part 2: -
Both parts: -
Both parts: 88.714ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 29/50
Total time: 38913.342ms
Total stars: 30/50
Total time: 150797.381ms
```
<!--/RESULTS-->

View file

@ -3,8 +3,6 @@ import Heap from 'heap'
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] = [
[ 0, 1], // right
[ 1, 0], // down
@ -15,7 +13,7 @@ const deltas: [Coord, Coord, Coord, Coord] = [
type Coord = [number, number]
const bold = (text: string) => {
return `\x1b[44;37m${text}\x1b[0m`
return `\x1b[44;37;1m${text}\x1b[0m`
}
type Step = {
@ -40,16 +38,17 @@ const part1 = (rawInput: string) => {
cost: 0,
prev: null
})
const pqDict: Record<string, Step> = {}
while (pq.size() > 0) {
const prev = pq.pop()
const { r, c, dir, cost } = prev
const here = get(r, c)
if (here === 'E') {
// draw it
let cur = prev.prev
let cur = prev
const visited = new Map<string, number>()
while (cur.prev !== null) {
visited.set(`${cur.r},${cur.c}`, cur.dir)
while (cur.prev.prev !== null) {
visited.set(`${cur.prev.r},${cur.prev.c}`, cur.dir)
cur = cur.prev
}
console.log(
@ -67,19 +66,25 @@ const part1 = (rawInput: string) => {
})
.join("\n"),
)
// done drawing
return cost
}
for (let i = 0; i < (here === "S" ? 4 : 3); i += 1) {
const newDir = (dir + 3 + i) % 4 // left, ahead, right, u-turn
const [dr, dc] = deltas[newDir]
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,
c: c + dc,
dir: newDir,
cost: cost + addedCost,
prev,
})
}
pqDict[newKey] = newStep
pq.push(newStep)
}
}
}
@ -91,7 +96,50 @@ const part2 = (rawInput: string) => {
}
run({
part1: {
// part1: {
// tests: [
// {
// input: `###############
// #.......#....E#
// #.#.###.#.###.#
// #.....#.#...#.#
// #.###.#####.#.#
// #.#.#.......#.#
// #.#.#####.###.#
// #...........#.#
// ###.#.#####.#.#
// #...#.....#.#.#
// #.#.#.###.#.#.#
// #.....#...#.#.#
// #.###.#.#.#.#.#
// #S..#.....#...#
// ###############`,
// expected: 7036,
// },
// {
// input: `#################
// #...#...#...#..E#
// #.#.#.#.#.#.#.#.#
// #.#.#.#...#...#.#
// #.#.#.#.###.#.#.#
// #...#.#.#.....#.#
// #.#.#.#.#.#####.#
// #.#...#.#.#.....#
// #.#.#####.#.###.#
// #.#.#.......#...#
// #.#.###.#####.###
// #.#.#...#.....#.#
// #.#.#.#####.###.#
// #.#.#.........#.#
// #.#.#.#########.#
// #S#.............#
// #################`,
// expected: 11048
// }
// ],
// solution: part1,
// },
part2: {
tests: [
{
input: `###############
@ -109,17 +157,28 @@ run({
#.###.#.#.#.#.#
#S..#.....#...#
###############`,
expected: 7036,
expected: 45,
},
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
{
input: `#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################`,
expected: 64
}
],
solution: part2,
},