Created
December 4, 2024 19:31
-
-
Save isabelroses/7a80c915ee2341825623b55397c5ee1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
lib = import <nixpkgs/lib>; | |
inherit (builtins) | |
map | |
groupBy | |
substring | |
elemAt | |
genList | |
sort | |
split | |
foldl' | |
add | |
head | |
concatLists | |
length | |
stringLength | |
; | |
pos = | |
t: x: y: | |
substring y 1 (elemAt t x); | |
genIdList = n: genList lib.id n; | |
columns = | |
t: w: | |
let | |
listsOfChars = map (x: (map (u: substring x 1 u) t)) (genIdList w); | |
in | |
map lib.concatStrings listsOfChars; | |
diag = | |
t: h: w: | |
let | |
p = lib.cartesianProduct { | |
x = genIdList h; | |
y = genIdList w; | |
}; | |
chars = map (u: { | |
inherit (u) x y; | |
c = pos t u.x u.y; | |
}) p; | |
groupedPri = lib.attrsToList (groupBy (u: toString (u.x - u.y)) chars); | |
groupedSec = lib.attrsToList (groupBy (u: toString (u.x + u.y)) chars); | |
both = map (u: u.value) (concatLists [ | |
groupedPri | |
groupedSec | |
]); | |
order = | |
h: k: | |
if h.x < k.x then | |
true | |
else if h.x == k.x && h.y < k.y then | |
true | |
else | |
false; | |
sorted = map (sort order) both; | |
onlyChars = map (map (v: v.c)) sorted; | |
in | |
map lib.concatStrings onlyChars; | |
scoreStr = | |
s: | |
let | |
match = pattern: length (lib.filter lib.isList (split pattern s)); | |
in | |
(match "XMAS") + (match "SAMX"); | |
sum = foldl' add 0; | |
inputPartOne = '' | |
MMMSXXMASM | |
MSAMXMSMSA | |
AMXSXMAAMM | |
MSAMASMSMX | |
XMASAMXAMM | |
XXAMMXXAMA | |
SMSMSASXSS | |
SAXAMASAAA | |
MAMMMXMMMM | |
MXMXAXMASX | |
''; | |
rows = lib.splitString "\n" inputPartOne; | |
height = length rows; | |
width = stringLength (head rows); | |
in | |
sum ( | |
map scoreStr (concatLists [ | |
rows | |
(columns rows width) | |
(diag rows height width) | |
]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment