2024 day 4 part 2

This commit is contained in:
tristan 2024-12-04 17:02:21 +00:00
parent 5dee6636c7
commit d0f2fa688f
2 changed files with 133 additions and 1 deletions

View file

@ -69,4 +69,56 @@
+ findXMAS (rotated content) + findXMAS (rotated content)
+ findXMAS (diagonal (reversed 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
;
} }

View file

@ -49,7 +49,7 @@ let
in [ in [
(describe "part 1" [ (describe "part 1" [
(it "gets content forward" { (it "gets content forward" {
actual = example.forward; actual = example.content;
expected = content; expected = content;
}) })
(it "gets content rotated" { (it "gets content rotated" {
@ -77,4 +77,84 @@ in [
expected = 18; 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;
})
])
] ]