A quick post on day 2 of 2023’s Advent of Code, being completed in XC=BASIC and run on the Commodore 64.
I’m going to have to stop thinking of pre-code data manipulation as somehow cheating – I am working with constraints and XC=BASIC, whilst an excellent compiler and incredibly flexible tool, has it’s own peculiar limits, such as 96 character string lengths, for example.
For this puzzle I had to either split the strings, or reduce them in length, and I opted to reduce them by substituting the colours for their initial letters. So blue became ‘b’ etc.
I also removed the word ‘game’ from the beginning of each line.
All that meant I now had data that fitted nicely into the limits of XCB and I could continue with the conundrum.
The Cube Conundrum Part One
The puzzle describes how a bag contains a number of coloured cubes which are either red, green, or blue. Each time we play this game, the Elf will hide a secret number of cubes of each colour in the bag, and our goal is to figure out information about the number of cubes.
We gather information on these cubes by the elf reaching in and grabbing a handful. So, for example, far game 1 he would reach in and grab 7 blue cubes, 4 red and 11 green. He would then put them back and grab some more, i.e. the second set of cubes for that game, and so on.
The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
So, in the example below 7 blue, 4 red and 11 green fits within that criteria. If all the other sets did too, that would be a possible game. If one set, however, contained 15 blue cubes, then that would mark it as an impossible game. You get the picture…
To achieve this I first created a subroutine called possible.
So for each set of each game it checks if the numbers fit within the limits, and checks if the game as a whole has already had an impossible number. If all is well, it marks the game as possible (P=0).
The subroutine is called for each set within a data string, i.e. each time a semi-colon, marking the end of that set, is reached.
And because we now have a semi-colon in the buffer, it won’t find the the correct next number as it looks for a comma, so we basically do the same as above but substitute ‘,’ with ‘;’
When it reaches the final step, i.e. when the length of the string is reached and the FOR loop is exited, we simply need to check if the game was possible or not and, if it was, total up the game ID.
The Cube Conundrum Part Two
For part 2 we had to find the minimum number of each colour that allowed the game to be played, and multiply them together r x b x g. I tried the same approach, a subroutine called for each iteration or set. This, for some reason I just could not work out, caused strange behaviour with odd characters and text colours changing – classic symptoms that the C64 is not happy with something.
I spent far too long trying to debug and just couldn’t get it, coming to the conclusion that something somewhere was amis with XCB itself. In the end I abandoned the subroutine and simply added the following to the logic for each set.
MINB etc are set to zero for each iteration of the main FOR loop, and then it’s simply set and checked against the current value. It was all too easy in the end.
Once that was done the correct number was arrived at.
I still have 3 days to fit into 1 to catch up, and if yesterday is anything to go by that won’t be done – but that’s what Christmas holidays are for!