Added 2019 day 13 part 1
This commit is contained in:
1 parent
a4db04926a
commit
041713b520
4 files changed
+146
No files matched your search
@@ -0,0 +1,34 @@
|
|||||||
|
# 2019 Day 13: Care Package
|
||||||
|
Copyright (c) Eric Wastl
|
||||||
|
#### [Direct Link](https://adventofcode.com/2019/day/13)
|
||||||
|
|
||||||
|
## Part 1
|
||||||
|
As you ponder the solitude of space and the ever-increasing three-hour roundtrip for messages between you and Earth, you notice that the Space Mail Indicator Light is blinking. To help keep you sane, the Elves have sent you a care package.
|
||||||
|
|
||||||
|
It's a new game for the ship's [arcade cabinet](https://en.wikipedia.org/wiki/Arcade_cabinet)! Unfortunately, the arcade is **all the way** on the other end of the ship. Surely, it won't be hard to build your own - the care package even comes with schematics.
|
||||||
|
|
||||||
|
The arcade cabinet runs [Intcode](https://adventofcode.com/2019/day/9) software like the game the Elves sent (your puzzle input). It has a primitive screen capable of drawing square tiles on a grid. The software draws tiles to the screen with output instructions: every three output instructions specify the `x` position (distance from the left), `y` position (distance from the top), and `tile id`. The `tile id` is interpreted as follows:
|
||||||
|
|
||||||
|
- `0` is an **empty** tile. No game object appears in this tile.
|
||||||
|
- `1` is a **wall** tile. Walls are indestructible barriers.
|
||||||
|
- `2` is a **block** tile. Blocks can be broken by the ball.
|
||||||
|
- `3` is a **horizontal paddle** tile. The paddle is indestructible.
|
||||||
|
- `4` is a **ball** tile. The ball moves diagonally and bounces off objects.
|
||||||
|
|
||||||
|
For example, a sequence of output values like `1,2,3,6,5,4` would draw a **horizontal paddle** tile (`1` tile from the left and `2` tiles from the top) and a **ball** tile (`6` tiles from the left and `5` tiles from the top).
|
||||||
|
|
||||||
|
Start the game. **How many block tiles are on the screen when the game exits**?
|
||||||
|
|
||||||
|
## Part 2
|
||||||
|
|
||||||
|
The game didn't run because you didn't put in any quarters. Unfortunately, you did not bring any quarters. Memory address `0` represents the number of quarters that have been inserted; set it to `2` to play for free.
|
||||||
|
|
||||||
|
The arcade cabinet has a [joystick](https://en.wikipedia.org/wiki/Joystick) that can move left and right. The software reads the position of the joystick with input instructions:
|
||||||
|
|
||||||
|
- If the joystick is in the **neutral position**, provide `0`.
|
||||||
|
- If the joystick is **tilted to the left**, provide `-1`.
|
||||||
|
- If the joystick is **tilted to the right**, provide `1`.
|
||||||
|
|
||||||
|
The arcade cabinet also has a [segment display](https://en.wikipedia.org/wiki/Display_device#Segment_displays) capable of showing a single number that represents the player's current score. When three output instructions specify `X=-1, Y=0`, the third output instruction is not a tile; the value instead specifies the new score to show in the segment display. For example, a sequence of output values like `-1,0,12345` would show `12345` as the player's current score.
|
||||||
|
|
||||||
|
Beat the game by breaking all the blocks. **What is your score after the last block is broken**?
|
||||||
+110
@@ -0,0 +1,110 @@
|
|||||||
|
""" https://adventofcode.com/2019/day/13 """
|
||||||
|
|
||||||
|
class InvalidOpcode(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class mylist(list):
|
||||||
|
def __getitem__(self, item):
|
||||||
|
try:
|
||||||
|
return super(mylist,self).__getitem__(item)
|
||||||
|
except IndexError as e:
|
||||||
|
if item < 0: raise IndexError()
|
||||||
|
super(mylist,self).extend([0]*(item + 1 - super(mylist,self).__len__()))
|
||||||
|
return super(mylist,self).__getitem__(item)
|
||||||
|
|
||||||
|
def __setitem__(self, idx, val):
|
||||||
|
try:
|
||||||
|
return super(mylist,self).__setitem__(idx,val)
|
||||||
|
except IndexError as e:
|
||||||
|
if idx < 0: raise IndexError()
|
||||||
|
super(mylist,self).extend([0]*(idx + 1 - super(mylist,self).__len__()))
|
||||||
|
return super(mylist,self).__setitem__(idx,val)
|
||||||
|
|
||||||
|
def readFile():
|
||||||
|
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
|
||||||
|
return [int(num) for num in f.readline().split(",")]
|
||||||
|
|
||||||
|
def getOutput(vals : list, input=0, base=0):
|
||||||
|
vals = mylist(vals)
|
||||||
|
i = 0
|
||||||
|
out = []
|
||||||
|
while 1:
|
||||||
|
opcode = (vals[i] % 100,
|
||||||
|
vals[i] // 100 % 10,
|
||||||
|
vals[i] // 1000 % 10,
|
||||||
|
vals[i] // 10000 % 10)
|
||||||
|
|
||||||
|
# 0 Parameter
|
||||||
|
if opcode[0] in [99]: # Termination
|
||||||
|
return vals, out
|
||||||
|
|
||||||
|
# 1 Parameter
|
||||||
|
elif opcode[0] in [3,4,9]:
|
||||||
|
a = vals[i+1] if not opcode[1] else i+1 if opcode[1] == 1 else base+vals[i+1]
|
||||||
|
if opcode[0] == 3: # Input
|
||||||
|
vals[a] = input
|
||||||
|
elif opcode[0] == 4: # Output
|
||||||
|
vals[0] = vals[a]
|
||||||
|
out.append(vals[0])
|
||||||
|
elif opcode[0] == 9: # Adjust Base
|
||||||
|
base += vals[a]
|
||||||
|
i += 2
|
||||||
|
|
||||||
|
# 2 Parameter
|
||||||
|
elif opcode[0] in [5,6]:
|
||||||
|
a = vals[i+1] if not opcode[1] else i+1 if opcode[1] == 1 else base+vals[i+1]
|
||||||
|
b = vals[i+2] if not opcode[2] else i+2 if opcode[2] == 1 else base+vals[i+2]
|
||||||
|
if opcode[0] == 5 and vals[a] != 0: # Jump-if-true
|
||||||
|
i = vals[b]
|
||||||
|
elif opcode[0] == 6 and vals[a] == 0: # Jump-if-false
|
||||||
|
i = vals[b]
|
||||||
|
else:
|
||||||
|
i += 3
|
||||||
|
|
||||||
|
# 3 Parameter
|
||||||
|
elif opcode[0] in [1,2,7,8]:
|
||||||
|
a = vals[i+1] if not opcode[1] else i+1 if opcode[1] == 1 else base+vals[i+1]
|
||||||
|
b = vals[i+2] if not opcode[2] else i+2 if opcode[2] == 1 else base+vals[i+2]
|
||||||
|
c = vals[i+3] if not opcode[3] else i+3 if opcode[3] == 1 else base+vals[i+3]
|
||||||
|
if opcode[0] == 1: # Addition
|
||||||
|
vals[c] = vals[a] + vals[b]
|
||||||
|
elif opcode[0] == 2: # Multiplication
|
||||||
|
vals[c] = vals[a] * vals[b]
|
||||||
|
elif opcode[0] == 7: # Less Than
|
||||||
|
vals[c] = int(vals[a] < vals[b])
|
||||||
|
elif opcode[0] == 8: # Equals
|
||||||
|
vals[c] = int(vals[a] == vals[b])
|
||||||
|
i += 4
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise InvalidOpcode()
|
||||||
|
|
||||||
|
def getEntities():
|
||||||
|
output = getOutput(vals.copy())[1]
|
||||||
|
output.reverse()
|
||||||
|
assert len(output) % 3 == 0
|
||||||
|
entities = [set() for _ in range(5)]
|
||||||
|
while output:
|
||||||
|
pos = (output.pop(), output.pop())
|
||||||
|
tile = output.pop()
|
||||||
|
entities[tile].add(pos)
|
||||||
|
return entities
|
||||||
|
|
||||||
|
def part1(entities):
|
||||||
|
print([len(x) for x in entities])
|
||||||
|
print(entities[3], entities[4])
|
||||||
|
return len(entities[2])
|
||||||
|
|
||||||
|
class Ball:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.x, self.y = 0,0
|
||||||
|
self.dx, self.dy = 0,0
|
||||||
|
|
||||||
|
def part2(entities):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
vals = readFile()
|
||||||
|
entities = getEntities()
|
||||||
|
print(f"Part 1: {part1(entities)}")
|
||||||
|
#print(f"Part 2: {part2(entities)}")
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1,380,379,385,1008,2399,648621,381,1005,381,12,99,109,2400,1102,0,1,383,1102,1,0,382,20101,0,382,1,21002,383,1,2,21102,1,37,0,1105,1,578,4,382,4,383,204,1,1001,382,1,382,1007,382,44,381,1005,381,22,1001,383,1,383,1007,383,20,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1105,1,161,107,1,392,381,1006,381,161,1102,-1,1,384,1105,1,119,1007,392,42,381,1006,381,161,1102,1,1,384,21002,392,1,1,21101,18,0,2,21101,0,0,3,21102,1,138,0,1105,1,549,1,392,384,392,20102,1,392,1,21101,18,0,2,21102,1,3,3,21102,161,1,0,1105,1,549,1101,0,0,384,20001,388,390,1,21001,389,0,2,21102,180,1,0,1106,0,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21001,389,0,2,21101,0,205,0,1106,0,393,1002,390,-1,390,1102,1,1,384,20102,1,388,1,20001,389,391,2,21101,228,0,0,1106,0,578,1206,1,261,1208,1,2,381,1006,381,253,20102,1,388,1,20001,389,391,2,21101,253,0,0,1106,0,393,1002,391,-1,391,1101,1,0,384,1005,384,161,20001,388,390,1,20001,389,391,2,21101,0,279,0,1106,0,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21101,304,0,0,1106,0,393,1002,390,-1,390,1002,391,-1,391,1101,0,1,384,1005,384,161,21001,388,0,1,20101,0,389,2,21101,0,0,3,21102,1,338,0,1105,1,549,1,388,390,388,1,389,391,389,21002,388,1,1,20102,1,389,2,21101,4,0,3,21102,365,1,0,1105,1,549,1007,389,19,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,200,20,15,1,1,22,109,3,22102,1,-2,1,21202,-1,1,2,21101,0,0,3,21102,1,414,0,1105,1,549,21202,-2,1,1,22102,1,-1,2,21101,429,0,0,1106,0,601,1201,1,0,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2106,0,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,21202,-3,1,-7,109,-8,2106,0,0,109,4,1202,-2,44,566,201,-3,566,566,101,639,566,566,1202,-1,1,0,204,-3,204,-2,204,-1,109,-4,2105,1,0,109,3,1202,-1,44,594,201,-2,594,594,101,639,594,594,20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,52,24,56,6,64,22,27,80,39,70,29,95,21,91,98,42,19,15,31,83,55,13,38,46,22,3,42,88,79,4,79,23,33,37,61,56,68,84,83,88,29,82,95,15,9,74,52,64,46,35,6,81,38,19,82,84,76,76,78,47,8,40,13,51,91,51,37,2,76,28,20,44,15,74,82,37,77,46,50,53,40,26,51,10,94,24,68,59,18,50,78,7,77,74,54,90,83,51,39,92,78,37,65,49,41,34,32,19,75,91,48,93,37,64,26,24,26,77,58,31,76,75,76,16,56,21,25,55,52,92,58,87,27,52,66,61,49,68,87,55,88,57,54,5,61,50,70,27,5,84,43,30,33,85,69,72,95,76,36,56,60,61,51,61,64,68,81,42,35,91,30,82,64,1,61,76,81,7,31,98,28,35,95,8,73,29,20,75,66,87,89,6,35,36,98,74,64,89,95,29,96,39,70,83,55,52,82,92,96,32,61,21,87,19,3,93,76,97,5,67,69,43,68,72,57,56,51,77,19,22,5,84,91,88,98,91,11,52,Line truncated
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Part 1: 200
|
||||||
Reference in new issue
Block a user