Skip to content

Instantly share code, notes, and snippets.

View rodrigogiraoserrao's full-sized avatar
🐍

Rodrigo Girão Serrão rodrigogiraoserrao

🐍
View GitHub Profile
# === Parsing ===
obstacles = set()
walls = set()
initial_position = None
with open("input.txt", "r") as f:
for y, line in enumerate(iter(f.readline, "\n")):
for x, char in enumerate(line.strip()):
if char == "@":
initial_position = (x, y)
# === Parsing ===
import re
LINE_RE = re.compile(r"p=(\d+),(\d+) v=(-?\d+),(-?\d+)")
WIDTH = 101
HEIGHT = 103
robots = []
with open("input.txt", "r") as f:
for line in f:
# === Parsing ===
from fractions import Fraction as F
from itertools import batched
import re
BUTTON_RE = re.compile(r"Button .: X\+(\d+), Y\+(\d+)")
PRIZE_RE = re.compile(r"Prize: X=(\d+), Y=(\d+)")
games = []
with open("input.txt", "r") as f:
# === Parsing ===
from collections import defaultdict
garden = defaultdict(str)
with open("input.txt", "r") as f:
for y, line in enumerate(f):
for x, plot in enumerate(line.strip()):
garden[x, y] = plot
WIDTH = x + 1
# === Parsing ===
with open("input.txt", "r") as f:
nums = map(int, f.read().split())
# === Part 1 ===
from collections import Counter
# === Parsing ===
from collections import defaultdict
map = defaultdict(lambda: -1)
with open("input.txt", "r") as f:
for y, line in enumerate(f):
for x, char in enumerate(line.strip()):
map[x, y] = int(char)
MAX_X = x
MAX_Y = y
# === Parsing ===
with open("input.txt", "r") as f:
filesystem_string = f.read().strip()
filesystem = [
[(idx // 2 if idx % 2 == 0 else None, int(char))]
for idx, char in enumerate(filesystem_string)
]
# === Parsing ===
antennas = {}
with open("input.txt", "r") as f:
for y, line in enumerate(f):
for x, char in enumerate(line.strip()):
if char != ".":
antennas.setdefault(char, set()).add((x, y))
max_x = x
# === Parsing ===
equations = []
with open("input.txt", "r") as f:
for line in f:
target, _, operands = line.partition(":")
equations.append(
(
int(target),
[int(num) for num in operands.split()],
# === Parsing ===
obstacles = set() # (x, y) positions of the obstacles.
initial_position = None
with open("input.txt", "r") as f:
for y, line in enumerate(f):
for x, char in enumerate(line):
if char == "#":
obstacles.add((x, y))
elif char == "^":