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() # (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 == "^":
# === Parsing ===
from collections import defaultdict
illegal_before = defaultdict(set)
print_orders = []
with open("input.txt", "r") as f:
for line in iter(f.readline, "\n"):
key, value = line.split("|")
illegal_before[int(key)].add(int(value))
# === Parsing ===
with open("input.txt", "r") as f:
grid = [line.strip() for line in f]
print(grid[0])
# === Part 1 ===
# === Parsing ===
import pathlib
contents = pathlib.Path("input.txt").read_text()
"""
with open("input.txt", "r") as f:
contents = f.read()
"""
# === Parsing ===
reports = []
with open("input.txt", "r") as f:
reports = [
[int(num) for num in line.split()]
for line in f
]
print(reports)
# === Parsing ===
left = []
right = []
with open("input.txt") as f:
for line in f:
l, r = map(int, line.split())
left.append(l)
right.append(r)
@rodrigogiraoserrao
rodrigogiraoserrao / sudoku.py
Last active December 4, 2024 17:50
Sudoku board abstraction to simplify teaching a basic search algorithm to solve a Sudoku puzzle
# https://mathspp.com/blog
from dataclasses import dataclass
@dataclass
class Cell:
"""Class that represents a Sudoku board cell."""
row: int
@rodrigogiraoserrao
rodrigogiraoserrao / logdict.py
Created September 3, 2021 13:31
A simple Python🐍 3.8+ `dict` subclass that prints a debug message before setting/getting values.
class LogDict(dict):
def __setitem__(self, key, value):
print(f"Setting {key = } with {value = }")
return super().__setitem__(key, value)
def __getitem__(self, key):
print(f"Getting {key = }")
return super().__getitem__(key)
@rodrigogiraoserrao
rodrigogiraoserrao / ocalc.py
Last active July 13, 2021 12:16
One-expression calculator.
# A Tkinter calculator in a single Python *expression*
# See https://www.reddit.com/r/Python/comments/ojab0n/i_see_your_17_loc_calculator_and_i_raise_you_a/
(tk := __import__("tkinter")) and (bt_draw := lambda k, c, l: (bt := tk.Button(w, text=k, command=lambda: bt_press(k), width=6)) and bt.grid(column=c, row=l)) and (update := lambda t: d.config(text=t)) and (bt_press := lambda k: update("") if k == "C" else update(d["text"][:-1]) if k == "<" else update(str(round(eval(d["text"]), 6))) if k == "=" else update(d["text"] + k)) and (w := tk.Tk()) and w.title("TKalc") or (d := tk.Label(w, text="")) and d.grid(column=0, row=0, columnspan=5) or [bt_draw(k, n%4 + 1, n//4 + 1) for n, k in enumerate("()C<789/456*123-.0=+")] and w.mainloop()
\documentclass[11pt]{article}
\usepackage{fontspec}
\setmainfont{APL385 Unicode}
\setmonofont{APL385 Unicode}[Scale=MatchLowercase]
\begin{document}
I just want some ← +-×÷*⍟⌹○!? |⌈⌊⊥⊤⊣⊢ =≠≤<>≥≡≢ ∨∧⍲⍱ ↑↓⊂⊃⊆⌷⍋⍒ ⍳⍸∊⍷∪∩~ /\textbackslash⌿⍀ ,⍪⍴⌽⊖⍉ ¨⍨⍣.∘⍤⍥@ ⍞⎕⍠⌸⌺⌶⍎⍕ ⋄⍝→⍵⍺∇\& ¯⍬