Finish 2015.
This commit is contained in:
52
2015/d19.py
52
2015/d19.py
@@ -1,41 +1,41 @@
|
|||||||
import sys
|
with open("i19.txt", "r") as f:
|
||||||
from random import shuffle
|
data = f.read()
|
||||||
|
|
||||||
data = open(0).read().strip()
|
|
||||||
|
|
||||||
eqs = []
|
eqs = []
|
||||||
text = ""
|
molecule = ""
|
||||||
for line in data.splitlines():
|
for line in data.splitlines():
|
||||||
if "=>" in line:
|
if "=>" in line:
|
||||||
lhs, rhs = line.split(" => ")
|
lhs, rhs = line.split(" => ")
|
||||||
eqs.append((lhs, rhs))
|
eqs.append((lhs, rhs))
|
||||||
elif line:
|
elif line:
|
||||||
text = line
|
molecule = line.strip()
|
||||||
|
|
||||||
molecules = set()
|
molecules = set()
|
||||||
for lhs, rhs in eqs:
|
for lhs, rhs in eqs:
|
||||||
for i in range(len(text)):
|
for i in range(len(molecule)):
|
||||||
f, t = text[:i], text[i:]
|
f, t = molecule[:i], molecule[i:]
|
||||||
if t.startswith(lhs):
|
if t.startswith(lhs):
|
||||||
n = f + t.replace(lhs, rhs, 1)
|
n = f + t.replace(lhs, rhs, 1)
|
||||||
molecules.add(n)
|
molecules.add(n)
|
||||||
print(len(molecules))
|
print(len(molecules))
|
||||||
|
|
||||||
# m = float("inf")
|
|
||||||
# for _ in range(100000):
|
# It seems like the problem input is specifically designed so that reversing
|
||||||
# getout = False
|
# the molecule back to 'e' works by simply replacing RHS with LHS continuously.
|
||||||
# c = str(text)
|
# Intuitively, it seems like this should not work in many cases or at least not
|
||||||
# for i in range(10**4):
|
# result in the shortest sequence. Probably I am just salty that it took me so
|
||||||
# if i % 1000 == 0:
|
# long to find this trivial solution.
|
||||||
# shuffle(eqs)
|
x = molecule
|
||||||
# for lhs, rhs in eqs:
|
count = 0
|
||||||
# if rhs in c:
|
while len(x) > 1:
|
||||||
# c = c.replace(rhs, lhs, 1)
|
old_len = len(x)
|
||||||
# if c == "e":
|
for lhs, rhs in eqs:
|
||||||
# if i < m:
|
if rhs in x:
|
||||||
# m = i
|
x = x.replace(rhs, lhs, 1)
|
||||||
# print(i)
|
count += 1
|
||||||
# getout = True
|
break
|
||||||
# if getout:
|
else:
|
||||||
# break
|
break
|
||||||
# print(m)
|
|
||||||
|
assert x == 'e'
|
||||||
|
print(count)
|
||||||
|
|||||||
40
2015/d23.py
Normal file
40
2015/d23.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
insts = []
|
||||||
|
with open("i23.txt", "r") as f:
|
||||||
|
for line in f:
|
||||||
|
args = line.strip().replace(",", "").split()
|
||||||
|
insts.append(args)
|
||||||
|
|
||||||
|
part_2 = True
|
||||||
|
i = 0
|
||||||
|
regs = {"a": 0, "b": 0}
|
||||||
|
if part_2:
|
||||||
|
regs["a"] = 1
|
||||||
|
|
||||||
|
while i < len(insts):
|
||||||
|
inst = insts[i]
|
||||||
|
match inst:
|
||||||
|
case ["inc", reg]:
|
||||||
|
regs[reg] += 1
|
||||||
|
case ["hlf", reg]:
|
||||||
|
regs[reg] //= 2
|
||||||
|
case ["tpl", reg]:
|
||||||
|
regs[reg] *= 3
|
||||||
|
case ["jio", reg, offset]:
|
||||||
|
if regs[reg] == 1:
|
||||||
|
i += int(offset)
|
||||||
|
continue
|
||||||
|
case ["jie", reg, offset]:
|
||||||
|
offset = int(offset)
|
||||||
|
if regs[reg] % 2 == 0:
|
||||||
|
i += int(offset)
|
||||||
|
continue
|
||||||
|
case ["jmp", offset]:
|
||||||
|
i += int(offset)
|
||||||
|
continue
|
||||||
|
case _:
|
||||||
|
print(inst)
|
||||||
|
assert False
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
print(regs["b"])
|
||||||
46
2015/d24.py
Normal file
46
2015/d24.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import sys
|
||||||
|
from itertools import combinations
|
||||||
|
|
||||||
|
with open("i24.txt", "r") as f:
|
||||||
|
weights = list(map(int, f.readlines()))
|
||||||
|
|
||||||
|
total_sum = sum(weights)
|
||||||
|
|
||||||
|
indices = list(range(len(weights)))
|
||||||
|
|
||||||
|
lowest = None
|
||||||
|
def find(part_1=True):
|
||||||
|
if part_1:
|
||||||
|
target_weight = total_sum // 3
|
||||||
|
else:
|
||||||
|
target_weight = total_sum // 4
|
||||||
|
for group_1_size in range(1, len(weights) - 1):
|
||||||
|
for group_1_indices in combinations(indices, group_1_size):
|
||||||
|
group_1_weight = sum(weights[i] for i in group_1_indices)
|
||||||
|
if not group_1_weight == target_weight:
|
||||||
|
continue
|
||||||
|
remaining_indices = list(set(indices) - set(group_1_indices))
|
||||||
|
for group_2_size in range(1, len(remaining_indices) - 1):
|
||||||
|
for group_2_indices in combinations(remaining_indices, group_2_size):
|
||||||
|
group_2_weight = sum(weights[i] for i in group_2_indices)
|
||||||
|
if not group_2_weight == target_weight:
|
||||||
|
continue
|
||||||
|
if part_1 :
|
||||||
|
r = 1
|
||||||
|
for i in group_1_indices:
|
||||||
|
r *= weights[i]
|
||||||
|
return r
|
||||||
|
else:
|
||||||
|
remaining_indices = list(set(indices) - set(group_1_indices))
|
||||||
|
for group_3_size in range(1, len(remaining_indices)- 1):
|
||||||
|
for group_3_indices in combinations(remaining_indices, group_3_size):
|
||||||
|
group_3_weight = sum(weights[i] for i in group_3_indices)
|
||||||
|
if not group_3_weight == target_weight:
|
||||||
|
continue
|
||||||
|
r = 1
|
||||||
|
for i in group_1_indices:
|
||||||
|
r *= weights[i]
|
||||||
|
return r
|
||||||
|
|
||||||
|
print(find(True))
|
||||||
|
print(find(False))
|
||||||
19
2015/d25.py
Normal file
19
2015/d25.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import sys
|
||||||
|
from lib import str_to_ints
|
||||||
|
|
||||||
|
with open("i25.txt", "r") as f:
|
||||||
|
target_row, target_col = str_to_ints(f.read())
|
||||||
|
|
||||||
|
x = 20151125
|
||||||
|
m = 252533
|
||||||
|
d = 33554393
|
||||||
|
|
||||||
|
for start_row in range(1, 10000):
|
||||||
|
row, col = start_row, 1
|
||||||
|
while row > 0:
|
||||||
|
if row == target_row and col == target_col:
|
||||||
|
print(x)
|
||||||
|
sys.exit(0)
|
||||||
|
x = (x * m) % d
|
||||||
|
row -= 1
|
||||||
|
col += 1
|
||||||
@@ -23,12 +23,12 @@ written in Python.
|
|||||||
- Day 16: 19:00
|
- Day 16: 19:00
|
||||||
- Day 17: 9:05
|
- Day 17: 9:05
|
||||||
- Day 18: 10:39
|
- Day 18: 10:39
|
||||||
- Day 19:
|
- Day 19: Many days... yeah this one took me way too long to figure out
|
||||||
- Day 20: 10:54
|
- Day 20: 10:54
|
||||||
- Day 21: 25:52 cute bug where I didn't consider that no armor is an option
|
- Day 21: 25:52 cute bug where I didn't consider that no armor is an option
|
||||||
- Day 22: That was bad. Did not know how to choose between dfs/bfs and logic errors.
|
- Day 22: That was bad. Did not know how to choose between dfs/bfs and logic errors.
|
||||||
- Day 23:
|
- Day 23: 10:00
|
||||||
- Day 24:
|
- Day 24: 20:00 ugly - recursive solution would be more elegant
|
||||||
- Day 25:
|
- Day 25:
|
||||||
|
|
||||||
# 2016
|
# 2016
|
||||||
|
|||||||
Reference in New Issue
Block a user