Created
January 30, 2021 08:12
-
-
Save jackdreilly/80774c132fccbe3e64b9f4f808f7c587 to your computer and use it in GitHub Desktop.
This file contains 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
from parse import parse | |
store = {} | |
def looper(ander, orer, child): | |
if not len(child): | |
yield tuple(int("0b" + x, 2) for x in (ander, orer)) | |
return | |
letter, rest = child[0], child[1:] | |
if letter == "X": | |
for b in ("0", "1"): | |
yield from looper(ander + b, orer + b, rest) | |
return | |
yield from looper(ander + "1", orer + letter, rest) | |
with open("data/14.txt") as fn: | |
for line in fn.readlines(): | |
r = parse("mem[{}] = {}", line.strip()) | |
if r: | |
i, v = map(int, r) | |
for ander, orer in masks: | |
store[(i & ander) | orer] = v | |
continue | |
masks = list(looper("", "", parse("mask = {}", line.strip())[0])) | |
print(sum(store.values())) |
This file contains 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
starts = [0,13,16,17,1,10,6] | |
nums = {v: k for k, v in enumerate(starts[:-1])} | |
last = starts[-1] | |
for i in range(len(nums), 30000000 - 1): | |
next = 0 if last not in nums else i - nums[last] | |
nums[last] = i | |
last = next | |
print(last) |
This file contains 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
from parse import parse | |
import itertools | |
import math | |
with open("data/16.txt") as fn: | |
ranges = [] | |
mine = None | |
while True: | |
line = fn.readline().strip() | |
if not line: | |
fn.readline() | |
mine = list(map(int, fn.readline().strip().split(","))) | |
fn.readline() | |
fn.readline() | |
break | |
p = parse("{}: {}-{} or {}-{}", line) | |
ranges.append((p[0], list(map(int, p[1:])))) | |
theirs = [list(map(int, line.strip().split(","))) for line in fn.readlines()] | |
keepers = [ | |
x | |
for x in theirs | |
if all(any(a <= y <= b or c <= y <= d for _, (a, b, c, d) in ranges) for y in x) | |
] + [mine] | |
zips = list(zip(*keepers)) | |
candidates = { | |
k: tuple( | |
i for i, vs in enumerate(zips) if all(a <= y <= b or c <= y <= d for y in vs) | |
) | |
for k, (a, b, c, d) in ranges | |
} | |
inverted = { | |
k: {b for _, b in v} | |
for k, v in itertools.groupby( | |
sorted( | |
((v, k) for k, vs in candidates.items() for v in vs), key=lambda a: a[0] | |
), | |
lambda a: a[0], | |
) | |
} | |
sln = {} | |
for k, v in sorted(inverted.items(), key=lambda a: len(a[1])): | |
sln[next(iter(v.difference(set(sln.keys()))))] = k | |
print(math.prod(mine[i] for k, i in sln.items() if "departure" in k)) |
This file contains 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
with open("data/17.txt") as fn: | |
r = fn.read().strip() | |
dim = 5 | |
cubes = { | |
(x, y) + tuple([0] * (dim - 2)) | |
for x, row in enumerate(r.split("\n")) | |
for y, col in enumerate(row) | |
if col == "#" | |
} | |
def perms(i): | |
if not i: | |
yield tuple() | |
return | |
for j in range(-1, 2): | |
for p in perms(i - 1): | |
yield (j, *p) | |
_perms = list(perms(dim)) | |
perms = lambda cube: {tuple(map(sum, zip(cube, perm))) for perm in _perms} | |
for _ in range(6): | |
cubes = { | |
cube | |
for ct, cube in [ | |
(len(perms(cube).intersection(cubes)), cube) | |
for cube in {perm_cube for cube in cubes for perm_cube in perms(cube)} | |
] | |
if (cube in cubes and ct in {3, 4}) or ct == 3 | |
} | |
print(len(cubes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment