2017 progress.
This commit is contained in:
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)
|
||||||
Reference in New Issue
Block a user