From 9d90dd828eb50a3f56ffcc9e1305dcb85d4b0f91 Mon Sep 17 00:00:00 2001 From: Steve Dunham Date: Tue, 24 Dec 2024 22:28:46 -0800 Subject: [PATCH] day25 --- aoc2024/Day25.newt | 68 +++++++++++++++++++++++++++ aoc2024/day25/eg.txt | 39 +++++++++++++++ playground/samples/aoc2024/Day25.newt | 1 + playground/samples/aoc2024/day25 | 1 + 4 files changed, 109 insertions(+) create mode 100644 aoc2024/Day25.newt create mode 100644 aoc2024/day25/eg.txt create mode 120000 playground/samples/aoc2024/Day25.newt create mode 120000 playground/samples/aoc2024/day25 diff --git a/aoc2024/Day25.newt b/aoc2024/Day25.newt new file mode 100644 index 0000000..e180978 --- /dev/null +++ b/aoc2024/Day25.newt @@ -0,0 +1,68 @@ +module Day25 + +import Prelude +import Node +import Aoc + +data Chunk : U where + Key : List Int → Chunk + Lock : List Int → Chunk + +-- cribbed from the idris library, it's late and I don't want to work thisout +transpose : ∀ a. List (List a) → List (List a) +transpose Nil = Nil +transpose {a} (heads :: tails) = spreadHeads heads (transpose tails) + where + spreadHeads : List a → List (List a) → List (List a) + spreadHeads Nil tails = tails + spreadHeads (head :: heads) Nil = (head :: Nil) :: spreadHeads heads Nil + spreadHeads (head :: heads) (tail :: tails) = (head :: tail) :: spreadHeads heads tails + +count : List Char → Int +count cs = go cs 0 + where + go : List Char → Int → Int + go ('#' :: cs) acc = go cs (1 + acc) + go _ acc = acc + +toChunk : List (List Char) → Chunk + +parseChunk : String → Chunk +parseChunk text = + let stuff = transpose $ map unpack $ split text "\n" in + -- TODO - sort this out + case map {List} count stuff of + 0 :: xs => Key $ map (count ∘ reverse) stuff + xs => Lock xs + + +parseFile : String → List Chunk +parseFile text = do + let parts = split (trim text) "\n\n" + map parseChunk parts + +splitKeys : List Chunk → List (List Int) → List (List Int) → List (List Int) × List (List Int) +splitKeys (Lock xs :: rest) locks keys = splitKeys rest (xs :: locks) keys +splitKeys (Key xs :: rest) locks keys = splitKeys rest locks (xs :: keys) +splitKeys Nil locks keys = (locks, keys) + +check : List Int → List Int → Int +check Nil Nil = 1 +check (a :: as) (b :: bs) = if a + b <= 7 then check as bs else 0 +check _ _ = 0 + +run : String -> IO Unit +run fn = do + putStrLn fn + text <- readFile fn + putStrLn text + let chunks = parseFile text + let (locks,keys) = splitKeys chunks Nil Nil + debugLog (length locks, length keys) + let p1 = foldl _+_ 0 $ map (\ l => foldl _+_ 0 $ map (check l) keys) locks + putStrLn $ "part1 " ++ show p1 + +main : IO Unit +main = do + run "aoc2024/day25/eg.txt" + run "aoc2024/day25/input.txt" diff --git a/aoc2024/day25/eg.txt b/aoc2024/day25/eg.txt new file mode 100644 index 0000000..8e29855 --- /dev/null +++ b/aoc2024/day25/eg.txt @@ -0,0 +1,39 @@ +##### +.#### +.#### +.#### +.#.#. +.#... +..... + +##### +##.## +.#.## +...## +...#. +...#. +..... + +..... +#.... +#.... +#...# +#.#.# +#.### +##### + +..... +..... +#.#.. +###.. +###.# +###.# +##### + +..... +..... +..... +#.... +#.#.. +#.#.# +##### diff --git a/playground/samples/aoc2024/Day25.newt b/playground/samples/aoc2024/Day25.newt new file mode 120000 index 0000000..022657f --- /dev/null +++ b/playground/samples/aoc2024/Day25.newt @@ -0,0 +1 @@ +../../../aoc2024/Day25.newt \ No newline at end of file diff --git a/playground/samples/aoc2024/day25 b/playground/samples/aoc2024/day25 new file mode 120000 index 0000000..3fda4ae --- /dev/null +++ b/playground/samples/aoc2024/day25 @@ -0,0 +1 @@ +../../../aoc2024/day25 \ No newline at end of file