days 7 and 8
This commit is contained in:
parent
1446ca039e
commit
eec8b960ea
87
2018/07.py
87
2018/07.py
@ -1,11 +1,82 @@
|
|||||||
class BuildTree:
|
from collections import defaultdict
|
||||||
def __init__(self):
|
import re
|
||||||
self.tree = {}
|
import string
|
||||||
self.in_tree = set()
|
|
||||||
|
|
||||||
def _add_root(self, item):
|
|
||||||
self.tree[item] = {}
|
|
||||||
|
|
||||||
def _add_leaf(self, leaf, branch):
|
def get_first(steps):
|
||||||
b =
|
for step, deps in steps.items():
|
||||||
|
if len(deps) == 0:
|
||||||
|
return step
|
||||||
|
|
||||||
|
|
||||||
|
with open('data/07.txt') as f:
|
||||||
|
regexp = re.compile('Step (.) must be finished before step (.) can begin.')
|
||||||
|
data = [regexp.match(line).groups() for line in f]
|
||||||
|
|
||||||
|
|
||||||
|
requires = {}
|
||||||
|
for before, after in data:
|
||||||
|
if before not in requires:
|
||||||
|
requires[before] = set()
|
||||||
|
if after not in requires:
|
||||||
|
requires[after] = set()
|
||||||
|
requires[after].add(before)
|
||||||
|
|
||||||
|
workers = []
|
||||||
|
|
||||||
|
|
||||||
|
incomplete = {s: set(d) for s, d in requires.items()}
|
||||||
|
complete = []
|
||||||
|
available = set()
|
||||||
|
while len(incomplete):
|
||||||
|
for step, deps in incomplete.items():
|
||||||
|
if len(deps) == 0:
|
||||||
|
available.add(step)
|
||||||
|
|
||||||
|
s = sorted(available)[0]
|
||||||
|
complete.append(s)
|
||||||
|
for deps in incomplete.values():
|
||||||
|
if s in deps:
|
||||||
|
deps.remove(s)
|
||||||
|
available.remove(s)
|
||||||
|
del incomplete[s]
|
||||||
|
|
||||||
|
print(f'Part 1: {"".join(complete)}\n')
|
||||||
|
|
||||||
|
|
||||||
|
step_times = {c: 61 + i for i, c in enumerate(string.ascii_uppercase)}
|
||||||
|
unattempted = {s: set(d) for s, d in requires.items()}
|
||||||
|
complete = []
|
||||||
|
available = set()
|
||||||
|
running = set()
|
||||||
|
workers = [(None, 0) for i in range(5)]
|
||||||
|
time = -1
|
||||||
|
while len(unattempted) or len(running):
|
||||||
|
time += 1
|
||||||
|
for i, (s, t) in enumerate(workers):
|
||||||
|
if s and (time - step_times[s] - t == 0):
|
||||||
|
complete.append(s)
|
||||||
|
running.remove(s)
|
||||||
|
for deps in unattempted.values():
|
||||||
|
if s in deps:
|
||||||
|
deps.remove(s)
|
||||||
|
workers[i] = (None, 0)
|
||||||
|
|
||||||
|
for step, deps in unattempted.items():
|
||||||
|
if len(deps) == 0:
|
||||||
|
available.add(step)
|
||||||
|
|
||||||
|
ordered_steps = sorted(available)
|
||||||
|
for i, (s, t) in enumerate(workers):
|
||||||
|
if s is None:
|
||||||
|
try:
|
||||||
|
new_step = ordered_steps.pop(0)
|
||||||
|
workers[i] = (new_step, time)
|
||||||
|
del unattempted[new_step]
|
||||||
|
available.remove(new_step)
|
||||||
|
running.add(new_step)
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
print(f'Part 2: {time}\n')
|
||||||
|
46
2018/08.py
Normal file
46
2018/08.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
with open('data/08.txt') as f:
|
||||||
|
data = [int(n) for n in f.read().split()]
|
||||||
|
|
||||||
|
|
||||||
|
all_meta = []
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
(children, meta_count), data = data[:2], data[2:]
|
||||||
|
tree = {
|
||||||
|
'children': [],
|
||||||
|
'metadata': []
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in range(children):
|
||||||
|
child, data = parse(data)
|
||||||
|
tree['children'].append(child)
|
||||||
|
|
||||||
|
for i in range(meta_count):
|
||||||
|
m = data.pop(0)
|
||||||
|
all_meta.append(m)
|
||||||
|
tree['metadata'].append(m)
|
||||||
|
|
||||||
|
return tree, data
|
||||||
|
|
||||||
|
|
||||||
|
tree, _ = parse(data)
|
||||||
|
|
||||||
|
print(f'Part 1: {sum(all_meta)}\n')
|
||||||
|
|
||||||
|
|
||||||
|
def value(node):
|
||||||
|
children, metadata, = node['children'], node['metadata']
|
||||||
|
if len(children) == 0:
|
||||||
|
return sum(metadata)
|
||||||
|
|
||||||
|
child_values = []
|
||||||
|
for i in metadata:
|
||||||
|
if i == 0:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
child_values.append(value(children[i - 1]))
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
return sum(child_values)
|
||||||
|
|
||||||
|
print(f'Part 2: {value(tree)}\n')
|
101
2018/data/07.txt
Normal file
101
2018/data/07.txt
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
Step T must be finished before step X can begin.
|
||||||
|
Step G must be finished before step O can begin.
|
||||||
|
Step X must be finished before step B can begin.
|
||||||
|
Step I must be finished before step W can begin.
|
||||||
|
Step N must be finished before step V can begin.
|
||||||
|
Step K must be finished before step H can begin.
|
||||||
|
Step S must be finished before step R can begin.
|
||||||
|
Step P must be finished before step J can begin.
|
||||||
|
Step L must be finished before step V can begin.
|
||||||
|
Step D must be finished before step E can begin.
|
||||||
|
Step J must be finished before step R can begin.
|
||||||
|
Step U must be finished before step W can begin.
|
||||||
|
Step M must be finished before step Q can begin.
|
||||||
|
Step B must be finished before step F can begin.
|
||||||
|
Step F must be finished before step E can begin.
|
||||||
|
Step V must be finished before step Q can begin.
|
||||||
|
Step C must be finished before step A can begin.
|
||||||
|
Step H must be finished before step Z can begin.
|
||||||
|
Step A must be finished before step Y can begin.
|
||||||
|
Step O must be finished before step Y can begin.
|
||||||
|
Step W must be finished before step Q can begin.
|
||||||
|
Step E must be finished before step Y can begin.
|
||||||
|
Step Y must be finished before step Z can begin.
|
||||||
|
Step Q must be finished before step R can begin.
|
||||||
|
Step R must be finished before step Z can begin.
|
||||||
|
Step S must be finished before step E can begin.
|
||||||
|
Step O must be finished before step W can begin.
|
||||||
|
Step G must be finished before step B can begin.
|
||||||
|
Step I must be finished before step N can begin.
|
||||||
|
Step G must be finished before step I can begin.
|
||||||
|
Step H must be finished before step R can begin.
|
||||||
|
Step N must be finished before step C can begin.
|
||||||
|
Step M must be finished before step W can begin.
|
||||||
|
Step Y must be finished before step R can begin.
|
||||||
|
Step T must be finished before step B can begin.
|
||||||
|
Step G must be finished before step D can begin.
|
||||||
|
Step J must be finished before step O can begin.
|
||||||
|
Step I must be finished before step A can begin.
|
||||||
|
Step J must be finished before step H can begin.
|
||||||
|
Step T must be finished before step Y can begin.
|
||||||
|
Step N must be finished before step H can begin.
|
||||||
|
Step B must be finished before step V can begin.
|
||||||
|
Step M must be finished before step R can begin.
|
||||||
|
Step Y must be finished before step Q can begin.
|
||||||
|
Step X must be finished before step J can begin.
|
||||||
|
Step A must be finished before step E can begin.
|
||||||
|
Step P must be finished before step Z can begin.
|
||||||
|
Step P must be finished before step C can begin.
|
||||||
|
Step N must be finished before step Q can begin.
|
||||||
|
Step A must be finished before step O can begin.
|
||||||
|
Step G must be finished before step X can begin.
|
||||||
|
Step P must be finished before step U can begin.
|
||||||
|
Step T must be finished before step S can begin.
|
||||||
|
Step I must be finished before step V can begin.
|
||||||
|
Step V must be finished before step H can begin.
|
||||||
|
Step U must be finished before step F can begin.
|
||||||
|
Step D must be finished before step Q can begin.
|
||||||
|
Step D must be finished before step O can begin.
|
||||||
|
Step G must be finished before step H can begin.
|
||||||
|
Step I must be finished before step Z can begin.
|
||||||
|
Step N must be finished before step D can begin.
|
||||||
|
Step B must be finished before step Y can begin.
|
||||||
|
Step J must be finished before step M can begin.
|
||||||
|
Step V must be finished before step Y can begin.
|
||||||
|
Step W must be finished before step Y can begin.
|
||||||
|
Step E must be finished before step Z can begin.
|
||||||
|
Step T must be finished before step N can begin.
|
||||||
|
Step L must be finished before step U can begin.
|
||||||
|
Step S must be finished before step A can begin.
|
||||||
|
Step Q must be finished before step Z can begin.
|
||||||
|
Step T must be finished before step F can begin.
|
||||||
|
Step F must be finished before step Z can begin.
|
||||||
|
Step J must be finished before step C can begin.
|
||||||
|
Step X must be finished before step Y can begin.
|
||||||
|
Step K must be finished before step V can begin.
|
||||||
|
Step T must be finished before step I can begin.
|
||||||
|
Step I must be finished before step O can begin.
|
||||||
|
Step C must be finished before step W can begin.
|
||||||
|
Step B must be finished before step Q can begin.
|
||||||
|
Step W must be finished before step Z can begin.
|
||||||
|
Step D must be finished before step H can begin.
|
||||||
|
Step K must be finished before step A can begin.
|
||||||
|
Step M must be finished before step E can begin.
|
||||||
|
Step T must be finished before step U can begin.
|
||||||
|
Step I must be finished before step J can begin.
|
||||||
|
Step O must be finished before step Q can begin.
|
||||||
|
Step M must be finished before step Z can begin.
|
||||||
|
Step U must be finished before step C can begin.
|
||||||
|
Step N must be finished before step F can begin.
|
||||||
|
Step C must be finished before step H can begin.
|
||||||
|
Step X must be finished before step E can begin.
|
||||||
|
Step F must be finished before step O can begin.
|
||||||
|
Step P must be finished before step O can begin.
|
||||||
|
Step J must be finished before step A can begin.
|
||||||
|
Step H must be finished before step Y can begin.
|
||||||
|
Step A must be finished before step Q can begin.
|
||||||
|
Step V must be finished before step Z can begin.
|
||||||
|
Step S must be finished before step L can begin.
|
||||||
|
Step H must be finished before step E can begin.
|
||||||
|
Step X must be finished before step I can begin.
|
||||||
|
Step O must be finished before step R can begin.
|
1
2018/data/08.txt
Normal file
1
2018/data/08.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user