Skip to content

Commit

Permalink
Day 6 Both Stars
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Stewart committed Dec 6, 2023
1 parent 69c4b76 commit 609853f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions data/input6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 40 92 97 90
Distance: 215 1064 1505 1100
77 changes: 77 additions & 0 deletions src/dc/aoc2023/Day6.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Class doc.aoc2023.Day6 Extends doc.aoc2023.Base
{

Parameter InputFile = "input6.txt";

ClassMethod ParseValue(line, mode, part As %Integer = 1)
{
if part=1{
For i = 1:1:$LENGTH($P(line,":",2)," "){//Skip first, it will be blank
Set ^races(mode,i)=+$PIECE(line," ",i)
}
} else {
set ^races(mode,1)=+$PIECE(line,":",2)
}
}

// The difference between pt 1 and 2 is so slight, this will just do double duty

ClassMethod Run(part As %Integer = 1) As %Integer
{
Kill ^races
Kill ^raceresults
Set stream=..GetInput(..#InputFile)
Set count=0

//Setup all reference globals
While 'stream.AtEnd {
Set line=stream.ReadLine()

set stripcond = "=W"
set:part=2 stripcond="*W"
Set line =$ZSTRIP(line,stripcond) //get rid of repeat whitespaces
if $EXTRACT(line,1,4)="Time" Do ..ParseValue($ZSTRIP(line,"<>W"),"time",part)
if $EXTRACT(line,1,4)="Dist" Do ..ParseValue($ZSTRIP(line,"<>W"),"dist",part)
}

set raceno=""
for {
set raceno=$ORDER(^races("time",raceno))
q:raceno=""
w !,"Simulating race "_raceno
set targetdist = ^races("dist",raceno)
Set racetime = ^races("time",raceno)
set wins = 0
for i=1:1:racetime {
if ((i*1)*(racetime-i)>=targetdist) {
d $INCREMENT(wins)
d $INCREMENT(^raceresults)
}
}
w !,"Wins counted "_wins,!



set ^raceresults(raceno)=wins
}

//Parse out seeds
Set count = 0
Set seedvalue=""

zw ^raceresults

Set racekey = ""
Set racekey=$ORDER(^raceresults(racekey)) //Prime the first run
Set count = ^raceresults(racekey)
For {
Set racekey=$ORDER(^raceresults(racekey))
Quit:racekey=""
Set count=count*^raceresults(racekey)

}
Write !,"returning results: "_count,!
Quit count
}

}

0 comments on commit 609853f

Please sign in to comment.