Advent of Code 2022 (Day 1)
Bruce Axtens
Posted on December 6, 2022
I'm a couple of days behind in the AoC. To make the challenge more demanding I opted to use a language I'm not extremely familiar with: 8th.
8th is a FORTH-dialect aimed at the cross-platform developer market. I'm in the process of adding an 8th language track on Exercism.
I expect an 8th guru (there are a few) will sigh with exasperation on examining my code, but I plead ignorance (and a "let's just solve the problem, okay?" attitude).
My progress is being logged on github at AdventOfCode.
Day 1, Part 1 resulted in this little blob of code
"input" f:slurp \ read in input file
>s s:trim \ convert buffer to string and trim
"\r\n\r\n" s:/ \ split into elves (on Windows so CRLF)
(
"\r\n" s:/ \ split into meals
' >n a:map \ convert to number
' n:+ 0 a:reduce \ total calories
) a:map \ apply to each elf
' n:cmp a:sort \ sort smallest to largest
a:pop nip . cr \ pop of the last item, clear the stack, display
bye \ exit
The RPN syntax is a challenge but I'm starting to get the hang of it. Having to deal with a stack forces you to refactor. For a longer discussion of that particular issue see ForthValues.
Day 1, Part 2. For all of my talk about refactoring, there's no sign of it at this point.
"input" f:slurp >s s:trim \ read in file and trim
"\r\n\r\n" s:/ \ split into elves
(
"\r\n" s:/ \ split into meals
' >n a:map \ convert to number
' n:+ 0 a:reduce \ total calories
) a:map \ apply to each elf
' n:cmp a:sort \ sort smallest to greatest
0 >r \ keep a total in r-stack
( a:pop r> n:+ >r ) 3 times \ pop off three items from the high end and accrue
r> . \ retrieve total and display
drop \ clean up stack
bye \ exit
Posted on December 6, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.