[2022 day 3][C] Rucksacks on an Arduino Uno
I usually do AoC challenges in C on a normal computer but I have an Arduino Uno so I thought I'd try that. Took me a little while to figure out how to use the built-in flash memory (of 32 kB, that's right: kilobyte) to store the input file, because it certainly didn't fit in normal program memory which is only TWO kilobyte ...
So what I did is transform the input file to one long string with embedded newlines and use the magic Arduino incantation "PROGMEM" like so:
const char data[] PROGMEM =
"MVWpzTTrTFNNLtssjV\n"
"hRJncnJCnhPCnBSbCQRhhQRPFHmsbHLzbLNHsjNNFmGGGsGF\n"
// etc. for 298 more lines
After that I used pgm_read_byte_near() to read the lines char by char into a buffer for further processing, while checking for newlines manually. Fortunately it is possible to use 64-bit ints on an Arduino Uno even though it's an 8-bit controller. I'm sure that that is extra slow but I had already solved today using those as bit masks, so [shrug].
Compiler info:
Sketch uses 13554 bytes (42%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.
And output over USB serial:
Part 1: 7701
Part 2: 2644
Time: 193 ms
Which is correct, and surprisingly fast! Yay. Code here: https://github.com/ednl/aoc2022/tree/main/aoc22-03
Posted Dec 4, 2022, 12:36 AM UTC