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": { "part1": {
"solved": false, "solved": true,
"result": null, "result": null,
"attempts": [], "attempts": [
"134596"
],
"time": null "time": null
}, },
"part2": { "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/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/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/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/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/18/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/19/%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 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-->

View file

@ -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,
}, },