Copyright | (c) Eric Mertens 2021 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
https://adventofcode.com/2021/day/18
Today's problem had us perform manipulations on a tree-based term language. It was made tricky because the problem asked us to do things to the nearest left and right neighbors of elements of our tree.
Synopsis
- main :: IO ()
- add :: Tree Int -> Tree Int -> Tree Int
- reduce :: Tree Int -> Tree Int
- unstable :: Tree a -> Maybe (a, a, Zip a)
- explode :: (Int, Int, Zip Int) -> Tree Int
- split :: Tree Int -> Maybe (Tree Int)
- magnitude :: Tree Int -> Int
- data Tree a
- data Side
- type Zip a = [(Side, Tree a)]
- fromZip :: Tree a -> Zip a -> Tree a
- appUp :: Side -> (Tree a -> Tree a) -> Zip a -> Zip a
- appL :: (a -> a) -> Tree a -> Tree a
- appR :: (a -> a) -> Tree a -> Tree a
- parse :: String -> Tree Int
- pInt :: ReadP Int
- pTree :: ReadP a -> ReadP (Tree a)
Documentation
Snailfish operations
split :: Tree Int -> Maybe (Tree Int) Source #
Replace the first number with value 10 or more with a pair of it divided in half rounding first down then up.
magnitude :: Tree Int -> Int Source #
Compute the magnitude of an expression
>>>
magnitude (parse "[9,1]")
29
>>>
magnitude (parse "[[1,2],[[3,4],5]]")
143
Binary trees
A binary tree with data at the leaves
Tree zippers
fromZip :: Tree a -> Zip a -> Tree a Source #
Rebuild a tree given a zipper and the value to put in the hole.
appUp :: Side -> (Tree a -> Tree a) -> Zip a -> Zip a Source #
Apply the given function to the nearest parent sibling on the given side.