This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
# indexing of positions is as follows: | |
# 0 1 2 3 4 5 6 7 8 9 10 | |
# 11 12 13 14 | |
# 15 16 17 18 | |
# 19 20 21 22 | |
# 23 24 25 26 | |
ROOMS = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from collections import defaultdict | |
LINE_PATTERN = re.compile( | |
r"(\w+) x=(-?\d+)\.\.(-?\d+),y=(-?\d+)\.\.(-?\d+),z=(-?\d+)\.\.(-?\d+)" | |
) | |
def overlap(cuboid1, cuboid2): | |
x1a, x1b, y1a, y1b, z1a, z1b = cuboid1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import itertools | |
LINE_PATTERN = re.compile( | |
r"(\w+) x=(-?\d+)\.\.(-?\d+),y=(-?\d+)\.\.(-?\d+),z=(-?\d+)\.\.(-?\d+)" | |
) | |
def str2r(s1, s2): | |
start, end = int(s1), int(s2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from collections import defaultdict | |
DISPATCHES = {3: 1, 4: 3, 5: 6, 6: 7, 7: 6, 8: 3, 9: 1} | |
inp = open("input_21.txt").read().strip() | |
pos = tuple(map(lambda x: int(x) - 1, re.findall(r": (\d+)", inp))) | |
ucnts = defaultdict(int) | |
ucnts[((pos[0], 0), (pos[1], 0))] = 1 # key: ((pos, score), (pos, score)) | |
wins = [0, 0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def turns(): | |
roll = 0 | |
while True: | |
roll += 3 | |
delta = (roll - 2) % 100 + (roll - 1) % 100 + roll % 100 | |
yield delta, roll |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TRANSLATION = str.maketrans({"#": "1", ".": "0"}) | |
def index(y, x, img): | |
bitstr = "".join( | |
img[yy][xx] for yy in (y - 1, y, y + 1) for xx in (x - 1, x, x + 1) | |
).translate(TRANSLATION) | |
return int(bitstr, 2) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
My solution for Advent of Code year 2021 day 19 | |
(https://adventofcode.com/2021/day/19). | |
The algorithm reduces the search space by filtering scanner pairs based on | |
inter beacon distances. The reasoning is as follows: let a 'valid pair' be a | |
set of two scanners having 12 or more overlapping beacons. Now, if two scanners | |
form a valid pair, they will also see equal distances within the set of | |
overlapping beacons. This is because distances are invariant under rotations | |
and translations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def add(n1, n2): | |
return reduction([n1, n2]) | |
def reduction(num): | |
while True: | |
_, num, _, exploded = ignite(num) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is essentially @benediktwerner's solution. I only did a few | |
renames and refactoring to get to understand how it works. | |
""" | |
import json | |
import functools | |
def add(n1, n2): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
inp = open("input_17.txt").read().strip() | |
xmin, xmax, ymin, ymax = map(int, re.findall(r"([+-]?\d+)", inp)) | |
gtop = 0 | |
for (vx, vy) in ( | |
(a, b) | |
for a in range(1, xmax + 1) |
NewerOlder