diff --git a/aoc2024/Day19.newt b/aoc2024/Day19.newt new file mode 100644 index 0000000..148052c --- /dev/null +++ b/aoc2024/Day19.newt @@ -0,0 +1,63 @@ +module Day19 + +import Prelude +import Node +import Aoc + +Rules : U +Rules = List (List Char) + +data Problem : U where + MkP : Rules → Rules → Problem + +parseFile : String -> Either String Problem +parseFile text = + let (a :: b :: Nil) = split (trim text) "\n\n" | xs => Left $ (show $ length xs) ++ " parts" + in Right (MkP (map unpack $ split a ", ") (map unpack $ lines b)) + +State : U +State = List (Int × List Char) + +matches : Rules -> List Char -> Int +matches rules text = go (map (_,_ 1) rules) text + where + step : State -> Char -> State -> State + step acc c Nil = acc + step acc c ((n, Nil) :: rs) = step acc c rs + step acc c ((n, (x :: xs)) :: rs) = if x == c + then step ((n, xs) :: acc) c rs + else step acc c rs + + nils : Int -> State -> Int + nils acc Nil = acc + nils acc ((n, Nil) :: xs) = nils (acc + n) xs + nils acc (x :: xs) = nils acc xs + + go : State -> List Char -> Int + go st Nil = nils 0 st + go st (c :: cs) = case nils 0 st of + 0 => go (step Nil c st) cs + n => let st = map (_,_ n) rules ++ st + in go (step Nil c st) cs + +part1 : Problem -> IO Unit +part1 (MkP rules msgs) = do + let (r :: rs) = rules | _ => putStrLn "no rules" + let out = map (matches rules) msgs + let p1 = length $ filter (_/=_ 0) out + putStrLn $ "part1 " ++ show p1 + let p2 = foldl _+_ 0 out + putStrLn $ "part2 " ++ show p2 + +run : String -> IO Unit +run fn = do + putStrLn fn + text <- readFile fn + let (Right prob) = parseFile text | Left err => putStrLn err + part1 prob + + +main : IO Unit +main = do + run "aoc2024/day19/eg.txt" + run "aoc2024/day19/input.txt" diff --git a/aoc2024/day19/eg.txt b/aoc2024/day19/eg.txt new file mode 100644 index 0000000..29648be --- /dev/null +++ b/aoc2024/day19/eg.txt @@ -0,0 +1,10 @@ +r, wr, b, g, bwu, rb, gb, br + +brwrr +bggr +gbbr +rrbgbr +ubwu +bwurrg +brgr +bbrgwb diff --git a/newt/Prelude.newt b/newt/Prelude.newt index 88929d6..a607ce6 100644 --- a/newt/Prelude.newt +++ b/newt/Prelude.newt @@ -287,7 +287,7 @@ pfunc arrayToList uses (Nil _::_) : {0 a} → Array a → List a := `(a,arr) => -- for now I'll run this in JS -pfunc lines : String → List String := `(s) => arrayToList(s.split('\n'))` +pfunc lines uses (arrayToList) : String → List String := `(s) => arrayToList(undefined,s.split('\n'))` pfunc p_strHead : (s : String) -> Char := `(s) => s[0]` pfunc p_strTail : (s : String) -> String := `(s) => s[0]` diff --git a/playground/samples/aoc2024/Day19.newt b/playground/samples/aoc2024/Day19.newt new file mode 120000 index 0000000..75c3ce2 --- /dev/null +++ b/playground/samples/aoc2024/Day19.newt @@ -0,0 +1 @@ +../../../aoc2024/Day19.newt \ No newline at end of file diff --git a/playground/samples/aoc2024/day19 b/playground/samples/aoc2024/day19 new file mode 120000 index 0000000..1a9b9a4 --- /dev/null +++ b/playground/samples/aoc2024/day19 @@ -0,0 +1 @@ +../../../aoc2024/day19 \ No newline at end of file