Skip to content

Commit 0e22a23

Browse files
day 16 (not finished)
1 parent 96bf5c9 commit 0e22a23

File tree

5 files changed

+219
-26
lines changed

5 files changed

+219
-26
lines changed

package-lock.json

Lines changed: 141 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@prefecthq/eslint-config": "^1.0.20",
13-
"@types/node": "^16.11.12"
13+
"@types/node": "^16.11.12",
14+
"ts-node": "^10.9.1"
1415
}
1516
}

src/17/01.ts renamed to src/16/01.ts

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { demo } from '@/17/input'
1+
import { input } from '@/16/input'
22

33
const valves = new Map<string, Valve>()
44

55
class Valve {
66
public id: string
77
public flowRate: number
8-
public minutesOpen: number
8+
public minutesOpen: number | null
99
private readonly connectionIds: string[]
1010

1111
public constructor(input: string) {
@@ -41,7 +41,7 @@ class Valve {
4141
}
4242

4343
public get totalFlowRate(): number {
44-
return this.minutesOpen * this.flowRate
44+
return (this.minutesOpen ?? 0) * this.flowRate
4545
}
4646
}
4747

@@ -91,40 +91,89 @@ function buildDistancesMap(): Map<string, Map<string, number>> {
9191
return distancesMap
9292
}
9393

94-
function getAllPermutations(ids: string[], minutesRemaining: number): string[][] {
94+
function getValveSequences(ids: string[], minutesRemaining: number): string[][] {
9595
if (!ids.length || minutesRemaining <= 0) {
9696
return [[]]
9797
}
9898

99-
function openValve(valve: Valve): void {
100-
minutesRemaining--
101-
valve.minutesOpen = minutesRemaining
102-
}
103-
10499
return ids.flatMap(id => {
105-
const distances = distancesMap.get(id)
106-
const remaining = ids.filter(x => x !== id)
107-
const [nextId] = remaining
108-
const nextValve = valves.get(nextId)
109-
110-
if (nextValve && nextValve.flowRate > 0) {
111-
openValve(nextValve)
100+
const distancesFromValve = distancesMap.get(id)
101+
const remainingValves = ids.filter(x => x !== id)
102+
const valve = valves.get(id)
103+
const [nextId] = remainingValves
104+
105+
if (valve && valve.flowRate > 0) {
106+
const timeToOpenValve = 1
107+
minutesRemaining -= timeToOpenValve
112108
}
113109

114-
const distanceToNextValve = distances?.get(nextId) ?? 0
110+
const distanceToNextValve = distancesFromValve?.get(nextId) ?? 0
115111

116-
return getAllPermutations(remaining, minutesRemaining - distanceToNextValve)
112+
return getValveSequences(remainingValves, minutesRemaining - distanceToNextValve)
117113
.map(x => [id, ...x])
118114
})
119115
}
120116

121-
function solve(): void {
122-
const valveIds = Array.from(valves.values()).map(valve => valve.id)
123-
const allPermutations = getAllPermutations(valveIds, 30)
117+
function resetValves(): void {
118+
for (const [, valve] of valves) {
119+
valve.minutesOpen = null
120+
}
121+
}
124122

125-
console.log(allPermutations.length)
123+
function calculateFlow(sequence: string[]): number {
124+
resetValves()
125+
setValvesOpen(sequence)
126+
127+
return sequence.reduce((sum, id) => sum += valves.get(id)!.totalFlowRate, 0)
126128
}
127129

128-
demo.split('\n').map(input => new Valve(input))
130+
function setValvesOpen(sequence: string[]): void {
131+
let current = valves.get('AA')!
132+
let minutesRemaining = 30
133+
134+
for (const id of sequence) {
135+
const distancesFromValve = distancesMap.get(current.id)!
136+
const distanceToNextValve = distancesFromValve.get(id)!
137+
if (minutesRemaining - 1 <= distanceToNextValve) {
138+
return
139+
}
140+
minutesRemaining -= distanceToNextValve
141+
142+
const valve = valves.get(id)!
143+
144+
if (valve.flowRate > 0) {
145+
minutesRemaining--
146+
valve.minutesOpen = minutesRemaining
147+
}
148+
149+
if (minutesRemaining <= 0) {
150+
return
151+
}
152+
153+
current = valve
154+
}
155+
}
156+
157+
function solve(): void {
158+
const valveIds = Array.from(valves.values())
159+
.filter(valve => valve.flowRate > 0)
160+
.map(valve => valve.id)
161+
162+
const sequences = getValveSequences(valveIds, 30)
163+
let maxFlowRate = 0
164+
for (const sequence of sequences) {
165+
const result = calculateFlow(sequence)
166+
167+
if (result > maxFlowRate) {
168+
maxFlowRate = result
169+
console.log(maxFlowRate, sequence)
170+
}
171+
}
172+
}
173+
174+
input.split('\n').map(input => new Valve(input))
129175
const distancesMap = buildDistancesMap()
130-
solve()
176+
solve()
177+
178+
// 1251 too low
179+
// 1601 too low
File renamed without changes.

src/17/input.ts renamed to src/16/input.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export const input = `
1515
Valve TN has flow rate=0; tunnels lead to valves IX, ZZ
1616
Valve DS has flow rate=0; tunnels lead to valves IF, OU
1717
Valve OP has flow rate=0; tunnels lead to valves UH, ZQ
18+
Valve FS has flow rate=0; tunnels leValve TN has flow rate=0; tunnels lead to valves IX, ZZ
19+
Valve DS has flow rate=0; tunnels lead to valves IF, OU
20+
Valve OP has flow rate=0; tunnels lead to valves UH, ZQ
1821
Valve FS has flow rate=0; tunnels lead to valves IF, UH
1922
Valve WO has flow rate=0; tunnels lead to valves IS, RW
2023
Valve KQ has flow rate=0; tunnels lead to valves SI, WZ

0 commit comments

Comments
 (0)