зеркало из https://github.com/microsoft/Reactors.git
Episode 3
Co-authored-by: Jack Reichelt <jackreichelt@users.noreply.github.com>
This commit is contained in:
Родитель
7d8ae71d40
Коммит
99ee00fb86
|
@ -0,0 +1,93 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
instructions = [x.strip() for x in open("day10-data.txt").readlines()]
|
||||
|
||||
class CPU:
|
||||
def __init__(self):
|
||||
self.clock = 0
|
||||
self.X = 1
|
||||
self.signal_beats = []
|
||||
self.display = []
|
||||
|
||||
def log(self):
|
||||
if (self.clock+20) % 40 == 0:
|
||||
print(self.clock, self.X, self.signal_strength())
|
||||
self.signal_beats.append(self.signal_strength())
|
||||
|
||||
def setPixel(self):
|
||||
if abs(self.clock%40 - self.X) <= 1:
|
||||
self.display.append('#')
|
||||
else:
|
||||
self.display.append('.')
|
||||
|
||||
def drawDisplay(self):
|
||||
for i in range(self.clock):
|
||||
if i % 40 == 0:
|
||||
print()
|
||||
print(self.display[i], end="")
|
||||
|
||||
def step(self):
|
||||
self.setPixel()
|
||||
self.clock += 1
|
||||
self.log()
|
||||
|
||||
def noop(self):
|
||||
self.step()
|
||||
|
||||
def addx(self, val):
|
||||
self.step()
|
||||
self.step()
|
||||
self.X += val
|
||||
|
||||
def signal_strength(self):
|
||||
return self.clock * self.X
|
||||
|
||||
cpu = CPU()
|
||||
|
||||
for i in instructions:
|
||||
#print(">", i)
|
||||
if i == "noop":
|
||||
cpu.noop()
|
||||
elif i.startswith("addx"):
|
||||
val = int(i.split(" ")[1])
|
||||
cpu.addx(val)
|
||||
#print(">>", cpu.clock, cpu.X)
|
||||
|
||||
print("Signal sum:", sum(cpu.signal_beats))
|
||||
|
||||
cpu.drawDisplay()
|
||||
|
||||
|
||||
# Renee's code
|
||||
# x = 1
|
||||
# cycle_x = []
|
||||
|
||||
# with open("day10-data.txt") as f:
|
||||
# for line in f:
|
||||
# inst = line.strip().split(" ")
|
||||
|
||||
# if inst[0] == "noop":
|
||||
# cycle_x.append(x)
|
||||
|
||||
# else:
|
||||
# num = inst[1]
|
||||
# cycle_x.append(x)
|
||||
# cycle_x.append(x)
|
||||
# x += int(num)
|
||||
|
||||
|
||||
# total = 0
|
||||
# for i in range(19, len(cycle_x), 40):
|
||||
# print(cycle_x[i - 1], i + 1)
|
||||
# total += cycle_x[i - 1] * (i + 1)
|
||||
|
||||
# print(total)
|
||||
|
||||
# for i, x in enumerate(cycle_x):
|
||||
# if i%40 == x-1 or i%40 == x or i%40 == x + 1:
|
||||
# print("#", end="")
|
||||
# else:
|
||||
# print(".", end="")
|
||||
|
||||
# if i + 1 in list(range(0, len(cycle_x), 40)):
|
||||
# print()
|
|
@ -0,0 +1,88 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
data = [x.strip() for x in open("day11-data.txt").readlines()]
|
||||
|
||||
class Monkey:
|
||||
allMonkeys = []
|
||||
lcm = 1
|
||||
|
||||
def monkey(i):
|
||||
return Monkey.allMonkeys[i]
|
||||
|
||||
def __init__(self, items, op, test, tRecipient, fRecipient):
|
||||
self.items = items
|
||||
self.op = op
|
||||
self.test = test
|
||||
self.tRecipient = tRecipient
|
||||
self.fRecipient = fRecipient
|
||||
self.inspections = 0
|
||||
Monkey.allMonkeys.append(self)
|
||||
|
||||
def turn(self):
|
||||
self.inspections += len(self.items)
|
||||
for i in self.items:
|
||||
i = self.op(i)
|
||||
# i //= 3
|
||||
i %= Monkey.lcm
|
||||
if self.test(i):
|
||||
self.giveItem(i, Monkey.allMonkeys[self.tRecipient])
|
||||
else:
|
||||
self.giveItem(i, Monkey.allMonkeys[self.fRecipient])
|
||||
self.items = []
|
||||
|
||||
def giveItem(self, i, recipient):
|
||||
recipient.items.append(i)
|
||||
|
||||
for i in range(0, len(data), 7):
|
||||
# Monkey 0:
|
||||
# Starting items: 79, 98
|
||||
startingItemsData = data[i+1].split(": ")[1]
|
||||
startingItems = [int(x) for x in startingItemsData.split(", ")]
|
||||
# Operation: new = old * 19
|
||||
operationStr = data[i+2].split(" = ")[1]
|
||||
a, op, b = operationStr.split(" ")
|
||||
if op == "+":
|
||||
if b.isnumeric():
|
||||
operationFunc = lambda x, b=int(b): x + b
|
||||
else:
|
||||
operationFunc = lambda x: x + x
|
||||
elif op == "*":
|
||||
if b.isnumeric():
|
||||
operationFunc = lambda x, b=int(b): x * b
|
||||
else:
|
||||
operationFunc = lambda x: x * x
|
||||
# Test: divisible by 23
|
||||
modNum = int(data[i+3].split(" ")[-1])
|
||||
Monkey.lcm *= modNum
|
||||
testFunc = lambda x, m=modNum: x % m == 0
|
||||
# If true: throw to monkey 2
|
||||
tRecipient = int(data[i+4].split(" ")[-1])
|
||||
# If false: throw to monkey 3
|
||||
fRecipient = int(data[i+5].split(" ")[-1])
|
||||
|
||||
Monkey(startingItems, operationFunc, testFunc, tRecipient, fRecipient)
|
||||
|
||||
for i in range(20):
|
||||
print("Round", i+1)
|
||||
for m in Monkey.allMonkeys:
|
||||
m.turn()
|
||||
for m in Monkey.allMonkeys:
|
||||
print(m.items)
|
||||
|
||||
inspections = sorted([m.inspections for m in Monkey.allMonkeys])
|
||||
print(inspections)
|
||||
print(inspections[-1] * inspections[-2])
|
||||
|
||||
for i in range(20, 10000):
|
||||
print("Round", i+1)
|
||||
for m in Monkey.allMonkeys:
|
||||
m.turn()
|
||||
for m in Monkey.allMonkeys:
|
||||
print(m.items)
|
||||
|
||||
inspections = sorted([m.inspections for m in Monkey.allMonkeys])
|
||||
print(inspections)
|
||||
print(inspections[-1] * inspections[-2])
|
||||
|
||||
# 15105064777 too high
|
||||
# 13937702909 correct!
|
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env python3
|
||||
from string import ascii_lowercase
|
||||
|
||||
data = [x.strip() for x in open("day12-data.txt").readlines()]
|
||||
|
||||
start = None
|
||||
target = None
|
||||
cells = {}
|
||||
|
||||
for y, row in enumerate(data):
|
||||
for x, cell in enumerate(row):
|
||||
if cell == "S":
|
||||
start = (x, y)
|
||||
cells[(x, y)] = 0
|
||||
elif cell == "E":
|
||||
target = (x, y)
|
||||
cells[(x, y)] = 25
|
||||
else:
|
||||
cells[(x, y)] = ascii_lowercase.index(cell)
|
||||
|
||||
def adjacentCells(cell):
|
||||
x, y = cell
|
||||
allAdjacents = []
|
||||
if (x-1, y) in cells:
|
||||
allAdjacents.append((x-1, y))
|
||||
if (x+1, y) in cells:
|
||||
allAdjacents.append((x+1, y))
|
||||
if (x, y-1) in cells:
|
||||
allAdjacents.append((x, y-1))
|
||||
if (x, y+1) in cells:
|
||||
allAdjacents.append((x, y+1))
|
||||
return allAdjacents
|
||||
|
||||
def canMove(fromCell, toCell):
|
||||
return cells[toCell] <= cells[fromCell]+1
|
||||
|
||||
def findPath(start):
|
||||
current = start
|
||||
shortestPaths = {start: 0}
|
||||
|
||||
cellsToCheck = [start]
|
||||
history = set()
|
||||
|
||||
while cellsToCheck:
|
||||
current = cellsToCheck.pop(0)
|
||||
steps = shortestPaths[current]
|
||||
|
||||
history.add(current)
|
||||
adjCells = adjacentCells(current)
|
||||
validCells = [c for c in adjCells if canMove(current, c)]
|
||||
|
||||
for c in validCells:
|
||||
if c not in history and c not in cellsToCheck:
|
||||
cellsToCheck.append(c)
|
||||
|
||||
if c in shortestPaths:
|
||||
shortestPaths[c] = min(steps+1, shortestPaths[c])
|
||||
else:
|
||||
shortestPaths[c] = steps+1
|
||||
|
||||
if target not in shortestPaths:
|
||||
return len(cells)
|
||||
return shortestPaths[target]
|
||||
|
||||
part1 = findPath(start)
|
||||
print(part1)
|
||||
|
||||
|
||||
shortestPath = part1
|
||||
bestStart = start
|
||||
|
||||
allACells = [c for c in cells.keys() if cells[c] == 0]
|
||||
|
||||
for aCell in allACells:
|
||||
dist = findPath(aCell)
|
||||
if dist < shortestPath:
|
||||
shortestPath = dist
|
||||
bestStart = aCell
|
||||
|
||||
print(bestStart, shortestPath)
|
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env python3
|
||||
from json import loads
|
||||
from functools import cmp_to_key
|
||||
|
||||
INORDER = 1
|
||||
OUTORDER = -1
|
||||
EQUAL = 0
|
||||
|
||||
fname = "day13-data.txt"
|
||||
|
||||
pairs = [x.strip().split("\n") for x in open(fname).read().split("\n\n")]
|
||||
|
||||
def compareList(left, right):
|
||||
for i, l in enumerate(left):
|
||||
if i >= len(right):
|
||||
return OUTORDER
|
||||
|
||||
r = right[i]
|
||||
|
||||
if type(l) == int and type(r) == int:
|
||||
if l < r:
|
||||
return INORDER
|
||||
elif l > r:
|
||||
return OUTORDER
|
||||
else:
|
||||
if type(l) == int:
|
||||
l = [l]
|
||||
if type(r) == int:
|
||||
r = [r]
|
||||
subComp = compareList(l, r)
|
||||
if subComp:
|
||||
return subComp
|
||||
|
||||
if len(right) > len(left):
|
||||
return INORDER
|
||||
return EQUAL
|
||||
|
||||
goodSum = 0
|
||||
for i, p in enumerate(pairs):
|
||||
left = loads(p[0])
|
||||
right = loads(p[1])
|
||||
if compareList(left, right) == 1:
|
||||
goodSum += i+1
|
||||
|
||||
print(goodSum)
|
||||
|
||||
allSignals = [loads(x.strip()) for x in open(fname).read().split("\n") if x]
|
||||
|
||||
allSignals.append([[2]])
|
||||
allSignals.append([[6]])
|
||||
|
||||
allSignals.sort(key=cmp_to_key(compareList), reverse=True)
|
||||
|
||||
div1Index = allSignals.index([[2]]) + 1
|
||||
div2Index = allSignals.index([[6]]) + 1
|
||||
print(div1Index * div2Index)
|
|
@ -0,0 +1,83 @@
|
|||
#!/usr/bin/env python3
|
||||
from math import sqrt
|
||||
|
||||
steps = [x.strip().split(" ") for x in open("day9-data.txt").readlines()]
|
||||
|
||||
U = (0,1)
|
||||
D = (0, -1)
|
||||
L = (-1, 0)
|
||||
R = (1, 0)
|
||||
|
||||
dirs = {"U":U, "D":D, "L":L, "R":R}
|
||||
|
||||
class Rope:
|
||||
def __init__(self, head=(0,0), tail=(0,0), nextRope=None):
|
||||
self.head = head
|
||||
self.tail = tail
|
||||
self.nextRope = nextRope
|
||||
self.tailLog = {tail}
|
||||
|
||||
def attachRope(self, rope):
|
||||
self.nextRope = rope
|
||||
|
||||
def ropeLength(self):
|
||||
a = self.head[0] - self.tail[0]
|
||||
b = self.head[1] - self.tail[1]
|
||||
return sqrt(a**2 + b**2)
|
||||
|
||||
def moveHead(self, dir):
|
||||
self.head = (self.head[0] + dir[0], self.head[1] + dir[1])
|
||||
self.catchUpTail()
|
||||
|
||||
def setHead(self, pos):
|
||||
self.head = pos
|
||||
self.catchUpTail()
|
||||
|
||||
def catchUpTail(self):
|
||||
rLen = self.ropeLength()
|
||||
if rLen < 2:
|
||||
# close enough
|
||||
pass
|
||||
elif rLen == 2:
|
||||
# linear offset
|
||||
if self.tail[0] == self.head[0]:
|
||||
# x pos is the same
|
||||
if self.tail[1] < self.head[1]:
|
||||
self.tail = (self.tail[0] + U[0], self.tail[1] + U[1])
|
||||
else:
|
||||
self.tail = (self.tail[0] + D[0], self.tail[1] + D[1])
|
||||
else:
|
||||
# y pos is the same
|
||||
if self.tail[0] < self.head[0]:
|
||||
self.tail = (self.tail[0] + R[0], self.tail[1] + R[1])
|
||||
else:
|
||||
self.tail = (self.tail[0] + L[0], self.tail[1] + L[1])
|
||||
else:
|
||||
# it's diagonally away
|
||||
if self.tail[1] < self.head[1]:
|
||||
self.tail = (self.tail[0] + U[0], self.tail[1] + U[1])
|
||||
else:
|
||||
self.tail = (self.tail[0] + D[0], self.tail[1] + D[1])
|
||||
if self.tail[0] < self.head[0]:
|
||||
self.tail = (self.tail[0] + R[0], self.tail[1] + R[1])
|
||||
else:
|
||||
self.tail = (self.tail[0] + L[0], self.tail[1] + L[1])
|
||||
self.tailLog.add(self.tail)
|
||||
|
||||
if self.nextRope:
|
||||
self.nextRope.setHead(self.tail)
|
||||
|
||||
firstRope = Rope()
|
||||
lastRope = firstRope
|
||||
|
||||
for i in range(8):
|
||||
newRope = Rope()
|
||||
lastRope.attachRope(newRope)
|
||||
lastRope = newRope
|
||||
|
||||
for d, c in steps:
|
||||
for i in range(int(c)):
|
||||
firstRope.moveHead(dirs[d])
|
||||
|
||||
print(len(lastRope.tailLog))
|
||||
|
Загрузка…
Ссылка в новой задаче