add 8
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { open } from 'node:fs/promises';
|
||||
|
||||
|
||||
const file = await open('./inputs/8');
|
||||
let sum = 0
|
||||
let index = -1
|
||||
let path = ""
|
||||
|
||||
const nodes: { [key: string]: { L: string, R: string } } = {}
|
||||
|
||||
for await (const line of file.readLines()) {
|
||||
index++
|
||||
if (index === 0) {
|
||||
path = line
|
||||
continue
|
||||
}
|
||||
if (line.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const result = /^(?<node>\w+) = \((?<L>\w+), (?<R>\w+)\)/gms.exec(line)
|
||||
if (result === null) {
|
||||
throw new Error('wtf')
|
||||
}
|
||||
if (result.groups === undefined) {
|
||||
throw new Error('wtf')
|
||||
}
|
||||
|
||||
const node = result.groups['node']
|
||||
const L = result.groups['L']
|
||||
const R = result.groups['R']
|
||||
|
||||
nodes[node] = { L, R }
|
||||
}
|
||||
|
||||
let step = 0
|
||||
let current = 'AAA'
|
||||
while (current !== 'ZZZ') {
|
||||
let direction = path.at(step % path.length) as 'L' | 'R'
|
||||
current = nodes[current][direction]
|
||||
step++
|
||||
}
|
||||
console.log(path)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
console.log(step)
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { open } from 'node:fs/promises';
|
||||
|
||||
|
||||
const file = await open('./inputs/8');
|
||||
let index = -1
|
||||
let path = ""
|
||||
|
||||
const nodes: { [key: string]: { L: string, R: string } } = {}
|
||||
|
||||
for await (const line of file.readLines()) {
|
||||
index++
|
||||
if (index === 0) {
|
||||
path = line
|
||||
continue
|
||||
}
|
||||
if (line.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const result = /^(?<node>\w+) = \((?<L>\w+), (?<R>\w+)\)/gms.exec(line)
|
||||
if (result === null) {
|
||||
throw new Error('wtf')
|
||||
}
|
||||
if (result.groups === undefined) {
|
||||
throw new Error('wtf')
|
||||
}
|
||||
|
||||
const node = result.groups['node']
|
||||
const L = result.groups['L']
|
||||
const R = result.groups['R']
|
||||
|
||||
nodes[node] = { L, R }
|
||||
}
|
||||
|
||||
let currents = Object.keys(nodes).filter(e => e.endsWith('A'))
|
||||
let currentSteps = currents.map(n => {
|
||||
let step = 0
|
||||
while (!n.endsWith('Z')) {
|
||||
let direction = path.at(Number(step % path.length)) as 'L' | 'R'
|
||||
n = nodes[n][direction]
|
||||
step++
|
||||
}
|
||||
return step
|
||||
})
|
||||
|
||||
console.log(currentSteps)
|
||||
@@ -0,0 +1,10 @@
|
||||
LR
|
||||
|
||||
11A = (11B, XXX)
|
||||
11B = (XXX, 11Z)
|
||||
11Z = (11B, XXX)
|
||||
22A = (22B, XXX)
|
||||
22B = (22C, 22C)
|
||||
22C = (22Z, 22Z)
|
||||
22Z = (22B, 22B)
|
||||
XXX = (XXX, XXX)
|
||||
Reference in New Issue
Block a user