Compare commits
4 Commits
e2c1c14b3d
...
7eb1ddac9b
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7eb1ddac9b | ||
![]() |
e5a2a3a704 | ||
![]() |
614e50c8a3 | ||
![]() |
c56be970d5 |
49
q2.py
Normal file
49
q2.py
Normal file
@ -0,0 +1,49 @@
|
||||
def q1(text: str):
|
||||
levels = text.splitlines()
|
||||
|
||||
count = 0
|
||||
for level in levels:
|
||||
curr = [int(l) for l in level.split()]
|
||||
|
||||
increasing = all(curr[i] < curr[i + 1] for i in range(len(curr) - 1))
|
||||
decreasing = all(curr[i] > curr[i + 1] for i in range(len(curr) - 1))
|
||||
diff_criterion = all(
|
||||
1 <= abs(curr[i] - curr[i + 1]) <= 3 for i in range(len(curr) - 1)
|
||||
)
|
||||
|
||||
count += 1 if diff_criterion and (increasing or decreasing) else 0
|
||||
|
||||
return count
|
||||
|
||||
|
||||
def q2(text: str):
|
||||
levels = text.splitlines()
|
||||
|
||||
count = 0
|
||||
for level in levels:
|
||||
curr = [int(l) for l in level.split()]
|
||||
|
||||
for i in range(len(curr) + 1):
|
||||
curr_i = curr.copy()
|
||||
if i < len(curr):
|
||||
del curr_i[i]
|
||||
|
||||
increasing = all(curr_i[i] < curr_i[i + 1] for i in range(len(curr_i) - 1))
|
||||
decreasing = all(curr_i[i] > curr_i[i + 1] for i in range(len(curr_i) - 1))
|
||||
diff_criterion = all(
|
||||
1 <= abs(curr_i[i] - curr_i[i + 1]) <= 3 for i in range(len(curr_i) - 1)
|
||||
)
|
||||
|
||||
if diff_criterion and (increasing or decreasing):
|
||||
count += 1
|
||||
break
|
||||
|
||||
return count
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("i2.txt") as f:
|
||||
text = f.read()
|
||||
|
||||
print(q1(text))
|
||||
print(q2(text))
|
33
q3.py
Normal file
33
q3.py
Normal file
@ -0,0 +1,33 @@
|
||||
import re
|
||||
|
||||
MUL_PATTERN = re.compile(r"mul\((\d+), *(\d+)\)")
|
||||
def task1(text: str) -> int:
|
||||
total = 0
|
||||
for mul in MUL_PATTERN.finditer(text):
|
||||
a, b = map(int, mul.groups())
|
||||
total += a*b
|
||||
return total
|
||||
|
||||
MUL_OR_DO_PATTERN = re.compile(r"do\(\)|don't\(\)|mul\((\d+), *(\d+)\)")
|
||||
def task2(text: str) -> int:
|
||||
total = 0
|
||||
on = True
|
||||
for op in MUL_OR_DO_PATTERN.finditer(text):
|
||||
if op.group(0) == "do()":
|
||||
on = True
|
||||
if op.group(0) == "don't()":
|
||||
on = False
|
||||
if op.group(1) and on:
|
||||
a, b = map(int, op.groups())
|
||||
total += a*b
|
||||
return total
|
||||
|
||||
def main():
|
||||
with open("i3.txt") as f:
|
||||
text = f.read()
|
||||
|
||||
print(task1(text))
|
||||
print(task2(text))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
79
q4.py
Normal file
79
q4.py
Normal file
@ -0,0 +1,79 @@
|
||||
from typing import Iterable
|
||||
|
||||
WORD = "XMAS"
|
||||
def words_from_grid(grid, startx, starty) -> Iterable[str]:
|
||||
num_rows = len(grid)
|
||||
num_cols = len(grid[0])
|
||||
|
||||
for (dx, dy) in (
|
||||
(1, 0), (-1, 0),
|
||||
(0, 1), (0, -1),
|
||||
(1, 1), (-1, 1),
|
||||
(1, -1), (-1, -1)
|
||||
):
|
||||
x = startx
|
||||
y = starty
|
||||
word = ""
|
||||
for i in range(len(WORD)):
|
||||
if x >= num_cols or x < 0:
|
||||
continue
|
||||
if y >= num_rows or y < 0:
|
||||
continue
|
||||
|
||||
word += grid[y][x]
|
||||
x += dx
|
||||
y += dy
|
||||
|
||||
yield word
|
||||
|
||||
def cross_words_from_grid(grid, startx, starty) -> tuple[str, str]:
|
||||
num_rows = len(grid)
|
||||
num_cols = len(grid[0])
|
||||
|
||||
output = []
|
||||
for (dx, dy) in (
|
||||
(1, 1), (1, -1)
|
||||
):
|
||||
x = startx - dx
|
||||
y = starty - dy
|
||||
word = ""
|
||||
for _ in range(3):
|
||||
if x >= num_cols or x < 0:
|
||||
continue
|
||||
if y >= num_rows or y < 0:
|
||||
continue
|
||||
|
||||
word += grid[y][x]
|
||||
x += dx
|
||||
y += dy
|
||||
output.append(word)
|
||||
# o: tuple[str, str] =
|
||||
return tuple(output)
|
||||
|
||||
def task1(grid: list[str]) -> int:
|
||||
count = 0
|
||||
for y in range(len(grid)):
|
||||
for x in range(len(grid[0])):
|
||||
for word in words_from_grid(grid, x, y):
|
||||
if word == WORD or word == str(reversed(WORD)):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def task2(grid: list[str]) -> int:
|
||||
count = 0
|
||||
for y in range(len(grid)):
|
||||
for x in range(len(grid[0])):
|
||||
leading, anti = cross_words_from_grid(grid, x, y)
|
||||
if (leading == "MAS" or leading == "SAM") and (anti == "MAS" or anti == "SAM"):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
# I can't believe I got both parts first try... this should've been a very error-prone episode!
|
||||
def main():
|
||||
with open("i4.txt") as f:
|
||||
text = f.read().splitlines()
|
||||
print(task1(text))
|
||||
print(task2(text))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user