summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorCrazazy2024-12-03 12:05:53 +0100
committerCrazazy2024-12-03 12:05:53 +0100
commit9dfdb20a55fe3ef7763aaee1be7de1b2179c46de (patch)
treeedb05c3731a7ea94c9abb4c50ddc79ca3d85065b /src
parente39685da36d625ce76f8ec376733dc67a80040d8 (diff)
Finish day3 HEAD master
easiest day so far
Diffstat (limited to 'src')
-rw-r--r--src/Day3.idr40
-rw-r--r--src/Main.idr2
2 files changed, 42 insertions, 0 deletions
diff --git a/src/Day3.idr b/src/Day3.idr
new file mode 100644
index 0000000..5f14827
--- /dev/null
+++ b/src/Day3.idr
@@ -0,0 +1,40 @@
+module Day3 
+
+import Data.String.Parser
+import Data.String
+
+mul : Parser Integer
+mul = (*) <$ string "mul(" <*> integer <* string "," <*> integer <* string ")"
+
+
+partParser : Parser a -> String -> List a
+partParser p str = case str of
+                 "" => []
+                 _ => case parse p str of
+                           Left _ => partParser p (strSubstr 1 (strLength str) str)
+                           Right (num, bla) => num :: (partParser p (strSubstr bla (strLength str) str))
+
+part1 : String -> List Integer
+part1 = partParser mul
+
+export sol1 : String -> String
+sol1 = show . sum . part1
+
+dont : Parser (Either Bool Integer)
+dont = Left <$> ((True <$ string "do()") <|> (False <$ string "don't()"))
+
+mul2 : Parser (Either Bool Integer)
+mul2 = (Right <$> mul) <|> dont
+
+part2 : String -> List (Either Bool Integer)
+part2 = partParser mul2
+
+toggleSum : Bool -> List (Either Bool Integer) -> Integer
+toggleSum x [] = 0
+toggleSum x ((Left y) :: xs) = toggleSum y xs
+toggleSum False ((Right y) :: xs) = toggleSum False xs
+toggleSum True ((Right y) :: xs) = y + (toggleSum True xs)
+
+export
+sol2 : String -> String
+sol2 = show . toggleSum True . part2
diff --git a/src/Main.idr b/src/Main.idr
index c90397f..b3b48e6 100644
--- a/src/Main.idr
+++ b/src/Main.idr
@@ -2,6 +2,7 @@ module Main
 
 import Day1
 import Day2
+import Day3
 import System
 import System.File
 import Data.Fin
@@ -11,6 +12,7 @@ runDay : Int -> String -> String
 runDay n inp = case n of
   1 => unlines [Day1.sol1 inp, Day1.sol2 inp]
   2 => unlines [Day2.sol1 inp, Day2.sol2 inp]
+  3 => unlines [Day3.sol1 inp, Day3.sol2 inp]
   _ => "Not implemented yet!"
 
 main : IO ()