diff --git a/2019/13/README.md b/2019/13/README.md new file mode 100644 index 0000000..785592b --- /dev/null +++ b/2019/13/README.md @@ -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**? \ No newline at end of file diff --git a/2019/13/code.py b/2019/13/code.py new file mode 100644 index 0000000..10a0cae --- /dev/null +++ b/2019/13/code.py @@ -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)}") \ No newline at end of file diff --git a/2019/13/input.txt b/2019/13/input.txt new file mode 100644 index 0000000..98663d6 --- /dev/null +++ b/2019/13/input.txt @@ -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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32,44,61,67,23,51,36,98,24,28,41,48,55,52,69,20,39,37,4,9,61,90,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,54,53,41,56,18,9,44,27,9,6,81,81,37,23,47,48,7,46,73,29,81,95,50,90,95,81,74,53,29,25,74,35,12,30,58,13,97,87,77,38,83,6,44,62,41,64,75,32,93,31,66,63,97,65,58,80,49,28,62,7,36,26,72,96,5,65,81,30,60,5,43,3,3,14,78,51,91,97,13,48,54,6,9,81,22,31,92,33,70,81,61,61,69,95,93,15,36,91,29,91,27,43,43,57,6,14,80,1,43,20,63,42,51,84,28,97,50,50,65,70,48,79,62,97,94,93,96,42,57,40,46,50,69,47,41,64,17,2,40,28,41,55,94,7,18,18,1,53,34,7,71,77,47,41,14,81,76,66,63,10,48,24,69,75,44,90,52,96,35,1,47,45,24,85,90,40,66,45,10,89,44,84,91,44,65,59,93,19,57,66,79,51,22,13,67,50,6,56,40,18,36,48,71,87,65,36,37,77,37,19,62,97,13,44,77,80,45,97,45,86,30,21,23,76,70,94,88,61,58,59,57,66,75,84,86,15,58,40,54,74,26,34,19,77,96,32,94,82,13,46,96,49,77,38,73,92,42,98,80,10,89,49,66,10,50,97,83,89,6,76,78,9,27,60,76,49,24,45,39,5,42,77,61,69,37,58,4,24,20,33,10,56,46,18,11,36,11,17,3,6,21,67,21,2,91,52,83,95,20,80,11,98,34,52,14,39,95,11,40,11,68,54,89,3,98,61,49,30,71,30,2,25,10,38,93,46,64,46,40,9,1,96,21,98,88,66,83,44,84,27,65,6,35,63,45,96,57,66,49,56,52,21,8,95,47,94,37,14,42,6,87,74,62,50,97,12,39,97,93,46,36,16,83,35,72,64,37,86,61,69,82,21,97,62,54,68,71,60,37,67,81,10,57,45,74,71,13,82,1,85,2,13,74,14,46,52,44,61,49,78,13,53,75,86,18,60,4,73,82,51,76,61,58,76,44,12,40,17,17,62,86,71,24,28,57,87,3,8,52,51,79,39,38,41,48,62,67,63,39,50,70,87,59,35,57,75,24,2,89,34,77,6,20,66,40,74,54,41,34,27,68,63,46,69,23,93,39,46,46,60,9,63,70,34,86,75,34,43,13,71,37,19,83,86,74,41,16,88,44,43,85,49,4,3,98,56,77,16,41,30,18,64,2,49,49,19,35,33,93,73,83,40,19,72,12,83,33,31,54,58,72,90,40,79,51,64,85,44,84,67,3,15,57,648621 diff --git a/2019/13/solution.txt b/2019/13/solution.txt new file mode 100644 index 0000000..5a25c12 --- /dev/null +++ b/2019/13/solution.txt @@ -0,0 +1 @@ +Part 1: 200 \ No newline at end of file