Skip to content

Commit 69c4b76

Browse files
author
Chris Stewart
committed
Day 5 Both Stars
1 parent 3eb99a1 commit 69c4b76

File tree

2 files changed

+381
-0
lines changed

2 files changed

+381
-0
lines changed

data/input5.txt

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
seeds: 304740406 53203352 1080760686 52608146 1670978447 367043978 1445830299 58442414 4012995194 104364808 4123691336 167638723 2284615844 178205532 3164519436 564398605 90744016 147784453 577905361 122056749
2+
3+
seed-to-soil map:
4+
0 699677807 922644641
5+
4174180469 3833727510 120786827
6+
1525682201 2566557266 229511566
7+
3280624601 3954514337 340452959
8+
2228029508 2796068832 310221139
9+
3621077560 3280624601 553102909
10+
2120836342 592484641 107193166
11+
1982514669 227320902 138321673
12+
1755193767 0 227320902
13+
922644641 1622322448 603037560
14+
2538250647 365642575 226842066
15+
2765092713 2225360008 341197258
16+
17+
soil-to-fertilizer map:
18+
1916776044 145070025 3464138
19+
1920240182 0 145070025
20+
706160141 2208005933 115191764
21+
2898492924 830275742 87027483
22+
3489083348 3344594558 103871907
23+
2985520407 148534163 415139950
24+
821351905 917303225 327392865
25+
1148744770 1517236949 182706102
26+
295069722 3448466465 411090419
27+
1816984891 3244803405 99791153
28+
4282585972 4292886644 2080652
29+
3592955255 563674113 266601629
30+
4266462972 4158154511 16123000
31+
1331450872 1244696090 272540859
32+
2715943131 3062253612 182549793
33+
4284666624 4174277511 10300672
34+
4158154511 4184578183 108308461
35+
1603991731 1995012773 212993160
36+
2065310207 2411620688 650632924
37+
0 1699943051 295069722
38+
3400660357 2323197697 88422991
39+
40+
fertilizer-to-water map:
41+
3585244197 3493316345 482900943
42+
2871272496 878061687 456215665
43+
3477664135 4187387234 107580062
44+
845559238 15587711 56716031
45+
121711204 2918313406 409174755
46+
1639718746 0 15587711
47+
530885959 2603640127 314673279
48+
902275269 2435903232 167736895
49+
2635221133 72303742 236051363
50+
1070012164 308355105 569706582
51+
1699846244 1334277352 935374889
52+
4279315086 3477664135 15652210
53+
1655306457 2269652241 44539787
54+
109056711 2423248739 12654493
55+
0 2314192028 109056711
56+
4068145140 3976217288 211169946
57+
58+
water-to-light map:
59+
3841742547 3016842841 17384315
60+
2875021919 2637593760 185450069
61+
3413635232 3588265685 87508205
62+
1311241677 236307150 54007684
63+
3349161906 4276682782 18284514
64+
896790030 1355845673 34430118
65+
3060471988 3835573209 145836645
66+
2741184131 3675773890 133837788
67+
1387754847 947687177 15489861
68+
3785944618 2057196631 55797929
69+
2006585491 2931426646 85416195
70+
3873217816 3809611678 25961531
71+
1667765627 643929130 34884144
72+
2092001686 2434956599 202637161
73+
1001898651 158618769 77688381
74+
3899179347 2253048950 181907649
75+
1786416461 377140410 101956748
76+
0 833901414 113785763
77+
1403244708 479097158 56815029
78+
3859126862 3034227156 14090954
79+
747996464 678813274 31450438
80+
869173795 963177038 27616235
81+
3268502638 2006585491 50611140
82+
113785763 0 148879571
83+
262665334 1511505797 386606610
84+
1187603975 710263712 123637702
85+
3319113778 3987361499 30048128
86+
3367446420 2885237834 46188812
87+
931220148 990793273 15913032
88+
1460059737 1006706305 120880314
89+
1079587032 535912187 108016943
90+
3645890228 2112994560 140054390
91+
3206308633 2823043829 62194005
92+
1888373209 148879571 9739198
93+
3501143437 3443518894 144746791
94+
779446902 1127586619 89726893
95+
947133180 1217313512 54765471
96+
2481910976 4017409627 259273155
97+
1365249361 1390275791 22505486
98+
4087038641 3048318110 207928655
99+
1702649771 1272078983 83766690
100+
649271944 1412781277 98724520
101+
2294638847 3256246765 187272129
102+
4081086996 3981409854 5951645
103+
1580940051 290314834 86825576
104+
105+
light-to-temperature map:
106+
2659452899 3773423191 23529065
107+
1010417677 1830019321 229964714
108+
1506263997 1764304095 65715226
109+
3017023682 3993999178 103632805
110+
3758361154 3931294907 62704271
111+
2513441862 2529586713 106552791
112+
3821065425 3163657189 7959671
113+
3410504451 3191697730 271334719
114+
2500616406 3150831733 12825456
115+
2065874786 2636139504 257698620
116+
4142272690 2382216135 108163002
117+
1377732678 1378901025 61208694
118+
91217027 248578952 8927711
119+
2463617376 3879075083 36999030
120+
3982807123 2315058258 67157877
121+
2323573406 2065874786 97274446
122+
958870382 916323074 51547295
123+
3868386197 3579887474 114420926
124+
931392999 1351423642 27477383
125+
2942753127 3694308400 74270555
126+
1812734437 168620508 79958444
127+
3301364949 2163149232 3197696
128+
2420847852 2166346928 42769524
129+
3829025096 3111470632 39361101
130+
2619994653 2490379137 39207576
131+
1571979223 1523548881 240755214
132+
2927532333 3916074113 15220794
133+
3125500723 4097631983 175864226
134+
1438941372 10080856 67322625
135+
2049903179 0 10080856
136+
3304562645 2209116452 105941806
137+
1976132043 1277652506 73771136
138+
2659202229 3171616860 250670
139+
4256036535 3463032449 38930761
140+
1240382391 257506663 137350287
141+
0 77403481 91217027
142+
3120656487 3768578955 4844236
143+
100144738 967870369 309782137
144+
409926875 394856950 521466124
145+
2682981964 4273496209 21471087
146+
2704453051 3501963210 77924264
147+
2802207515 2893838124 125324818
148+
3681839170 3796952256 76521984
149+
4250435692 3873474240 5600843
150+
1892692881 1440109719 83439162
151+
4049965000 3019162942 92307690
152+
2782377315 3171867530 19830200
153+
154+
temperature-to-humidity map:
155+
1281293605 2434144353 57731817
156+
3534843655 3623804479 36539813
157+
1516028925 367078655 499627624
158+
3340374639 3427302148 25514722
159+
1176213912 2491876170 105079693
160+
3872645852 3827818849 188531931
161+
508302359 1375008638 300832898
162+
0 866706279 508302359
163+
4146417618 3475254801 148549678
164+
4083438506 3660344292 62979112
165+
3365889361 3745584127 82234722
166+
4061177783 3723323404 22260723
167+
2015656549 1675841536 348405327
168+
1056134836 246999579 120079076
169+
3448124083 3452816870 22437931
170+
3321587434 3408514943 18787205
171+
3470562014 4016350780 64281641
172+
3571383468 3321587434 86927509
173+
1339025422 2024246863 177003503
174+
809135257 0 246999579
175+
2364061876 2596955863 115651453
176+
3658310977 4080632421 214334875
177+
2479713329 2201250366 232893987
178+
179+
humidity-to-location map:
180+
2408792839 708984436 12070437
181+
3916327360 4103567762 90492800
182+
2136669394 2902458135 226099404
183+
1414655297 721054873 722014097
184+
2462136308 3514619416 2467233
185+
1254861475 3327498132 98562162
186+
2362768798 569836962 46024041
187+
4185175199 3916327360 8885363
188+
421054090 234463197 201173738
189+
2497827912 1550759989 35404865
190+
849065671 0 224309687
191+
37059832 615861003 93123433
192+
4006820160 3925212723 178355039
193+
2863253575 1705311678 653833074
194+
622227828 1443068970 107691019
195+
2464603541 2869233764 33224371
196+
1353423637 3128557539 61231660
197+
1073375358 3426060294 47286090
198+
2725544642 3189789199 137708933
199+
26906322 224309687 10153510
200+
2420863276 3473346384 41273032
201+
729918847 1586164854 119146824
202+
2533232777 2676921899 192311865
203+
0 2359144752 26906322
204+
1120661448 435636935 134200027
205+
130183265 2386051074 290870825

