Readme

https://adventofcode.com/2023/day/8

the script times out on val.town, so I ran it with deno on my machine for some mintes.

By running

deno run https://esm.town/v/karfau/aoc23_08 | grep z

I observed a repeating pattern in the output...

each "track" was reaching the same position over and over again, but not on the same lines.

I dumped the beginning of the output into a text file, opened it to look at the patterns and confirmed that each "track" had a fixed frequency for each position ending on a Z.

so I took the "step"(+1 because it's zero based) and checked the primes each of them contains, to get the correct input. I calculated that with a calculator and copied in the number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { expect } from "https://esm.town/v/karfau/chai";
expect(
firstStar(`RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)`),
"*1 sample 1",
).to.equal(2);
expect(
firstStar(`LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)`),
"*1 sample 2",
).to.equal(6);
function firstStar(input: string) {
const [directions, ...mapRaw] = input.split("\n").filter((line) => Boolean(line.trim()));
const map = Object.fromEntries(mapRaw.map(line => {
const [key, L, R] = line.match(/[A-Z]{3}/g);
return [key, { L, R }];
}));
// debug(map, "map");
let step = 0, position = "AAA";
while (position !== "ZZZ") {
const dir = directions[step % directions.length];
position = map[position][dir];
if (!dir || !position) {
return -1;
}
step++;
}
return step;
}
// as as soon as the assertions pass, the solution is calculated
console.log("solution *1:", firstStar(input()));
expect(
secondStar(`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)`),
"*2 sample 1",
).to.equal(6);
// expect(secondStar(``), "*2 sample 2").to.equal("?");
function secondStar(input: string) {
const [directions, ...mapRaw] = input.split("\n").filter((line) => Boolean(line.trim()));
const map = Object.fromEntries(mapRaw.map(line => {
const [key, L, R] = line.match(/[0-9A-Z]{3}/g);
return [key, { L, R }];
}));
debug(map, "map");
let step = 0, positions = debug(Object.keys(map).filter(key => key.endsWith("A"))), reached = 6;
const notWithZ = pos => !pos.endsWith("Z");
while (positions.some(notWithZ)) {
const dir = directions[step % directions.length];
const id = `${positions.join(",")}->${dir}`;
positions = positions.map(position => map[position][dir]);
const left = positions.filter(notWithZ).length;
left <= reached && debug(positions, `${step} ${dir} ${left}`);
reached = Math.min(reached, left);
if (!dir) {
return -step;
}
step++;
}
return step;
}
console.log("solution *2:", secondStar(input()));
function input() {
return `LLLRRLRRRLLRRLRRLLRLRRLRRRLRRLRRLRRRLRLRRLRLRRLRRLLRRLRLLRRLLLRRRLRRLRLRLRRRLRLLRRLRRRLRRLRRLRRLRLLRLLRRLRRRLRRLRLRRLRRRLRRLLRLLRRLRRRLLRRRLRLRRRLLRLRRLRRLLRRLRRLLLRRRLRLRRRLRRLLRLRRLRLLRRRLRLRLLRLRRRLRLRRRLRRLRLRLLRLRRRLRRLRRRLRRRLRLRRRLRRRLLLLR
FSH = (CGN, NDK)
LQT = (NSK, XBG)
LCP = (QQB, NTB)
DFG = (KTV, NJR)
MCC = (TRF, NHH)
PHG = (VMX, SHB)
SMP = (MKD, TBS)
MRT = (NJX, HHJ)
LNG = (KRF, VDK)
TKG = (VLM, XFQ)
QCR = (BDL, VKQ)
PQC = (CXK, HKS)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
December 8, 2023