Compare commits
2 Commits
4e6a44f67b
...
78b1981ddf
| Author | SHA1 | Date | |
|---|---|---|---|
| 78b1981ddf | |||
| 3c290b8da3 |
32
2016/d18.py
Normal file
32
2016/d18.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from lib import *
|
||||
|
||||
row = open(0).read().strip()
|
||||
|
||||
def is_trap(s):
|
||||
assert len(s) == 3
|
||||
match s:
|
||||
case "^^.":
|
||||
return "^"
|
||||
case ".^^":
|
||||
return "^"
|
||||
case "^..":
|
||||
return "^"
|
||||
case "..^":
|
||||
return "^"
|
||||
case _:
|
||||
return "."
|
||||
|
||||
# 400_000 is still easily bruteforcible. If it was a much larget number, we
|
||||
# would have to cache the rows till we get a repeated row. We would then
|
||||
# memorize the number of safe tiles from repeated row to repeated row and jump
|
||||
# forward by multiples of that amount to reach much higher numbers.
|
||||
r = 0
|
||||
for _ in range(400_000):
|
||||
r += row.count(".")
|
||||
nrow = is_trap("." + row[:2])
|
||||
for i in range(1, len(row) - 1):
|
||||
nrow += is_trap(row[i-1:i+2])
|
||||
nrow += is_trap(row[-2:] + ".")
|
||||
row = nrow
|
||||
|
||||
print(r)
|
||||
18
2016/d19.py
Normal file
18
2016/d19.py
Normal file
@@ -0,0 +1,18 @@
|
||||
data = open(0).read().strip()
|
||||
elf_count = int(data)
|
||||
|
||||
elfs = [i for i in range(1, elf_count + 1)]
|
||||
while len(elfs) > 1:
|
||||
is_odd = len(elfs) % 2 == 1
|
||||
elfs = [elfs[i] for i in range(0, len(elfs), 2)]
|
||||
if is_odd and len(elfs) > 1:
|
||||
elfs = elfs[1:]
|
||||
print(elfs[0])
|
||||
|
||||
# elfs = [i for i in range(1, elf_count + 1)]
|
||||
# current_i = 0
|
||||
# while len(elfs) > 1:
|
||||
# remove_i = (current_i + len(elfs) // 2) % len(elfs)
|
||||
# current_i = (current_i + 1) % len(elfs)
|
||||
# elfs.pop(remove_i)
|
||||
# print(elfs[0])
|
||||
16
2017/d4.py
Normal file
16
2017/d4.py
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
data = open(0).read().strip()
|
||||
|
||||
part_2 = False
|
||||
r = 0
|
||||
words = []
|
||||
for line in data.splitlines():
|
||||
if part_2:
|
||||
words = list(map(lambda w: "".join(sorted(w)), line.split()))
|
||||
else:
|
||||
words = line.split()
|
||||
if len(words) == len(set(words)):
|
||||
r += 1
|
||||
|
||||
print(r)
|
||||
17
2017/d5.py
Normal file
17
2017/d5.py
Normal file
@@ -0,0 +1,17 @@
|
||||
insts = list(map(int, open(0).read().strip().splitlines()))
|
||||
part_2 = True
|
||||
c = 0
|
||||
i = 0
|
||||
while i < len(insts):
|
||||
ci = i
|
||||
i += insts[i]
|
||||
if part_2:
|
||||
if insts[ci] >= 3:
|
||||
insts[ci] -= 1
|
||||
else:
|
||||
insts[ci] += 1
|
||||
else:
|
||||
insts[ci] += 1
|
||||
c += 1
|
||||
print(c)
|
||||
|
||||
27
2017/d6.py
Normal file
27
2017/d6.py
Normal file
@@ -0,0 +1,27 @@
|
||||
data = open(0).read().strip()
|
||||
|
||||
# data = "0 2 7 0"
|
||||
banks = list(map(int, data.split()))
|
||||
seen = dict()
|
||||
seen[tuple(banks)] = 0
|
||||
|
||||
c = 0
|
||||
while True:
|
||||
c += 1
|
||||
max_index = banks.index(max(banks))
|
||||
blocks = banks[max_index]
|
||||
banks[max_index] = 0
|
||||
index = max_index
|
||||
while blocks > 0:
|
||||
index = (index + 1) % len(banks)
|
||||
if index == max_index:
|
||||
continue
|
||||
banks[index] += 1
|
||||
blocks -= 1
|
||||
|
||||
if tuple(banks) in seen:
|
||||
break
|
||||
seen[tuple(banks)] = c
|
||||
|
||||
print(c)
|
||||
print(c - seen[tuple(banks)])
|
||||
63
2017/d7.py
Normal file
63
2017/d7.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from dataclasses import dataclass
|
||||
from collections import defaultdict
|
||||
import sys
|
||||
|
||||
|
||||
@dataclass
|
||||
class Disc:
|
||||
name: str
|
||||
value: int
|
||||
holds: list
|
||||
|
||||
|
||||
discs = {}
|
||||
data = open(0).read().strip()
|
||||
|
||||
|
||||
for line in data.splitlines():
|
||||
right = None
|
||||
if "->" in line:
|
||||
left, right = line.split(" -> ")
|
||||
else:
|
||||
left = line
|
||||
name, value = left.split()
|
||||
value = int(value[1:-1])
|
||||
d = Disc(name, value, [])
|
||||
if right is not None:
|
||||
for r in right.split(", "):
|
||||
d.holds.append(r)
|
||||
discs[d.name] = d
|
||||
|
||||
|
||||
alldiscs = set(discs.keys())
|
||||
for d in discs.values():
|
||||
for h in d.holds:
|
||||
alldiscs.remove(h)
|
||||
assert len(alldiscs) == 1
|
||||
top_disc = alldiscs.pop()
|
||||
print(top_disc)
|
||||
|
||||
|
||||
def weight(disc_name):
|
||||
disc = discs[disc_name]
|
||||
child_weights = {}
|
||||
for child_disc_name in disc.holds:
|
||||
child_weights[child_disc_name] = weight(child_disc_name)
|
||||
|
||||
if not all([w == list(child_weights.values())[0] for w in child_weights.values()]):
|
||||
values = defaultdict(list)
|
||||
for child_name, child_value in child_weights.items():
|
||||
values[child_value].append(child_name)
|
||||
|
||||
same_weight, other_weight = 0, 0
|
||||
for child_weight, children in values.items():
|
||||
if len(children) == 1:
|
||||
other_weight = child_weight
|
||||
else:
|
||||
same_weight = child_weight
|
||||
weight_delta = other_weight - same_weight
|
||||
print(discs['dqwocyn'].value - weight_delta)
|
||||
sys.exit(0)
|
||||
return disc.value + sum(child_weights.values())
|
||||
|
||||
weight(top_disc)
|
||||
12
README.md
12
README.md
@@ -29,7 +29,7 @@ written in Python.
|
||||
- Day 22: That was bad. Did not know how to choose between dfs/bfs and logic errors.
|
||||
- Day 23: 10:00
|
||||
- Day 24: 20:00 ugly - recursive solution would be more elegant
|
||||
- Day 25:
|
||||
- Day 25: 9:34
|
||||
|
||||
# 2016
|
||||
|
||||
@@ -50,11 +50,17 @@ written in Python.
|
||||
- Day 15: Trial and error. Should use CRT instead.
|
||||
- Day 16: 14:11
|
||||
- Day 17: 45:00 didn't follow instructions... focus!
|
||||
- Day 18:
|
||||
- Day 18: 9:43
|
||||
- Day 19:
|
||||
|
||||
|
||||
# 2017
|
||||
|
||||
- Day 1: 7:30
|
||||
- Day 2: 6:15
|
||||
- Day 3: 75:00 hmm should have been way quicker
|
||||
- Day 4:
|
||||
- Day 4: 3:02
|
||||
- Day 5: 6:13
|
||||
- Day 6: 8:37
|
||||
- Day 7: 19:22
|
||||
- Day 8:
|
||||
|
||||
Reference in New Issue
Block a user