[py] Solve 2024_04 part 2
This commit is contained in:
parent
281a027857
commit
833a451c9c
4 changed files with 96 additions and 20 deletions
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 2464
|
Part 1: 2464
|
||||||
|
Part 2: 1982
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,104 @@
|
||||||
def mirror_h(lines):
|
def get_char(lines, x, y):
|
||||||
return [line[::-1] for line in lines]
|
if 0 <= y < len(lines):
|
||||||
|
line = lines[y]
|
||||||
|
if 0 <= x < len(line):
|
||||||
|
return line[x]
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def mirror_v(lines):
|
def is_pattern_at(lines, pattern, x, y):
|
||||||
return lines[::-1]
|
for dy, line in enumerate(pattern):
|
||||||
|
for dx, char in enumerate(line):
|
||||||
|
if char == " ":
|
||||||
|
continue
|
||||||
|
if get_char(lines, x + dx, y + dy) == char:
|
||||||
|
continue
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def transpose(lines):
|
def count_pattern(lines, pattern):
|
||||||
return ["".join(chars) for chars in zip(*lines)]
|
return sum(
|
||||||
|
is_pattern_at(lines, pattern, x, y)
|
||||||
|
for y, line in enumerate(lines)
|
||||||
|
for x, _ in enumerate(line)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def shift(lines):
|
def count_patterns(lines, patterns):
|
||||||
return [" " * i + line + " " * (len(lines) - 1 - i) for i, line in enumerate(lines)]
|
return sum(count_pattern(lines, pattern) for pattern in patterns)
|
||||||
|
|
||||||
|
|
||||||
def count_xmas(lines):
|
XMAS_PATTERNS = [
|
||||||
return sum(line.count("XMAS") for line in lines)
|
[
|
||||||
|
"XMAS",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"X",
|
||||||
|
" M",
|
||||||
|
" A",
|
||||||
|
" S",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"X",
|
||||||
|
"M",
|
||||||
|
"A",
|
||||||
|
"S",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
" X",
|
||||||
|
" M",
|
||||||
|
" A",
|
||||||
|
"S",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"SAMX",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"S",
|
||||||
|
" A",
|
||||||
|
" M",
|
||||||
|
" X",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"S",
|
||||||
|
"A",
|
||||||
|
"M",
|
||||||
|
"X",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
" S",
|
||||||
|
" A",
|
||||||
|
" M",
|
||||||
|
"X",
|
||||||
|
],
|
||||||
|
]
|
||||||
|
|
||||||
|
X_MAS_PATTERNS = [
|
||||||
|
[
|
||||||
|
"M M",
|
||||||
|
" A ",
|
||||||
|
"S S",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"S M",
|
||||||
|
" A ",
|
||||||
|
"S M",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"S S",
|
||||||
|
" A ",
|
||||||
|
"M M",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"M S",
|
||||||
|
" A ",
|
||||||
|
"M S",
|
||||||
|
],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def solve(inputstr):
|
def solve(inputstr):
|
||||||
w2e = inputstr.strip().split()
|
lines = inputstr.strip().split()
|
||||||
n2s = transpose(w2e)
|
print(f"Part 1: {count_patterns(lines, XMAS_PATTERNS)}")
|
||||||
nw2se = transpose(mirror_v(shift(mirror_v(w2e))))
|
print(f"Part 2: {count_patterns(lines, X_MAS_PATTERNS)}")
|
||||||
ne2sw = transpose(shift(w2e))
|
|
||||||
|
|
||||||
orientations = [w2e, n2s, nw2se, ne2sw]
|
|
||||||
orientations = orientations + [mirror_h(o) for o in orientations]
|
|
||||||
|
|
||||||
part1 = sum(count_xmas(o) for o in orientations)
|
|
||||||
print(f"Part 1: {part1}")
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 4
|
Part 1: 4
|
||||||
|
Part 2: 0
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
Part 1: 18
|
Part 1: 18
|
||||||
|
Part 2: 9
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue