day7
This commit is contained in:
58
aoc2024/Day7.newt
Normal file
58
aoc2024/Day7.newt
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
module Day7
|
||||||
|
|
||||||
|
import Prelude
|
||||||
|
import Node
|
||||||
|
import Aoc
|
||||||
|
|
||||||
|
Prob : U
|
||||||
|
Prob = Int × List Int
|
||||||
|
|
||||||
|
cases : Int → Int → List Int → Bool
|
||||||
|
cases goal acc Nil = goal == acc
|
||||||
|
cases goal acc (x :: xs) =
|
||||||
|
if goal < acc then False
|
||||||
|
else if cases goal (x + acc) xs then True
|
||||||
|
else cases goal (x * acc) xs
|
||||||
|
|
||||||
|
part1 : Prob → Bool
|
||||||
|
part1 (goal, x :: xs) = cases goal x xs
|
||||||
|
part1 _ = False
|
||||||
|
|
||||||
|
cat : Int → Int → Int
|
||||||
|
cat x y = stringToInt $ show x ++ show y
|
||||||
|
|
||||||
|
cases2 : Int → Int → List Int → Bool
|
||||||
|
cases2 goal acc Nil = goal == acc
|
||||||
|
cases2 goal acc (x :: xs) =
|
||||||
|
if goal < acc then False
|
||||||
|
else if cases2 goal (x + acc) xs then True
|
||||||
|
else if cases2 goal (x * acc) xs then True
|
||||||
|
else cases2 goal (cat acc x) xs
|
||||||
|
|
||||||
|
part2 : Prob → Bool
|
||||||
|
part2 (goal, x :: xs) = cases2 goal x xs
|
||||||
|
part2 _ = False
|
||||||
|
|
||||||
|
parse : String -> Maybe (List Prob)
|
||||||
|
parse text = do
|
||||||
|
traverse parseLine $ split (trim text) "\n"
|
||||||
|
where
|
||||||
|
parseLine : String → Maybe Prob
|
||||||
|
parseLine line = do
|
||||||
|
let (a :: b :: Nil) = split line ": " | _ => Nothing
|
||||||
|
Just (stringToInt a , nums b)
|
||||||
|
|
||||||
|
run : String -> IO Unit
|
||||||
|
run fn = do
|
||||||
|
putStrLn fn
|
||||||
|
text <- readFile fn
|
||||||
|
let (Just probs) = parse text | _ => putStrLn "parse error"
|
||||||
|
let p1 = foldl _+_ 0 $ map fst $ filter part1 probs
|
||||||
|
putStrLn $ "part1 " ++ show p1
|
||||||
|
let p2 = foldl _+_ 0 $ map fst $ filter part2 probs
|
||||||
|
putStrLn $ "part2 " ++ show p2
|
||||||
|
|
||||||
|
main : IO Unit
|
||||||
|
main = do
|
||||||
|
run "aoc2024/day7/eg.txt"
|
||||||
|
run "aoc2024/day7/input.txt"
|
||||||
9
aoc2024/day7/eg.txt
Normal file
9
aoc2024/day7/eg.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
190: 10 19
|
||||||
|
3267: 81 40 27
|
||||||
|
83: 17 5
|
||||||
|
156: 15 6
|
||||||
|
7290: 6 8 6 15
|
||||||
|
161011: 16 10 13
|
||||||
|
192: 17 8 14
|
||||||
|
21037: 9 7 18 13
|
||||||
|
292: 11 6 16 20
|
||||||
Reference in New Issue
Block a user