summaryrefslogtreecommitdiff
path: root/11-1.hs
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-11 10:22:10 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-11 10:22:10 +0100
commit109578eaa586a23830c0ae43a9f1418b77a27be1 (patch)
tree016018f4331d936be4fd616a2e6042e36c8b78c8 /11-1.hs
parentcff3c9c15bce92c6697ef13be2485d6bcf7291ed (diff)
downloadaoc2024-109578eaa586a23830c0ae43a9f1418b77a27be1.tar.gz
aoc2024-109578eaa586a23830c0ae43a9f1418b77a27be1.zip
11-1
Diffstat (limited to '11-1.hs')
-rw-r--r--11-1.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/11-1.hs b/11-1.hs
new file mode 100644
index 0000000..de60536
--- /dev/null
+++ b/11-1.hs
@@ -0,0 +1,37 @@
+data Tree = Node Int | Branch [Tree]
+
+toList :: Tree -> [Int]
+toList (Node n) = [n]
+toList (Branch ts) = concatMap toList ts
+
+digits :: Int -> Int
+digits n = floor $ logBase 10 (fromIntegral n) + 1
+
+transform :: Tree -> Tree
+transform (Node 0) = Node 1
+transform (Node n)
+ | even (digits n) = Branch . map Node $ halves n
+ | otherwise = Node $ n * 2024
+transform (Branch ts) = Branch $ fmap transform ts
+
+halves :: Int -> [Int]
+halves n = [ floor $ fromIntegral n / 10^x
+ , n `mod` 10^x
+ ]
+ where
+ x = digits n `div` 2
+
+parse :: String -> Tree
+parse = Branch . map (Node . read) . words
+
+flatten :: Tree -> Tree
+flatten = Branch . map Node . toList
+
+main :: IO ()
+main = getLine
+ >>= print
+ . length
+ . toList
+ . (!!25)
+ . iterate (flatten . transform)
+ . parse