From 9470da2adb7e8834d0084133d6823a08d5c1bddc Mon Sep 17 00:00:00 2001 From: plasmaofthedawn Date: Fri, 15 Dec 2023 07:18:53 -0500 Subject: [PATCH] day 8 part 2 --- resources/day8dory.txt | 15 ++++ resources/day8sample.txt | 5 +- resources/day8sample2.txt | 20 +++++ src/day8/part2.pas | 184 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 resources/day8dory.txt create mode 100644 resources/day8sample2.txt create mode 100644 src/day8/part2.pas diff --git a/resources/day8dory.txt b/resources/day8dory.txt new file mode 100644 index 0000000..fce7d9e --- /dev/null +++ b/resources/day8dory.txt @@ -0,0 +1,15 @@ +LR + +mmA = (mmb, xxx) +mmb = (xxx, mmZ) +mmZ = (mmb, xxx) +nnA = (nnb, xxx) +nnb = (nnc, nnc) +nnc = (nnZ, nnZ) +nnZ = (nnb, nnb) +ooA = (ooZ, oob) +oob = (ood, ooc) +ooc = (ooA, oox) +ood = (ooZ, ood) +ooZ = (oob, ooZ) +xxx = (xxx, xxx) diff --git a/resources/day8sample.txt b/resources/day8sample.txt index 2f77262..d007857 100644 --- a/resources/day8sample.txt +++ b/resources/day8sample.txt @@ -1,5 +1,8 @@ -LLLRR +LLLR AAA = (AAA, BBB) BBB = (BBB, CCC) CCC = (CCC, ZZZ) +ZZZ = (AAA, AFM) + + diff --git a/resources/day8sample2.txt b/resources/day8sample2.txt new file mode 100644 index 0000000..54f7de4 --- /dev/null +++ b/resources/day8sample2.txt @@ -0,0 +1,20 @@ +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) +33A = (33Z, XXX) +33Z = (XXX, 33A) +44A = (44B, XXX) +44B = (XXX, 44C) +44C = (44Z, XXX) +44Z = (XXX, 44C) +55A = (55B, XXX) +55B = (XXX, 55Z) +55Z = (55C, XXX) +55C = (XXX, 55Z) \ No newline at end of file diff --git a/src/day8/part2.pas b/src/day8/part2.pas new file mode 100644 index 0000000..6a43ef8 --- /dev/null +++ b/src/day8/part2.pas @@ -0,0 +1,184 @@ +program day8part2; +uses sysutils; + + +var + file_: text; + + instructions: AnsiString; + str: string; + + count: int32; + + steps: int32; + step: char; + + i, j: int32; + + { node, left, right } + nodes: array[0..1000, 0..2] of string; + + prev_states: array[0..100000] of string; + prev_ics: array[0..100000] of int32; + prev_count: int32; + + b: array[0..1000] of int32; + l: array[0..1000] of int32; + a: array[0..1000, 0..1000] of int32; + a_count: array[0..1000] of int32; + + current_nodes: array[0..1000] of string; + current_nodes_count: int32; + + finished: boolean; + +begin + { open inp.txt for reading } + assign(file_, 'resources/day8.txt'); + reset(file_); + + { read instructions } + readln(file_, instructions); + + { read empty line } + readln(file_, str); + + count := 0; + + { read nodes } + while not EOF(file_) do + begin + + readln(file_, str); + + { start } + nodes[count, 0] := copy(str, 1, 3); + { left } + nodes[count, 1] := copy(str, 8, 3); + { right } + nodes[count, 2] := copy(str, 13, 3); + + count := count + 1; + + end; + + current_nodes_count := 0; + + { find all nodes that end with A } + for i := 0 to count - 1 do + begin + { if ends with A } + if nodes[i, 0][3] = 'A' then + begin + { adds it to current nodes } + current_nodes[current_nodes_count] := nodes[i, 0]; + current_nodes_count := current_nodes_count + 1; + end; + end; + + + + { for each node } + + for j := 0 to current_nodes_count - 1 do + begin + + steps := 0; + prev_count := 0; + finished := False; + + writeln(current_nodes[j]); + + while not finished do + begin + + { add state } + prev_states[prev_count] := current_nodes[j]; + prev_ics[prev_count] := (steps mod length(instructions)) + 1; + + { get step } + step := instructions[(steps mod length(instructions)) + 1]; + steps := steps + 1; + + { find node } + for i := 0 to count do + begin + if CompareText(nodes[i, 0], current_nodes[j]) = 0 then + break + end; + + { go to next node } + case step of + 'L': current_nodes[j] := nodes[i, 1]; + 'R': current_nodes[j] := nodes[i, 2]; + end; + + { check if next node is in previous state } + for i := 0 to prev_count do + begin + if (prev_states[i] = current_nodes[j]) and (prev_ics[i] = (steps mod length(instructions)) + 1) then + begin + finished := true; + break; + end; + end; + + prev_count := prev_count + 1; + + end; + + { offset to start of loop } + write('b = '); + b[j] := i - 1; + writeln(b[j]); + + { length of loop } + write('l = '); + l[j] := prev_count - i; + writeln(l[j]); + + { find offsets to the loop where it's finished } + a_count[j] := 0; + + for i := b[j] to prev_count - 1 do + begin + + { i'm sure this will cause no problems } + if i < 0 then + continue; + + { if this is a finished state } + if prev_states[i][3] = 'Z' then + begin + + { add it to the a list } + a[j, a_count[j]] := i - b[j]; + + write('a_'); + write(a_count[j]); + write(' = '); + writeln(a[j, a_count[j]]); + + a_count[j] := a_count[j] + 1; + + end; + end; + + // O( n * (l + b)) + // l is length of loop + // b is length of leadup + // n is number of nodes + + // O(a^n) + // a is number of finished states + + { + + i might come back to this to actually do this i'm tired rn + + } + + end; + +end. +