summaryrefslogtreecommitdiff
path: root/7-2.hs
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-07 13:10:58 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-07 13:34:13 +0100
commit66c22faa1fd89b201154034ae7f5e618f00a8239 (patch)
treec237ece46d97cdc2a87ef3dd6a5f76566832a9a1 /7-2.hs
parentf6bd85a982cd057cd9c7f573686e50c5d8680b0f (diff)
downloadaoc2024-66c22faa1fd89b201154034ae7f5e618f00a8239.tar.gz
aoc2024-66c22faa1fd89b201154034ae7f5e618f00a8239.zip
7-2
Diffstat (limited to '7-2.hs')
-rw-r--r--7-2.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/7-2.hs b/7-2.hs
new file mode 100644
index 0000000..f191196
--- /dev/null
+++ b/7-2.hs
@@ -0,0 +1,25 @@
+cat :: Int -> Int -> Int
+cat x y = x * 10^(floor (logBase 10 (fromIntegral y)) + 1) + y
+
+combine :: [Int] -> [Int]
+combine (x:xs) = foldl f [x] xs
+ where
+ f :: [Int] -> Int -> [Int]
+ f ns n = [(+), (*), cat] <*> ns <*> [n]
+
+check :: Int -> [Int] -> Bool
+check x xs = elem x (combine xs)
+
+parseLine :: String -> (Int,[Int])
+parseLine s = (read $ init x, map read xs)
+ where
+ (x:xs) = words s
+
+result :: [(Int,[Int])] -> Int
+result xs = sum . map fst . filter snd
+ $ zip (map fst xs) (map (uncurry check) xs)
+
+main :: IO ()
+main = do
+ ls <- lines <$> getContents
+ print . result . map parseLine $ ls