Skip to content

Commit a0d4d0c

Browse files
author
Chris Stewart
committed
Day 8 Both Stars
1 parent 28f3f14 commit a0d4d0c

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

src/dc/aoc2023/Day8.cls

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Class doc.aoc2023.Day8 Extends doc.aoc2023.Base
2+
{
3+
4+
Parameter InputFile = "input8.txt";
5+
6+
ClassMethod Run() As %Integer
7+
{
8+
Kill ^map
9+
10+
Set stream=..GetInput(..#InputFile)
11+
12+
Set count=0
13+
Set instructions=stream.ReadLine() //store the path here
14+
set ^maps = instructions
15+
Set null=stream.ReadLine() //There's a blank line, it's hacky but just skip over it
16+
//Setup all reference globals
17+
While 'stream.AtEnd {
18+
19+
Set line=stream.ReadLine()
20+
set node = $P(line," ",1) //first string is node name
21+
//cheating ahed, the keys are fixed width, so will just $E them out
22+
//this would need to be changed if the keys were variable length
23+
//or just a different length.
24+
set left = $E(line,8,10)
25+
set right = $E(line,13,15)
26+
w !,"Node "_node_" LEFT: "_left_" RIGHT: "_right
27+
set ^maps(node,"L")=left
28+
set ^maps(node,"R")=right
29+
30+
}
31+
32+
s rank = ""
33+
s count =0
34+
s accumulator = 0
35+
set currentnode = "AAA"
36+
for {
37+
38+
39+
s realcount = (count#($L(^maps)))+1
40+
s instruction = $E(^maps,realcount)
41+
s currentnode =^maps(currentnode,instruction)
42+
s count=$I(count)
43+
q:currentnode="ZZZ"
44+
w !, count,?10,currentnode,?20,instruction,?23,realcount
45+
46+
}
47+
48+
Write !,"returning results: "_count,!
49+
Quit count
50+
}
51+
52+
ClassMethod Run2() As %Integer
53+
{
54+
Kill ^ghostmaps
55+
kill ^ghostindex
56+
Kill ^foundnodes
57+
58+
Set stream=..GetInput(..#InputFile)
59+
60+
Set count=0
61+
Set instructions=stream.ReadLine() //store the path here
62+
set ^ghostmaps = instructions
63+
Set null=stream.ReadLine() //There's a blank line, it's hacky but just skip over it
64+
//Setup all reference globals
65+
While 'stream.AtEnd {
66+
67+
Set line=stream.ReadLine()
68+
set node = $P(line," ",1) //first string is node name
69+
if $E(node,3)="A" set ^ghostindex(node)="" //Get all starting points
70+
//cheating ahed, the keys are fixed width, so will just $E them out
71+
//this would need to be changed if the keys were variable length
72+
//or just a different length.
73+
set left = $E(line,8,10)
74+
set right = $E(line,13,15)
75+
w !,"Node "_node_" LEFT: "_left_" RIGHT: "_right
76+
set ^maps(node,"L")=left
77+
set ^maps(node,"R")=right
78+
79+
}
80+
81+
s rank = ""
82+
s count =0
83+
s accumulator = 0
84+
85+
set aKey = ""
86+
set aCount = 0
87+
for {
88+
set aKey=$O(^ghostindex(aKey))
89+
q:aKey=""
90+
d $INCREMENT(aCount)
91+
92+
}
93+
w !,"There are "_aCount_" nodes that start with A (or Z)"
94+
95+
set nodekey=""
96+
for {
97+
set count = 0
98+
set nodekey=$O(^ghostindex(nodekey))
99+
quit:nodekey=""
100+
w !,"nodekey "_nodekey
101+
set startnode=nodekey
102+
set currentnode=nodekey
103+
for {
104+
105+
s realcount = (count#($L(^maps)))+1
106+
s instruction = $E(^maps,realcount)
107+
s currentnode =^maps(currentnode,instruction)
108+
s count=$I(count)
109+
if $E(currentnode,3)="Z" {
110+
111+
s ^foundnodes(startnode)=count
112+
}
113+
q:$DATA(^foundnodes(startnode))
114+
115+
}
116+
}
117+
W !
118+
zw ^foundnodes
119+
120+
set foundkey=$O(^foundnodes(""))
121+
set gcd = ^foundnodes(foundkey)
122+
123+
for {
124+
set foundkey=$O(^foundnodes(foundkey))
125+
q:foundkey=""
126+
set oldgcd=gcd
127+
set gcd=..GreatestCommonDivisor(gcd,^foundnodes(foundkey))
128+
}
129+
w !,"GCD is "_gcd
130+
//compare smallest and biggest, and we should be ok?
131+
set largekey=$O(^foundnodes("")) //prime the first one
132+
set largenumber=^foundnodes(largekey)
133+
set lcm = largenumber
134+
for {
135+
set largekey=$O(^foundnodes(largekey))
136+
quit:largekey=""
137+
set largestnumber = ^foundnodes(largekey)
138+
w !,"Comparing "_lcm_" and "_largestnumber
139+
set lcm = (lcm*largestnumber)/..GreatestCommonDivisor(lcm,largestnumber)
140+
}
141+
w !,"LCM is "_lcm
142+
143+
Write !,"returning results: "_count,!
144+
Quit count
145+
}
146+
147+
ClassMethod GreatestCommonDivisor(a, b) As %Integer
148+
{
149+
150+
for {
151+
if a>b {set a = a-b}
152+
else {set b = b-a}
153+
quit:a=b
154+
}
155+
quit a
156+
}
157+
158+
}

0 commit comments

Comments
 (0)