2019 day 6 and 7
This commit is contained in:
parent
9718165d7b
commit
6f71ed1717
57
2019/d6.py
Normal file
57
2019/d6.py
Normal file
@ -0,0 +1,57 @@
|
||||
from lib import get_data, str_to_ints
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def part_1(data):
|
||||
orbits = defaultdict(list)
|
||||
for line in data.splitlines():
|
||||
a, b = line.split(")")
|
||||
orbits[a].append(b)
|
||||
|
||||
count = 0
|
||||
for key in orbits.keys():
|
||||
seen = set()
|
||||
around = list(orbits[key])
|
||||
while around:
|
||||
a = around.pop()
|
||||
count += 1
|
||||
if a in orbits:
|
||||
for o in orbits[a]:
|
||||
if o not in seen:
|
||||
around.append(o)
|
||||
seen.add(a)
|
||||
print(count)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
g = defaultdict(list)
|
||||
for line in data.splitlines():
|
||||
a, b = line.split(")")
|
||||
g[a].append(b)
|
||||
g[b].append(a)
|
||||
|
||||
seen = set()
|
||||
nodes = ["YOU"]
|
||||
steps = 0
|
||||
while True:
|
||||
new_nodes = []
|
||||
for node in nodes:
|
||||
seen.add(node)
|
||||
for nb in g[node]:
|
||||
if nb not in seen:
|
||||
new_nodes.append(nb)
|
||||
if nb == "SAN":
|
||||
print(steps - 1)
|
||||
return
|
||||
nodes = new_nodes
|
||||
steps += 1
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
144
2019/d7.py
Normal file
144
2019/d7.py
Normal file
@ -0,0 +1,144 @@
|
||||
from lib import get_data, str_to_ints
|
||||
from itertools import permutations
|
||||
|
||||
|
||||
class Amp:
|
||||
def __init__(self, xs):
|
||||
self.xs = list(xs)
|
||||
self.i = 0
|
||||
self.inputs = []
|
||||
self.outputs = []
|
||||
self.done = False
|
||||
|
||||
def feed(self, input):
|
||||
self.inputs.append(input)
|
||||
|
||||
def pop(self):
|
||||
v = self.outputs[0]
|
||||
self.outputs = self.outputs[1:]
|
||||
return v
|
||||
|
||||
def go(self):
|
||||
xs = self.xs
|
||||
i = self.i
|
||||
while i < len(xs):
|
||||
inst = str(xs[i])
|
||||
inst = "0" * (5 - len(inst)) + inst
|
||||
assert len(inst) == 5
|
||||
op = int(inst[3:5])
|
||||
mode_p1 = int(inst[2])
|
||||
mode_p2 = int(inst[1])
|
||||
mode_p3 = int(inst[0])
|
||||
match op:
|
||||
case 1:
|
||||
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
|
||||
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
|
||||
assert mode_p3 == 0
|
||||
xs[xs[i + 3]] = p1 + p2
|
||||
i += 4
|
||||
case 2:
|
||||
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
|
||||
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
|
||||
assert mode_p3 == 0
|
||||
xs[xs[i + 3]] = p1 * p2
|
||||
i += 4
|
||||
case 3:
|
||||
assert mode_p1 == 0
|
||||
assert len(self.inputs) > 0
|
||||
xs[xs[i + 1]] = self.inputs[0]
|
||||
self.inputs = self.inputs[1:]
|
||||
i += 2
|
||||
case 4:
|
||||
if mode_p1 == 0:
|
||||
v = xs[xs[i + 1]]
|
||||
else:
|
||||
v = xs[i + 1]
|
||||
self.outputs.append(v)
|
||||
i += 2
|
||||
self.i = i
|
||||
return
|
||||
case 99:
|
||||
self.done = True
|
||||
return
|
||||
case 5:
|
||||
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
|
||||
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
|
||||
if p1 != 0:
|
||||
i = p2
|
||||
else:
|
||||
i += 3
|
||||
case 6:
|
||||
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
|
||||
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
|
||||
if p1 == 0:
|
||||
i = p2
|
||||
else:
|
||||
i += 3
|
||||
case 7:
|
||||
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
|
||||
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
|
||||
assert mode_p3 == 0
|
||||
if p1 < p2:
|
||||
xs[xs[i + 3]] = 1
|
||||
else:
|
||||
xs[xs[i + 3]] = 0
|
||||
i += 4
|
||||
case 8:
|
||||
p1 = xs[xs[i + 1]] if mode_p1 == 0 else xs[i + 1]
|
||||
p2 = xs[xs[i + 2]] if mode_p2 == 0 else xs[i + 2]
|
||||
assert mode_p3 == 0
|
||||
if p1 == p2:
|
||||
xs[xs[i + 3]] = 1
|
||||
else:
|
||||
xs[xs[i + 3]] = 0
|
||||
i += 4
|
||||
self.i = i
|
||||
|
||||
|
||||
def part_1(data):
|
||||
xs_orig = str_to_ints(data)
|
||||
max_output = 0
|
||||
for ps in permutations(list(range(5))):
|
||||
current_output = 0
|
||||
for p in ps:
|
||||
a = Amp(xs_orig)
|
||||
a.feed(p)
|
||||
a.feed(current_output)
|
||||
a.go()
|
||||
assert len(a.outputs) == 1
|
||||
current_output = a.outputs.pop()
|
||||
max_output = max(max_output, current_output)
|
||||
print(max_output)
|
||||
|
||||
|
||||
def part_2(data):
|
||||
xs_orig = str_to_ints(data)
|
||||
max_output = 0
|
||||
for ps in permutations(list(range(5, 10))):
|
||||
amps = [Amp(xs_orig) for _ in range(len(ps))]
|
||||
for i, p in enumerate(ps):
|
||||
amps[i].feed(p)
|
||||
|
||||
current_output = 0
|
||||
current_amp_i = 0
|
||||
while True:
|
||||
amps[current_amp_i].feed(current_output)
|
||||
amps[current_amp_i].go()
|
||||
if amps[current_amp_i].done:
|
||||
max_output = max(max_output, current_output)
|
||||
break
|
||||
assert len(amps[current_amp_i].outputs) == 1
|
||||
current_output = amps[current_amp_i].outputs.pop()
|
||||
current_amp_i = (current_amp_i + 1) % len(amps)
|
||||
|
||||
print(max_output)
|
||||
|
||||
|
||||
def main():
|
||||
data = get_data(__file__)
|
||||
part_1(data)
|
||||
part_2(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -58,7 +58,7 @@ Solutions and utility script for Advent of Code challenges in Python.
|
||||
- Day 24: 48:00
|
||||
- Day 25: 6:45
|
||||
|
||||
## 2017
|
||||
## AoC 2017
|
||||
|
||||
- Day 1: 7:30
|
||||
- Day 2: 6:15
|
||||
@ -115,7 +115,9 @@ Solutions and utility script for Advent of Code challenges in Python.
|
||||
- Day 3: 10:46 (37th)
|
||||
- Day 4: 8:48 (just too slow, no leaderboard)
|
||||
- Day 5: 34:24 (that wasn't hard at all)
|
||||
- Day 6:
|
||||
- Day 6: 23:17 (so slow, no leaderboard)
|
||||
- Day 7: 72:17 (I found that one challenging, 30:33 would have been required for leaderboard)
|
||||
- Day 8:
|
||||
|
||||
## AoC 2022
|
||||
|
||||
|
1
lib.py
1
lib.py
@ -275,6 +275,7 @@ def get_data(filename):
|
||||
return f.read()
|
||||
else:
|
||||
import subprocess
|
||||
|
||||
subprocess.call(["../get.py", year, day])
|
||||
assert os.path.isfile(txt_file), "Could not download AoC file"
|
||||
with open(txt_file) as f:
|
||||
|
@ -8,7 +8,7 @@ import sys
|
||||
|
||||
def get_file_hash(filename):
|
||||
hasher = hashlib.sha256()
|
||||
with open(filename, 'rb') as f:
|
||||
with open(filename, "rb") as f:
|
||||
hasher.update(f.read())
|
||||
return hasher.hexdigest()
|
||||
|
||||
@ -25,7 +25,7 @@ def main(script_name, interval=1):
|
||||
if process and process.poll() is None:
|
||||
process.terminate()
|
||||
print(f"Detected change in {script_name}, running script...")
|
||||
process = subprocess.Popen(['pypy3', script_name], shell=False)
|
||||
process = subprocess.Popen(["pypy3", script_name], shell=False)
|
||||
time.sleep(interval)
|
||||
except KeyboardInterrupt:
|
||||
if process:
|
||||
@ -35,5 +35,6 @@ def main(script_name, interval=1):
|
||||
print("The file was not found. Make sure the script name is correct.")
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1])
|
||||
|
Loading…
Reference in New Issue
Block a user