diff --git a/2024/04/solution.nix b/2024/04/solution.nix index 9aba538..4db9cea 100644 --- a/2024/04/solution.nix +++ b/2024/04/solution.nix @@ -69,4 +69,56 @@ + findXMAS (rotated content) + findXMAS (diagonal (reversed content)) ; + + xmasMatcher = let + xmas = '' + M.S + .A. + M.S''; + in + "${xmas}|${rotated xmas}|${reversed xmas}|${rotated (reversed xmas)}"; + + isX-MAS = s: s + |> builtins.match xmasMatcher + |> lib.isList + ; + + break3s = s: let + len = builtins.length s - 2; + in + builtins.genList (i: lib.sublist i 3 s) len + ; + + break3lines = s: s + |> lib.splitString "\n" + |> map lib.stringToCharacters + |> break3s + ; + + log = t: lib.traceSeq t t; + + break3x3s = l: l + |> map break3s + |> (groups: + let empty = builtins.elemAt groups 0 + |> builtins.length + |> genStrList []; + in builtins.foldl' + (lib.zipListsWith (a: b: a ++ ["\n"] ++ b)) + empty groups) + |> map lib.concatStrings + |> map lib.trim + ; + + break3x3 = s: s + |> break3lines + |> map break3x3s + |> builtins.concatLists + ; + + part2result = content + |> break3x3 + |> lib.count isX-MAS + ; + } diff --git a/2024/04/solution.test.nix b/2024/04/solution.test.nix index 8e03679..22fc0f0 100644 --- a/2024/04/solution.test.nix +++ b/2024/04/solution.test.nix @@ -49,7 +49,7 @@ let in [ (describe "part 1" [ (it "gets content forward" { - actual = example.forward; + actual = example.content; expected = content; }) (it "gets content rotated" { @@ -77,4 +77,84 @@ in [ expected = 18; }) ]) + (describe "part 2" [ + (it "checks if string is X-MAS" { + actual = map example.isX-MAS [ + '' + M.S + .A. + M.S'' + '' + S.S + .A. + M.M'' + '' + M.M + .A. + S.S'' + '' + S.M + .A. + S.M'' + '' + S.M + .A. + M.M'' + ]; + expected = [true true true true false]; + }) + (it "breaks list into 3s" { + actual = example.break3s [1 2 3 4 5]; + expected = [[1 2 3] [2 3 4] [3 4 5]]; + }) + (it "breaks list into 3x3s" { + actual = example.break3x3s [ + ["1" "2" "3" "4"] + ["5" "6" "7" "8"] + ["9" "A" "B" "C"] + ]; + expected = [ + '' + 123 + 567 + 9AB'' + '' + 234 + 678 + ABC'' + ]; + }) + (it "breaks string into 3 lines" { + actual = example.break3lines '' + 1234 + 5678 + 9ABC + DEFG''; + expected = [[ + ["1" "2" "3" "4"] + ["5" "6" "7" "8"] + ["9" "A" "B" "C"] + ] + [ + ["5" "6" "7" "8"] + ["9" "A" "B" "C"] + ["D" "E" "F" "G"] + ]]; + }) + (it "breaks string into 3x3 blocks" { + actual = example.break3x3 '' + 1234 + 5678 + 9ABC + DEFG''; + expected = [ + "123\n567\n9AB" "234\n678\nABC" + "567\n9AB\nDEF" "678\nABC\nEFG" + ]; + }) + (it "gets the result" { + actual = example.part2result; + expected = 9; + }) + ]) ]