src/dc/aoc2023/Day5.cls

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
Class doc.aoc2023.Day5 Extends doc.aoc2023.Base
2+
{
3+
4+
Parameter InputFile = "input5.txt";
5+
6+
ClassMethod ParseSeeds(line)
7+
{
8+
for i = 1:1:$L(line," "){
9+
set ^seeds($P(line," ",i))=""
10+
}
11+
}
12+
13+
ClassMethod ParseSeeds2(line)
14+
{
15+
For i = 1:2:$LENGTH(line," "){
16+
Set ^seeds($PIECE(line," ",i),$PIECE(line," ",i+1))=""
17+
}
18+
}
19+
20+
ClassMethod storeNumber(line, mode)
21+
{
22+
set source = $P(line," ",2)
23+
set dest = $P(line," ",1)
24+
set ticks = $P(line," ",3)
25+
w !,"source: "_source_" dest: "_dest_" ticks: "_ticks
26+
27+
set @mode@(source,source+ticks,dest)=""
28+
}
29+
30+
ClassMethod storeNumber2(line, mode)
31+
{
32+
Set source = $PIECE(line," ",2)
33+
Set dest = $PIECE(line," ",1)
34+
Set ticks = $PIECE(line," ",3)
35+
Write !,"source: "_source_" dest: "_dest_" ticks: "_ticks
36+
37+
Set @mode@(dest,(dest+(ticks-1)),source)=""
38+
}
39+
40+
ClassMethod checkMapping(number, mode)
41+
{
42+
if $DATA(@mode@(number)){
43+
set mappingkey=number
44+
}else{
45+
Set mappingkey = $ORDER(@mode@(number),-1)
46+
}
47+
Set limitkey = $ORDER(@mode@(mappingkey,""))
48+
49+
If number<=limitkey {
50+
51+
Set destvalue=$ORDER(@mode@(mappingkey,limitkey,""))
52+
Set offset = number-mappingkey
53+
54+
Set number = destvalue+(offset) //set the value by the offset
55+
56+
}
57+
58+
Quit number
59+
}
60+
61+
ClassMethod Run() As %Integer
62+
{
63+
k locations
64+
Set stream=..GetInput(..#InputFile)
65+
Set min=0
66+
67+
//Setup all reference globals
68+
While 'stream.AtEnd {
69+
Set line=stream.ReadLine()
70+
Set line =$ZSTRIP(line,"=W") //get rid of repeat whitespaces
71+
if $P(line," ",1)="seeds:" d ..ParseSeeds($ZSTRIP($P(line,":",2),"<>W")) //Pass in just the values
72+
If ($PIECE(line," ",1))="seed-to-soil" {Set mode = "^seed2soil" K @mode} //Initialise each PPG
73+
If ($PIECE(line," ",1))="soil-to-fertilizer" {Set mode = "^soil2fert" Kill @mode}
74+
If ($PIECE(line," ",1))="fertilizer-to-water" {Set mode = "^fert2water" Kill @mode}
75+
If ($PIECE(line," ",1))="water-to-light" {Set mode = "^water2light" Kill @mode}
76+
If ($PIECE(line," ",1))="light-to-temperature" {Set mode = "^light2temp" Kill @mode}
77+
If ($PIECE(line," ",1))="temperature-to-humidity" {Set mode = "^temp2humid" Kill @mode}
78+
If ($PIECE(line," ",1))="humidity-to-location" {Set mode = "^humid2loc" Kill @mode}
79+
if $E(line)=+$E(line) d ..storeNumber(line,mode) //If we're a number then store it
80+
}
81+
82+
//Parse out seeds
83+
set count = 0
84+
set seedvalue=""
85+
for {
86+
s seedvalue = $ORDER(^seeds(seedvalue))
87+
do $I(count)
88+
q:seedvalue=""
89+
w !,"Seed "_count_" is "_seedvalue
90+
set soilvalue = ..checkMapping(seedvalue,"^seed2soil")
91+
Set fertvalue = ..checkMapping(soilvalue,"^soil2fert")
92+
Set watervalue = ..checkMapping(fertvalue,"^fert2water")
93+
Set lightvalue = ..checkMapping(watervalue,"^water2light")
94+
Set tempvalue = ..checkMapping(lightvalue,"^light2temp")
95+
Set humidvalue = ..checkMapping(tempvalue,"^temp2humid")
96+
Set locvalue = ..checkMapping(humidvalue,"^humid2loc")
97+
set locations(locvalue)=seedvalue
98+
}
99+
100+
Write !,"returning results: "_$ORDER(locations("")),!
101+
102+
103+
Return $O(locations(""))
104+
}
105+
106+
ClassMethod Run2() As %Integer
107+
{
108+
109+
//k ^running
110+
Set stream=..GetInput(..#InputFile)
111+
Set min=0
112+
K ^seeds
113+
//Setup all reference globals
114+
While 'stream.AtEnd {
115+
Set line=stream.ReadLine()
116+
Set line =$ZSTRIP(line,"=W") //get rid of repeat whitespaces
117+
If $PIECE(line," ",1)="seeds:" Do ..ParseSeeds2($ZSTRIP($PIECE(line,":",2),"<>W")) //Pass in just the values
118+
If ($PIECE(line," ",1))="seed-to-soil" {Set mode = "^soil2seed" Kill @mode} //Initialise each PPG
119+
If ($PIECE(line," ",1))="soil-to-fertilizer" {Set mode = "^fert2soil" Kill @mode}
120+
If ($PIECE(line," ",1))="fertilizer-to-water" {Set mode = "^water2fert" Kill @mode}
121+
If ($PIECE(line," ",1))="water-to-light" {Set mode = "^light2water" Kill @mode}
122+
If ($PIECE(line," ",1))="light-to-temperature" {Set mode = "^temp2light" Kill @mode}
123+
If ($PIECE(line," ",1))="temperature-to-humidity" {Set mode = "^humid2temp" Kill @mode}
124+
If ($PIECE(line," ",1))="humidity-to-location" {Set mode = "^loc2humid" Kill @mode}
125+
If $EXTRACT(line)=+$EXTRACT(line) Do ..storeNumber2(line,mode) //If we're a number then store it
126+
}
127+
128+
129+
//Parse out seeds
130+
Set startvalue=""
131+
set count=0
132+
For {
133+
d $INCREMENT(count)
134+
//Set startvalue = $ORDER(^seeds(startvalue))
135+
//Quit:startvalue=""
136+
//Set count = 0
137+
w !,"jobbing "_count
138+
q:..checkBack(count)
139+
140+
141+
}
142+
143+
144+
quit ^locations
145+
}
146+
147+
ClassMethod checkBack(locvalue) As %Boolean
148+
{
149+
150+
151+
//Set ^running(startvalue,"status")="Seed "_startvalue_" is "_count_" of "_$ORDER(^seeds(startvalue,""))
152+
Set humidvalue = ..checkMapping(locvalue,"^loc2humid")
153+
Set tempvalue = ..checkMapping(humidvalue,"^humid2temp")
154+
Set lightvalue = ..checkMapping(tempvalue,"^temp2light")
155+
Set watervalue = ..checkMapping(lightvalue,"^light2water")
156+
Set fertvalue = ..checkMapping(watervalue,"^water2fert")
157+
Set soilvalue = ..checkMapping(fertvalue,"^fert2soil")
158+
Set seedvalue = ..checkMapping(soilvalue,"^soil2seed")
159+
160+
w !,"seed value is "_seedvalue
161+
set seedbucket = $O(^seeds(seedvalue),-1)
162+
if seedbucket=""{
163+
set rangelow=0
164+
set rangehigh=0
165+
} else {
166+
set rangelow = seedbucket
167+
set rangehigh = seedbucket+($O(^seeds(seedbucket,"")))
168+
}
169+
170+
if ((seedvalue>=rangelow)&&(seedvalue<=rangehigh)) w !,"This was an ORIGINAL"
171+
172+
Set:((seedvalue>=rangelow)&&(seedvalue<=rangehigh)) ^locations=locvalue
173+
quit (seedvalue>=rangelow)&&(seedvalue<=rangehigh)
174+
}
175+
176+
}

0 commit comments

Comments
 (0)