From c29a87fb28170d9b551e7846fc459a487631e0fb Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Tue, 19 Dec 2023 02:29:32 +0200 Subject: [PATCH] Day 02, part 2 --- .gitignore | 2 +- day02/Makefile | 6 +++--- day02/{day02a.d => day02.d} | 32 +++++++++++++++++++++++++------ day02/{day02a.test => day02.test} | 0 4 files changed, 30 insertions(+), 10 deletions(-) rename day02/{day02a.d => day02.d} (83%) rename day02/{day02a.test => day02.test} (100%) diff --git a/.gitignore b/.gitignore index 3099ce8..4e80083 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.r *.in -/day02/day02a +/day02/day02 diff --git a/day02/Makefile b/day02/Makefile index 91f56e2..ea70829 100644 --- a/day02/Makefile +++ b/day02/Makefile @@ -1,8 +1,8 @@ -all: day02a +all: day02 -day02a: day02a.r +day02: day02.r vamos -c ~/draco/config c:drlink to $@ \ - from drlib:drstart.o+day02a.r \ + from drlib:drstart.o+day02.r \ library drlib:drio.lib+drlib:draco.lib+drlib:dos.lib+drlib:exec.lib \ smallcode diff --git a/day02/day02a.d b/day02/day02.d similarity index 83% rename from day02/day02a.d rename to day02/day02.d index e14b882..a8d8ded 100644 --- a/day02/day02a.d +++ b/day02/day02.d @@ -138,47 +138,67 @@ proc readColors(*uint red, green, blue) void: od; corp; -proc doGame(uint gameid) bool: +proc doGame(uint gameid; *uint power) bool: + uint min_red, min_green, min_blue; uint red, green, blue; bool continue, valid; + min_red := 0; + min_green := 0; + min_blue := 0; + valid := true; continue := true; while not match('\n') do readColors(&red, &green, &blue); + if red > MAX_RED or green > MAX_GREEN or blue > MAX_BLUE then valid := false; fi; + + if red > min_red then + min_red := red; + fi; + if green > min_green then + min_green := green; + fi; + if blue > min_blue then + min_blue := blue; + fi; + if match(';') then nextChar(); fi; od; + power* := min_red * min_green * min_blue; valid corp; proc main() void: - uint gameid; - uint sum; + uint gameid, sum, power, powersum; HasPeekChar := false; - sum := 0; + sum := 0; powersum := 0; + while peek() ~= END do expect("Game"); gameid := readUint(); expect(":"); - if doGame(gameid) then + if doGame(gameid, &power) then sum := sum + gameid; fi; + powersum := powersum + power; if match('\n') then nextChar(); fi; od; - writeln("Result: ", sum); + writeln("Result 1: ", sum); + writeln("Result 2: ", powersum); exit(0); corp; diff --git a/day02/day02a.test b/day02/day02.test similarity index 100% rename from day02/day02a.test rename to day02/day02.test -- 2.45.2