Compare commits
2 Commits
fc35818fd4
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 69fe5ef070 | |||
| 407ff5c036 |
@@ -0,0 +1,8 @@
|
||||
R 4
|
||||
U 4
|
||||
L 3
|
||||
D 1
|
||||
R 4
|
||||
D 1
|
||||
L 5
|
||||
R 2
|
||||
@@ -0,0 +1,8 @@
|
||||
R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
|
||||
import fileinput
|
||||
|
||||
class Rope:
|
||||
|
||||
def __init__(self):
|
||||
self.head = [0,0]
|
||||
self.tail = [0,0]
|
||||
self.visited = set([tuple(self.tail)])
|
||||
|
||||
def chatchup(self):
|
||||
if max(abs(self.head[0] - self.tail[0]), abs(self.head[1] - self.tail[1])) <= 1: # Same pos or next to each other
|
||||
pass
|
||||
else:
|
||||
if self.head[1] > self.tail[1]:
|
||||
self.tail[1] += 1
|
||||
elif self.head[1] < self.tail[1]:
|
||||
self.tail[1] -= 1
|
||||
if self.head[0] > self.tail[0]:
|
||||
self.tail[0] += 1
|
||||
elif self.head[0] < self.tail[0]:
|
||||
self.tail[0] -= 1
|
||||
self.visited.add(tuple(self.tail))
|
||||
|
||||
def step(self, d, count):
|
||||
if d == "U":
|
||||
self.head[1] += 1
|
||||
elif d == "D":
|
||||
self.head[1] -= 1
|
||||
elif d == "L":
|
||||
self.head[0] += 1
|
||||
elif d == "R":
|
||||
self.head[0] -= 1
|
||||
self.chatchup()
|
||||
if count > 1:
|
||||
self.step(d, count-1)
|
||||
|
||||
class LongRope:
|
||||
|
||||
def __init__(self):
|
||||
self.ropes = []
|
||||
for _ in range(9):
|
||||
self.ropes.append(Rope())
|
||||
|
||||
def step(self, d, count):
|
||||
if d == "U":
|
||||
self.ropes[0].head[1] += 1
|
||||
elif d == "D":
|
||||
self.ropes[0].head[1] -= 1
|
||||
elif d == "L":
|
||||
self.ropes[0].head[0] += 1
|
||||
elif d == "R":
|
||||
self.ropes[0].head[0] -= 1
|
||||
self.ropes[0].chatchup()
|
||||
for idx in range(1, len(self.ropes)):
|
||||
self.ropes[idx].head = self.ropes[idx-1].tail
|
||||
self.ropes[idx].chatchup()
|
||||
if count > 1:
|
||||
self.step(d,count-1)
|
||||
|
||||
def solve(lines):
|
||||
rope = Rope()
|
||||
for d, count in map(lambda x: x.split(' '), filter(lambda x: x != "", map(lambda x: x.strip(), lines))):
|
||||
count = int(count)
|
||||
rope.step(d, count)
|
||||
return len(rope.visited)
|
||||
|
||||
def solveb(lines):
|
||||
rope = LongRope()
|
||||
for d, count in map(lambda x: x.split(' '), filter(lambda x: x != "", map(lambda x: x.strip(), lines))):
|
||||
count = int(count)
|
||||
rope.step(d, count)
|
||||
return len(rope.ropes[-1].visited)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Visited {solve(fileinput.input())}")
|
||||
print(f"Visited long {solveb(fileinput.input())}")
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
import fileinput
|
||||
|
||||
class CPU:
|
||||
|
||||
def __init__(self):
|
||||
self.reg_x = 1
|
||||
self.cycle = 0
|
||||
|
||||
def draw(self):
|
||||
pos = self.cycle - 1
|
||||
if pos % 40 == 0 and pos != 0:
|
||||
print('')
|
||||
print("#" if abs((pos % 40) - self.reg_x) <= 1 else ".", end='')
|
||||
if self.cycle == 240:
|
||||
print("")
|
||||
|
||||
def exec(self, instructions, breaks=[]):
|
||||
for instruction in instructions:
|
||||
# print(f"Start cycle {self.cycle:3d}: begin executing {instruction}")
|
||||
if instruction == "noop":
|
||||
self.cycle += 1
|
||||
if self.cycle in breaks:
|
||||
yield self.cycle*self.reg_x
|
||||
self.draw()
|
||||
elif instruction.startswith("addx"):
|
||||
self.cycle += 1
|
||||
if self.cycle in breaks:
|
||||
yield self.cycle*self.reg_x
|
||||
self.draw()
|
||||
self.cycle += 1
|
||||
if self.cycle in breaks:
|
||||
yield self.cycle*self.reg_x
|
||||
self.draw()
|
||||
self.reg_x += int(instruction.split(' ')[1])
|
||||
else:
|
||||
print("Unknown instruction")
|
||||
|
||||
if __name__ == "__main__":
|
||||
cpu = CPU()
|
||||
print(f"Solution A: {sum(cpu.exec(filter(lambda x: x != '', map(lambda x: x.strip(), fileinput.input())), [20, 60, 100, 140, 180, 220]))}")
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
addx 15
|
||||
addx -11
|
||||
addx 6
|
||||
addx -3
|
||||
addx 5
|
||||
addx -1
|
||||
addx -8
|
||||
addx 13
|
||||
addx 4
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -35
|
||||
addx 1
|
||||
addx 24
|
||||
addx -19
|
||||
addx 1
|
||||
addx 16
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -15
|
||||
noop
|
||||
noop
|
||||
addx -3
|
||||
addx 9
|
||||
addx 1
|
||||
addx -3
|
||||
addx 8
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -36
|
||||
noop
|
||||
addx 1
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
noop
|
||||
addx -13
|
||||
addx 13
|
||||
addx 7
|
||||
noop
|
||||
addx 1
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 17
|
||||
addx -9
|
||||
addx 1
|
||||
addx 1
|
||||
addx -3
|
||||
addx 11
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -13
|
||||
addx -19
|
||||
addx 1
|
||||
addx 3
|
||||
addx 26
|
||||
addx -30
|
||||
addx 12
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -9
|
||||
addx 18
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 9
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx -37
|
||||
addx 1
|
||||
addx 3
|
||||
noop
|
||||
addx 15
|
||||
addx -21
|
||||
addx 22
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 20
|
||||
addx 1
|
||||
addx 2
|
||||
addx 2
|
||||
addx -6
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
Reference in New Issue
Block a user