Compare commits
No commits in common. "7eb1ddac9bba06cacea55365079dcda91b039e11" and "e2c1c14b3d972cebc8ee854063da27f7a083be71" have entirely different histories.
7eb1ddac9b
...
e2c1c14b3d
49
q2.py
49
q2.py
@ -1,49 +0,0 @@
|
|||||||
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
33
q3.py
@ -1,33 +0,0 @@
|
|||||||
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
79
q4.py
@ -1,79 +0,0 @@
|
|||||||
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