aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2022/25-Full_of_Hot_Air/example13
-rw-r--r--2022/25-Full_of_Hot_Air/first.hs79
-rw-r--r--2022/25-Full_of_Hot_Air/input124
3 files changed, 216 insertions, 0 deletions
diff --git a/2022/25-Full_of_Hot_Air/example b/2022/25-Full_of_Hot_Air/example
new file mode 100644
index 0000000..027aeec
--- /dev/null
+++ b/2022/25-Full_of_Hot_Air/example
@@ -0,0 +1,13 @@
+1=-0-2
+12111
+2=0=
+21
+2=01
+111
+20012
+112
+1=-1=
+1-12
+12
+1=
+122
diff --git a/2022/25-Full_of_Hot_Air/first.hs b/2022/25-Full_of_Hot_Air/first.hs
new file mode 100644
index 0000000..60e5c17
--- /dev/null
+++ b/2022/25-Full_of_Hot_Air/first.hs
@@ -0,0 +1,79 @@
+-- requires cabal install --lib megaparsec parser-combinators unordered-containers
+module Main (main) where
+import Control.Monad (void, when)
+import Data.Functor
+import Data.List qualified as L
+import Data.Map qualified as M
+import Data.Set qualified as S
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import System.Exit (die)
+
+import Debug.Trace
+
+exampleExpectedOutput = [Two, Equal, Minus, One, Equal, Zero] --"2=-1=0"
+
+data Digit = Equal | Minus | Zero | One | Two deriving (Eq, Ord)
+instance Show Digit where
+ show Equal = "="
+ show Minus = "-"
+ show Zero = "0"
+ show One = "1"
+ show Two = "2"
+type Snafu = [Digit]
+type Input = [Snafu]
+
+type Parser = Parsec Void String
+
+parseDigit :: Parser Digit
+parseDigit = (char '2' $> Two)
+ <|> (char '1' $> One)
+ <|> (char '0' $> Zero)
+ <|> (char '-' $> Minus)
+ <|> (char '=' $> Equal)
+
+parseInput' :: Parser Input
+parseInput' = some (some parseDigit <* eol) <* eof
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> die $ errorBundlePretty bundle
+ Right input' -> return input'
+
+snafuToInt :: Snafu -> Int
+snafuToInt = L.foldl' snafuToInt' 0
+ where
+ snafuToInt' :: Int -> Digit -> Int
+ snafuToInt' acc Equal = acc * 5 - 2
+ snafuToInt' acc Minus = acc * 5 - 1
+ snafuToInt' acc Zero = acc * 5
+ snafuToInt' acc One = acc * 5 + 1
+ snafuToInt' acc Two = acc * 5 + 2
+
+intToSnafu :: Int -> Snafu
+intToSnafu = intToSnafu' []
+ where
+ intToSnafu' :: Snafu -> Int -> Snafu
+ intToSnafu' acc 0 = acc
+ intToSnafu' acc num = let (d, m) = (num + 2) `divMod` 5
+ next = case m of
+ 0 -> Equal
+ 1 -> Minus
+ 2 -> Zero
+ 3 -> One
+ 4 -> Two
+ in intToSnafu' (next : acc) d
+
+compute :: Input -> Snafu
+compute = intToSnafu . sum . map snafuToInt
+
+main :: IO ()
+main = do
+ example <- parseInput "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- parseInput "input"
+ print . concatMap show $ compute input
diff --git a/2022/25-Full_of_Hot_Air/input b/2022/25-Full_of_Hot_Air/input
new file mode 100644
index 0000000..9b56092
--- /dev/null
+++ b/2022/25-Full_of_Hot_Air/input
@@ -0,0 +1,124 @@
+12-1
+1=-20
+1=112011=
+1=212=01
+2--2-=
+11-2-0201-1
+1--=1-110=00===-
+1=2==221011==22
+22=0=
+20---
+1==00-121=
+2-2=--00
+1110=22-10=1=12
+1-1===22-1-=2
+1010-22-2-121-1
+111
+1121102
+1=-11
+102=0-=-2-2=--1-2
+2-
+22-=000=1=-==
+2011-=1221
+211=02
+1=-002
+10---21102
+1-00=2-1==-0
+1012=22-22-
+10-01====-010
+22120020121=20
+1-01=002=1=-122=
+1-101-==1
+1=01=1-===--
+21122
+120=2=1102=1
+10=20=2==20
+1111200=221-1=--2
+1==2-=1
+11=1=2-1-21-=1
+2-02=0==-1---
+120
+12
+10-0=-=2==2120=-
+1-=0
+2==-=202-11110=020
+1-=212=0212--02---=0
+2-=1-2=22=
+1=-2-1===200-0=1
+10-110
+200=-2
+210=-21=021-=-12-
+2=-2122-20-11=
+2=1-1
+202=1-20=0--11
+12200202
+1-2-21-
+100=-2
+2101010-022
+1=-===010=1--1-2
+10-=2020===0021
+11=10=0=22-10=2=
+12-221-2==21=-1
+1=-01--00-
+21100==-0=120-=0--2
+122=1-220-010-0=
+120-=-0==--01222-1
+1=220=1==122-1-=22
+112=1-2=001-
+21-012
+1=21=10=1==2
+2=-=--112=1---
+2-=
+12020-022-210
+1=122==-20-12220-
+20=-021-22
+1=2-020
+101-
+2==-010=20-120
+10-2=110=
+2=22
+1=2=0112=22-1-=2
+20=0-22---0012=2-
+10121-=0=1
+1--2=
+1=--10222-1-2-1-2-
+212-01220=-=
+1-=-0==0=0
+10102-
+20-2-1=-120212-1
+2=01=
+10101-100-1-=10
+210=0----00=21-
+1--
+111-2=2=22--=
+12-22-102==1-21-=
+21-2==11
+1-=-2
+22=1=22-0=-20
+120-=
+1=20=20
+2=12-12-=-
+1=2--0-012=
+1-2=221102
+1=211=-===11-11
+21-==-01=0210--
+1110-=02001
+2=12
+1===11=1-1-2=
+212=11=11=12000
+20=12-02=-=201==-
+2==-=21-2=01=01-2
+1-==1=0-02=
+211==--22
+1-2-02=0=12=1-=1211
+12122-021==
+1-==2110021
+11210211==11=
+1=-
+112=12-
+1-
+1==-1=-110
+2-2100210
+2-=1---1-02220201
+1201-0
+1---02=-2--=-2