Interesting numerals! We hadn’t got a lot of that this year, so “Full of Hot Air”, today’s final Advent of Code 2022 puzzle, stands up to the challenge.
Literate Haskell because it’s the name of the game, though today’s import list might not stimulate your imagination as much as usual.
import Data.List (foldl1')
Ok, so we’re implementing addition on a balanced quinary numbering system. Let’s parse.
fromB :: Char -> Int
'=' = -2
fromB '-' = -1
fromB '0' = 0
fromB '1' = 1
fromB '2' = 2
fromB
parse :: String -> [[Int]]
= map (reverse . map fromB) . lines parse
There are more or less two easy ways to play this. We can either convert balanced quinary to native, add on native, and convert back to balanced quinary.
Or we can add directly on balanced quinary.
My favorite.
add :: [Int] -> [Int] -> [Int]
= go 0 where
add :as) (b:bs) =
go c (alet s = c + a + b
= if | s < (-2) -> (-1,s + 5)
(c',d) | s > 2 -> ( 1,s - 5)
| otherwise -> (0,s)
in d : go c' as bs
= [c]
go c [] [] = go c [0] bs
go c [] bs = go c as [0] go c as []
Granted, we still need to reorder terms for proper presentation.
toB :: Int -> Char
-2) = '='
toB (-1) = '-'
toB (0 = '0'
toB 1 = '1'
toB 2 = '2'
toB
format :: [Int] -> String
= reverse . map toB format
Whew. Let’s wrap it all in a main
function.
main :: IO ()
= interact $ format . foldl1' add . parse main
Yay! Done at last!
Hot take: Advent of Code remains one of the best-quality problemset out there. I’m merely starting to notice a decrease in quality when comparing it to its former years.
That’s probably be worthy of a postmortem of its own.
In the meantime, let’s appreciate the fact I appear to have ranked 4631th this year. And have not solved a single puzzle in more than 24 hours. Not bad for a transition year.
This will definitely not be remembered as my best year of writing about it, but I’m glad I could mostly maintain the pace. For the longest time I thought I was only writing this for myself, but at some point I started getting actual feedback, Mastodon DMs, GitHub pull requests, oh my!
So wow.
Like, I’m really going to have to implement comments here now.
This concludes the regularly scheduled posts about this. I hope I’ll be able to follow up. At any rate, it’s been a pleasure writing to you. See you soon!