Skip to content

Instantly share code, notes, and snippets.

View Mu-adventofcode's full-sized avatar

Mu Mu-adventofcode

  • The Netherlands
View GitHub Profile
@Mu-adventofcode
Mu-adventofcode / AoC_2021_23_2.py
Last active July 24, 2022 17:19
Advent of Code day 23 part 2
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 = {
@Mu-adventofcode
Mu-adventofcode / AoC_2021_22_2.py
Created July 9, 2022 09:39
Advent of Code day 22 part 2
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
@Mu-adventofcode
Mu-adventofcode / AoC_2021_22_1.py
Last active July 3, 2022 09:25
Advent of Code 2021 day 22 part 1
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)
@Mu-adventofcode
Mu-adventofcode / AoC_2021_21_2.py
Last active July 2, 2022 17:01
Advent of Code 2021 day 21 part 2
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]
@Mu-adventofcode
Mu-adventofcode / AoC_2021_21_1.py
Last active July 2, 2022 16:40
Advent of Code 2021 day 21 part 1
import re
def turns():
roll = 0
while True:
roll += 3
delta = (roll - 2) % 100 + (roll - 1) % 100 + roll % 100
yield delta, roll
@Mu-adventofcode
Mu-adventofcode / AoC_2021_20.py
Created June 23, 2022 17:24
Advent of Code 2021 day 20
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)
@Mu-adventofcode
Mu-adventofcode / AoC_2021_19.py
Last active June 21, 2022 05:19
Advent of Code 2021 day 19
"""
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.
@Mu-adventofcode
Mu-adventofcode / AoC_2021_18_2.py
Last active June 8, 2022 19:46
Advent of Code 2021 day 18 part 2
import json
def add(n1, n2):
return reduction([n1, n2])
def reduction(num):
while True:
_, num, _, exploded = ignite(num)
@Mu-adventofcode
Mu-adventofcode / AoC_2021_18_1.py
Created June 8, 2022 19:12
Advent of Code day 18 part 1
"""
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):
@Mu-adventofcode
Mu-adventofcode / AoC_2021_17_1_simple.py
Last active May 30, 2022 08:07
AoC day 17 part 1 simple
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)