Created
August 2, 2024 15:25
-
-
Save Sorebit/50fcc77829cd60ee25a2b950870932ff to your computer and use it in GitHub Desktop.
41
This file contains 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
from collections import defaultdict | |
word = 'KELNER' | |
rev_word = word[::-1] | |
side = 16 | |
def groups(data, func): | |
grouping = defaultdict(list) | |
for y in range(len(data)): | |
for x in range(len(data[y])): | |
grouping[func(x, y)].append(data[y][x]) | |
return list(map(grouping.get, sorted(grouping))) | |
def count_in_str(s: str) -> int: | |
return s.count(word) + s.count(rev_word) | |
def count_in_groups(data, func): | |
total = 0 | |
for g in groups(data, func): | |
total += count_in_str("".join(g)) | |
return total | |
def count(box: list[str]) -> int: | |
total = 0 | |
total += count_in_groups(box, lambda x, y: y) # horizontal | |
total += count_in_groups(box, lambda x, y: x) # vertical | |
total += count_in_groups(box, lambda x, y: x + y) # forwards diag | |
total += count_in_groups(box, lambda x, y: x - y) # backwards diag | |
return total | |
data = [ | |
"KERENLEKRKELNERL", | |
"EKELNERRENLEKENK", | |
"RENLEKKLNERENLEK", | |
"EKLKKNNELRKLRLRK", | |
"NRENLEKNELEENKEE", | |
"LEKLRNLNKKNELLEL", | |
"ELRENELNRLRENNKN", | |
"KKELLEERENLEKLEE", | |
"RENNKNRKKRRRNNLR", | |
"RLLKERENLEKRKLNK", | |
"ENENEKNRRREEEEEL", | |
"NEKKELEEENLKLLRK", | |
"LRELKRNLLKNRNNRN", | |
"ELNRELLENEEEEKEE", | |
"KELNERKNRERERELR", | |
"RNEKELNERLRENLEK", | |
] | |
print(count(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment