Solve 2015 days 11-18.
This commit is contained in:
34
2015/d15.py
Normal file
34
2015/d15.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import lib
|
||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
part_1 = True
|
||||||
|
def maximize(tspr, score, ks):
|
||||||
|
if ks == [] or tspr == 0:
|
||||||
|
r = 1
|
||||||
|
for s in score[:-1]:
|
||||||
|
if s < 0:
|
||||||
|
return 0
|
||||||
|
r *= s
|
||||||
|
if part_1:
|
||||||
|
return r
|
||||||
|
elif score[-1] == 500:
|
||||||
|
return r
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
m = 0
|
||||||
|
for tsp in range(tspr + 1):
|
||||||
|
dscore = [v * tsp for v in ks[0]]
|
||||||
|
nscore = list(map(sum, zip(score, dscore)))
|
||||||
|
nm = maximize(tspr - tsp, nscore, ks[1:])
|
||||||
|
m = max(m, nm)
|
||||||
|
return m
|
||||||
|
|
||||||
|
ks = []
|
||||||
|
for line in data.splitlines():
|
||||||
|
k = lib.str_to_ints(line)
|
||||||
|
ks.append(k)
|
||||||
|
|
||||||
|
score = [0 for _ in range(len(ks[0]))]
|
||||||
|
print(maximize(100, score, ks))
|
||||||
|
|
||||||
48
2015/d16.py
Normal file
48
2015/d16.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import re
|
||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
target = {
|
||||||
|
"children": 3,
|
||||||
|
"cats": 7,
|
||||||
|
"samoyeds": 2,
|
||||||
|
"pomeranians": 3,
|
||||||
|
"akitas": 0,
|
||||||
|
"vizslas": 0,
|
||||||
|
"goldfish": 5,
|
||||||
|
"trees": 3,
|
||||||
|
"cars": 2,
|
||||||
|
"perfumes": 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
part_1 = False
|
||||||
|
for i, line in enumerate(data.splitlines()):
|
||||||
|
_, r = re.split(r"\d+: ", line)
|
||||||
|
d = {"index": i + 1}
|
||||||
|
kvs = r.split(", ")
|
||||||
|
for kv in kvs:
|
||||||
|
key, val = kv.split(": ")
|
||||||
|
val = int(val)
|
||||||
|
d[key] = val
|
||||||
|
|
||||||
|
if part_1:
|
||||||
|
for k, v in d.items():
|
||||||
|
if k in target and target[k] != v:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(d['index'])
|
||||||
|
else:
|
||||||
|
for k, v in d.items():
|
||||||
|
if k in target and (k == "cats" or k == "trees"):
|
||||||
|
if v > target[k]:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
elif k in target and (k == "pomeranians" or k == "goldfish"):
|
||||||
|
if v < target[k]:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
elif k in target and target[k] != v:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(d['index'])
|
||||||
26
2015/d17.py
Normal file
26
2015/d17.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from itertools import combinations
|
||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
cons = list(map(int, data.splitlines()))
|
||||||
|
part_1 = False
|
||||||
|
if part_1:
|
||||||
|
def count(n, cons):
|
||||||
|
if n == 0:
|
||||||
|
return 1
|
||||||
|
elif len(cons) == 0:
|
||||||
|
return 0
|
||||||
|
r = 0
|
||||||
|
first = cons[0]
|
||||||
|
r += count(n - first, cons[1:])
|
||||||
|
r += count(n, cons[1:])
|
||||||
|
return r
|
||||||
|
print(count(150, cons))
|
||||||
|
else:
|
||||||
|
r = 0
|
||||||
|
for i in range(1, len(cons) + 1):
|
||||||
|
for c in combinations(cons, i):
|
||||||
|
if sum(c) == 150:
|
||||||
|
r += 1
|
||||||
|
if r > 0:
|
||||||
|
print(i)
|
||||||
|
break
|
||||||
40
2015/d18.py
Normal file
40
2015/d18.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
part_2 = False
|
||||||
|
rows = list(map(str, data.splitlines()))
|
||||||
|
nrows = len(rows)
|
||||||
|
ncols = len(rows[0])
|
||||||
|
|
||||||
|
for _ in range(100):
|
||||||
|
new_field = []
|
||||||
|
for r in range(nrows):
|
||||||
|
nrow = []
|
||||||
|
for c in range(ncols):
|
||||||
|
count = 0
|
||||||
|
for nb in [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]:
|
||||||
|
nr = r + nb[0]
|
||||||
|
nc = c + nb[1]
|
||||||
|
if nr < 0 or nr >= nrows or nc < 0 or nc >= ncols:
|
||||||
|
continue
|
||||||
|
if rows[nr][nc] == "#":
|
||||||
|
count += 1
|
||||||
|
if rows[r][c] == "#" and (count == 2 or count == 3):
|
||||||
|
nrow.append("#")
|
||||||
|
elif rows[r][c] == "." and count == 3:
|
||||||
|
nrow.append("#")
|
||||||
|
else:
|
||||||
|
nrow.append(".")
|
||||||
|
new_field.append(nrow)
|
||||||
|
rows = new_field
|
||||||
|
if part_2:
|
||||||
|
rows[0][0] = "#"
|
||||||
|
rows[0][ncols - 1] = "#"
|
||||||
|
rows[nrows - 1][0] = "#"
|
||||||
|
rows[nrows - 1][ncols - 1] = "#"
|
||||||
|
|
||||||
|
r = 0
|
||||||
|
for row in rows:
|
||||||
|
for c in row:
|
||||||
|
if c == "#":
|
||||||
|
r += 1
|
||||||
|
print(r)
|
||||||
41
2015/d19.py
Normal file
41
2015/d19.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import sys
|
||||||
|
from random import shuffle
|
||||||
|
|
||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
eqs = []
|
||||||
|
text = ""
|
||||||
|
for line in data.splitlines():
|
||||||
|
if "=>" in line:
|
||||||
|
lhs, rhs = line.split(" => ")
|
||||||
|
eqs.append((lhs, rhs))
|
||||||
|
elif line:
|
||||||
|
text = line
|
||||||
|
|
||||||
|
molecules = set()
|
||||||
|
for lhs, rhs in eqs:
|
||||||
|
for i in range(len(text)):
|
||||||
|
f, t = text[:i], text[i:]
|
||||||
|
if t.startswith(lhs):
|
||||||
|
n = f + t.replace(lhs, rhs, 1)
|
||||||
|
molecules.add(n)
|
||||||
|
print(len(molecules))
|
||||||
|
|
||||||
|
# m = float("inf")
|
||||||
|
# for _ in range(100000):
|
||||||
|
# getout = False
|
||||||
|
# c = str(text)
|
||||||
|
# for i in range(10**4):
|
||||||
|
# if i % 1000 == 0:
|
||||||
|
# shuffle(eqs)
|
||||||
|
# for lhs, rhs in eqs:
|
||||||
|
# if rhs in c:
|
||||||
|
# c = c.replace(rhs, lhs, 1)
|
||||||
|
# if c == "e":
|
||||||
|
# if i < m:
|
||||||
|
# m = i
|
||||||
|
# print(i)
|
||||||
|
# getout = True
|
||||||
|
# if getout:
|
||||||
|
# break
|
||||||
|
# print(m)
|
||||||
@@ -19,4 +19,9 @@ written in Python.
|
|||||||
- Day 12: 6:03
|
- Day 12: 6:03
|
||||||
- Day 13: 7:06 (would have been first by a minute, probably 70ish in 2023)
|
- Day 13: 7:06 (would have been first by a minute, probably 70ish in 2023)
|
||||||
- Day 14: 27:45 ... that was weak, logic error for part 2 :/
|
- Day 14: 27:45 ... that was weak, logic error for part 2 :/
|
||||||
- Day 15:
|
- Day 15: 16:00 I should probably stop brute forcing these optimization problems
|
||||||
|
- Day 16: 19:00
|
||||||
|
- Day 17: 9:05
|
||||||
|
- Day 18: 10:39
|
||||||
|
- Day 19:
|
||||||
|
- Day 20:
|
||||||
|
|||||||
Reference in New Issue
Block a user