Add day 1-5
This commit is contained in:
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
filename = "example.txt"
|
||||
filename = "input.txt"
|
||||
|
||||
with open(filename) as f:
|
||||
input = f.readlines()
|
||||
|
||||
board = [
|
||||
[letter for letter in line.strip()]
|
||||
for line in input
|
||||
]
|
||||
|
||||
y_dim = len(board)
|
||||
x_dim = len(board[0])
|
||||
|
||||
valids = []
|
||||
|
||||
do_print = False
|
||||
|
||||
def print_board(b, indexes_to_print=[]):
|
||||
if not do_print:
|
||||
return
|
||||
|
||||
for y in range(y_dim):
|
||||
for x in range(x_dim):
|
||||
if indexes_to_print:
|
||||
if (y, x) in indexes_to_print:
|
||||
print(b[y][x], end="")
|
||||
else:
|
||||
print(".", end="")
|
||||
else:
|
||||
print(b[y][x], end="")
|
||||
print()
|
||||
print()
|
||||
|
||||
print_board(board)
|
||||
|
||||
def is_xmas(*args):
|
||||
a = board[args[0][0]][args[0][1]]
|
||||
b = board[args[1][0]][args[1][1]]
|
||||
c = board[args[2][0]][args[2][1]]
|
||||
d = board[args[3][0]][args[3][1]]
|
||||
if f"{a}{b}{c}{d}" == "XMAS":
|
||||
print_board(board, args)
|
||||
valids.append(args)
|
||||
return True
|
||||
return False
|
||||
|
||||
count = 0
|
||||
|
||||
for y in range(y_dim):
|
||||
for x in range(x_dim):
|
||||
# left (backwards)
|
||||
if x > 2:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y, x - 1),
|
||||
(y, x - 2),
|
||||
(y, x - 3),
|
||||
)
|
||||
# right
|
||||
if x_dim - x > 3:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y, x + 1),
|
||||
(y, x + 2),
|
||||
(y, x + 3),
|
||||
)
|
||||
# up
|
||||
if y > 2:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y - 1, x),
|
||||
(y - 2, x),
|
||||
(y - 3, x),
|
||||
)
|
||||
# down
|
||||
if y_dim - y > 3:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y + 1, x),
|
||||
(y + 2, x),
|
||||
(y + 3, x),
|
||||
)
|
||||
# diagonal up left
|
||||
if x > 2 and y > 2:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y - 1, x - 1),
|
||||
(y - 2, x - 2),
|
||||
(y - 3, x - 3),
|
||||
)
|
||||
# diagonal up right
|
||||
if x_dim - x > 3 and y > 2:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y - 1, x + 1),
|
||||
(y - 2, x + 2),
|
||||
(y - 3, x + 3),
|
||||
)
|
||||
# diagonal down left
|
||||
if x > 2 and y_dim - y > 3:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y + 1, x - 1),
|
||||
(y + 2, x - 2),
|
||||
(y + 3, x - 3),
|
||||
)
|
||||
# diagonal down right
|
||||
if x_dim - x > 3 and y_dim - y > 3:
|
||||
count = count + is_xmas(
|
||||
(y, x),
|
||||
(y + 1, x + 1),
|
||||
(y + 2, x + 2),
|
||||
(y + 3, x + 3),
|
||||
)
|
||||
|
||||
|
||||
print_board(board, [x for xs in valids for x in xs])
|
||||
|
||||
print(count)
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX
|
||||
@@ -0,0 +1,14 @@
|
||||
[tool.poetry]
|
||||
name = "aoc"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["restitux <restitux@ohea.xyz>"]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.12"
|
||||
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
Reference in New Issue
Block a user