Skip to content

Commit 489c4b8

Browse files
author
Joth
committed
solve Day 13
1 parent 8251c33 commit 489c4b8

File tree

6 files changed

+354
-0
lines changed

6 files changed

+354
-0
lines changed

2019/Day 13/Part 1/__main__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Advent of Code 2019, Day 13, Part 1
2+
3+
from os import path
4+
from intcode import IntCode
5+
6+
7+
def main():
8+
text_input = get_raw_input()
9+
int_code = IntCode(text_input)
10+
11+
int_code.execute_till_halt()
12+
13+
output_instructions = list(int_code.output_buffer.queue)
14+
tiles = dict()
15+
i = 0
16+
while i + 2 < len(output_instructions):
17+
tiles[(output_instructions[i], output_instructions[i + 1])] = output_instructions[i + 2]
18+
i += 3
19+
20+
count = 0
21+
for tile in tiles.values():
22+
if tile == 2:
23+
count += 1
24+
25+
print(count)
26+
27+
28+
def get_raw_input():
29+
return open(retrieve_input_file_path(), 'r').read()
30+
31+
32+
def retrieve_input_file_path():
33+
return path.join(path.dirname(__file__), 'input.txt')
34+
35+
36+
if __name__ == '__main__':
37+
main()

2019/Day 13/Part 1/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,380,379,385,1008,2875,924596,381,1005,381,12,99,109,2876,1101,0,0,383,1102,0,1,382,21001,382,0,1,21002,383,1,2,21102,1,37,0,1106,0,578,4,382,4,383,204,1,1001,382,1,382,1007,382,43,381,1005,381,22,1001,383,1,383,1007,383,26,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,1106,0,161,107,1,392,381,1006,381,161,1101,-1,0,384,1105,1,119,1007,392,41,381,1006,381,161,1102,1,1,384,21001,392,0,1,21102,1,24,2,21101,0,0,3,21102,138,1,0,1106,0,549,1,392,384,392,21001,392,0,1,21102,24,1,2,21102,3,1,3,21102,161,1,0,1105,1,549,1102,1,0,384,20001,388,390,1,20101,0,389,2,21101,0,180,0,1106,0,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,20102,1,389,2,21102,205,1,0,1105,1,393,1002,390,-1,390,1102,1,1,384,21002,388,1,1,20001,389,391,2,21102,228,1,0,1106,0,578,1206,1,261,1208,1,2,381,1006,381,253,20101,0,388,1,20001,389,391,2,21102,1,253,0,1106,0,393,1002,391,-1,391,1102,1,1,384,1005,384,161,20001,388,390,1,20001,389,391,2,21101,0,279,0,1105,1,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21101,0,304,0,1105,1,393,1002,390,-1,390,1002,391,-1,391,1101,0,1,384,1005,384,161,20101,0,388,1,21002,389,1,2,21102,1,0,3,21102,1,338,0,1105,1,549,1,388,390,388,1,389,391,389,21002,388,1,1,21002,389,1,2,21102,1,4,3,21102,365,1,0,1105,1,549,1007,389,25,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,452,19,21,1,1,21,109,3,21201,-2,0,1,21202,-1,1,2,21101,0,0,3,21102,414,1,0,1105,1,549,22101,0,-2,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,21201,-3,0,-7,109,-8,2106,0,0,109,4,1202,-2,43,566,201,-3,566,566,101,639,566,566,2102,1,-1,0,204,-3,204,-2,204,-1,109,-4,2106,0,0,109,3,1202,-1,43,594,201,-2,594,594,101,639,594,594,20101,0,0,-2,109,-3,2105,1,0,109,3,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,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,89,82,95,24,35,69,73,71,23,5,87,90,41,96,87,87,72,81,29,20,53,96,2,8,39,33,34,13,46,62,86,8,11,11,32,39,61,76,43,42,88,15,27,80,16,28,78,86,36,49,77,74,3,72,28,18,54,58,72,17,78,93,13,29,86,61,31,89,6,1,66,51,68,42,39,27,65,2,83,12,75,75,92,19,75,50,62,10,2,72,56,65,15,57,28,79,47,68,41,86,90,75,96,53,61,57,83,23,76,44,9,40,6,64,94,36,68,57,12,18,56,16,66,39,35,93,68,90,7,41,42,93,93,48,45,69,34,88,9,18,90,15,89,20,37,84,96,63,34,81,88,35,8,7,66,92,60,1,9,32,86,1,50,59,85,49,2,89,53,24,44,61,43,3,86,23,39,32,3,33,51,98,66,58,57,56,27,29,74,64,1,16,68,3,51,70,95,14,19,54,17,61,29,28,18,4,8,30,21,50,10,18,8,40,91,16,64,68,11,59,84,98,10,92,18,59,85,84,24,48,72,50,16,9,19,10,79,77,23,15,7,3,37,40,88,35,61,66,98,5,74,47,92,50,79,73,11,43,25,93,42,53,46,73,52,64,48,47,64,73,17,69,16,22,14,46,29,3,69,15,13,86,3,82,17,69,78,86,69,23,48,74,22,13,75,15,39,21,12,17,13,12,70,36,88,16,21,22,88,2,3,55,58,7,85,53,73,79,66,61,39,12,42,71,72,95,22,48,34,68,55,6,40,93,97,26,4,56,95,39,70,51,45,75,94,49,58,90,82,12,94,22,34,57,83,79,8,96,84,28,91,8,10,2,17,21,42,67,73,64,38,40,81,57,15,93,70,26,74,31,96,75,16,39,67,46,37,68,98,5,15,3,84,97,71,73,1,27,65,40,23,74,22,29,49,66,62,96,71,85,40,78,9,30,40,29,68,38,88,50,95,12,49,94,84,21,73,72,52,39,51,19,87,42,8,24,42,71,3,82,65,97,38,42,98,81,31,81,47,33,91,25,34,96,28,46,18,63,29,26,21,22,97,58,29,16,1,38,31,76,39,22,69,98,41,36,29,24,63,66,43,55,72,72,79,80,62,80,70,73,49,29,11,3,84,76,20,35,40,11,47,17,33,31,32,70,38,53,54,11,4,29,38,18,89,27,96,27,57,3,64,83,3,48,28,67,38,6,72,96,8,51,86,1,1,88,70,87,40,34,71,68,74,77,52,38,64,55,17,63,9,41,29,46,93,23,93,11,78,25,21,79,76,3,62,25,18,72,1,9,22,66,81,9,30,60,91,23,72,29,96,36,56,14,67,73,82,1,62,15,86,49,56,97,97,95,39,2,10,58,51,62,3,4,34,35,79,47,14,94,49,66,76,74,35,47,63,31,93,31,71,23,39,87,91,7,36,3,65,12,90,78,14,63,25,74,82,67,98,46,28,66,42,60,50,58,42,90,44,93,4,72,84,26,10,76,17,93,22,83,73,39,81,1,40,36,21,35,66,40,51,5,7,37,64,86,68,46,54,64,30,25,33,69,54,94,48,55,10,6,16,28,47,86,31,44,10,12,98,1,51,31,88,35,31,87,6,44,95,36,2,95,91,7,45,5,28,30,35,88,66,18,42,44,21,60,65,35,64,53,96,73,36,11,22,80,34,28,90,7,77,21,96,84,75,19,9,31,96,67,33,94,26,34,45,3,59,26,47,57,57,20,49,97,39,83,29,92,11,6,58,25,22,89,78,69,77,48,3,44,64,67,19,80,89,41,6,47,41,67,91,38,83,83,38,12,29,39,5,5,23,9,23,63,69,69,67,60,34,27,32,49,22,23,93,44,47,24,63,87,95,80,36,85,2,95,82,35,49,44,96,3,83,83,61,76,92,42,52,43,29,52,72,70,50,97,93,84,57,85,25,95,56,57,49,70,48,77,94,78,23,22,96,86,65,43,90,42,47,56,48,56,39,63,48,14,5,67,20,56,5,50,74,6,22,58,91,34,12,26,12,66,88,31,71,64,82,86,32,40,56,19,40,86,51,56,4,13,48,11,32,76,80,2,61,58,7,70,44,83,49,89,80,2,30,4,34,49,75,23,94,47,61,68,88,28,17,76,58,74,87,21,28,21,48,97,17,41,82,7,5,48,89,14,41,76,23,72,52,1,3,15,72,10,79,87,78,7,33,79,12,21,54,36,91,73,15,89,26,27,39,7,28,7,88,95,41,69,97,80,72,78,35,15,43,33,21,91,59,97,6,56,34,44,16,15,21,4,25,13,22,56,75,75,98,85,4,70,49,61,83,2,24,20,20,82,21,62,12,90,78,71,15,80,56,98,73,83,26,82,95,60,30,62,50,84,10,25,89,73,54,36,10,32,43,29,5,22,20,54,48,17,42,84,47,14,58,48,70,23,44,70,63,28,86,46,57,57,86,78,12,21,46,60,48,10,19,98,91,87,63,25,9,89,41,39,4,83,33,83,28,41,68,66,78,24,68,52,36,59,4,12,5,2,13,35,35,65,924596

2019/Day 13/Part 1/intcode.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from queue import Queue
2+
3+
4+
class IntCode:
5+
PARAMETER_COUNTS = {1: 3, 2: 3, 3: 1, 4: 1, 5: 2, 6: 2, 7: 3, 8: 3, 9: 1, 99: 0}
6+
7+
class ParameterModes:
8+
POSITION_MODE = 0
9+
IMMEDIATE_MODE = 1
10+
RELATIVE_MODE = 2
11+
12+
def __init__(self, raw_int_code):
13+
self._int_code = [int(x) for x in raw_int_code.split(',')]
14+
self._input_buffer = Queue()
15+
self.output_buffer = Queue()
16+
self._i_ptr = 0
17+
self._relative_base = 0
18+
self.finished = False
19+
20+
def execute_till_halt(self):
21+
while self._i_ptr < len(self._int_code):
22+
opcode, parameter_modes = self._get_instruction(self._i_ptr)
23+
parameter_count = IntCode.PARAMETER_COUNTS[opcode]
24+
parameter_modes = parameter_modes + [0]*(parameter_count - len(parameter_modes))
25+
parameters = self._get_parameters(self._i_ptr + 1, parameter_count)
26+
27+
for j in range(len(parameters)):
28+
if parameter_modes[j] == IntCode.ParameterModes.IMMEDIATE_MODE:
29+
parameters[j] = self._i_ptr + j + 1
30+
elif parameter_modes[j] == IntCode.ParameterModes.RELATIVE_MODE:
31+
parameters[j] += self._relative_base
32+
33+
# Check if location is outside memory, adjust memory capacity if necessary
34+
if parameters[j] >= len(self._int_code):
35+
self._int_code += [0]*(parameters[j] + 1 - len(self._int_code))
36+
37+
if opcode == 1:
38+
self._int_code[parameters[2]] = self._int_code[parameters[0]] + self._int_code[parameters[1]]
39+
elif opcode == 2:
40+
self._int_code[parameters[2]] = self._int_code[parameters[0]] * self._int_code[parameters[1]]
41+
elif opcode == 3:
42+
if self._is_input_available():
43+
self._int_code[parameters[0]] = self._get_input()
44+
else:
45+
return
46+
elif opcode == 4:
47+
self.output_buffer.put(self._int_code[parameters[0]])
48+
elif opcode == 5:
49+
if self._int_code[parameters[0]] != 0:
50+
self._i_ptr = self._int_code[parameters[1]]
51+
continue
52+
elif opcode == 6:
53+
if self._int_code[parameters[0]] == 0:
54+
self._i_ptr = self._int_code[parameters[1]]
55+
continue
56+
elif opcode == 7:
57+
if self._int_code[parameters[0]] < self._int_code[parameters[1]]:
58+
self._int_code[parameters[2]] = 1
59+
else:
60+
self._int_code[parameters[2]] = 0
61+
elif opcode == 8:
62+
if self._int_code[parameters[0]] == self._int_code[parameters[1]]:
63+
self._int_code[parameters[2]] = 1
64+
else:
65+
self._int_code[parameters[2]] = 0
66+
elif opcode == 9:
67+
self._relative_base += self._int_code[parameters[0]]
68+
elif opcode == 99:
69+
self.finished = True
70+
return
71+
72+
self._i_ptr += parameter_count + 1
73+
74+
def _get_instruction(self, opcode_location):
75+
full_opcode = self._int_code[opcode_location]
76+
opcode = full_opcode % 100
77+
parameter_modes = [int(x) for x in str(full_opcode)[:-2][::-1]]
78+
return (opcode, parameter_modes)
79+
80+
def _get_parameters(self, location, number_of_parameters):
81+
return self._int_code[location : location + number_of_parameters]
82+
83+
def _is_input_available(self):
84+
return not self._input_buffer.empty()
85+
86+
def _get_input(self):
87+
return self._input_buffer.get_nowait()
88+
89+
def add_input(self, input_):
90+
self._input_buffer.put(input_)

2019/Day 13/Part 2/__main__.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Advent of Code 2019, Day 13, Part 2
2+
3+
import os
4+
from intcode import IntCode
5+
6+
7+
class Tiles:
8+
EMPTY = 0
9+
WALL = 1
10+
BLOCK = 2
11+
PADDLE = 3
12+
BALL = 4
13+
14+
15+
class Joystick:
16+
LEFT = -1
17+
NEUTRAL = 0
18+
RIGHT = 1
19+
20+
21+
class Directions:
22+
LEFT = 0
23+
UNDEFINED = 1
24+
RIGHT = 2
25+
26+
27+
class Ball:
28+
def __init__(self, x_pos=0):
29+
self.x = x_pos
30+
self.old_x = x_pos
31+
self.direction = Directions.UNDEFINED
32+
33+
def set_x_pos(self, x):
34+
self.old_x = self.x
35+
self.x = x
36+
37+
def update_direction(self):
38+
if self.old_x < self.x:
39+
self.direction = Directions.RIGHT
40+
elif self.old_x > self.x:
41+
self.direction = Directions.LEFT
42+
else:
43+
self.direction = Directions.UNDEFINED
44+
45+
46+
class Paddle:
47+
def __init__(self, x_pos=0):
48+
self.x = x_pos
49+
50+
51+
def main():
52+
text_input = get_raw_input()
53+
int_code = IntCode(text_input)
54+
55+
int_code._int_code[0] = 2
56+
57+
tiles = dict()
58+
59+
score = 0
60+
paddle = Paddle()
61+
ball = Ball()
62+
while not int_code.finished:
63+
for co in tiles.keys():
64+
if tiles[co] == Tiles.PADDLE:
65+
paddle.x = co[0]
66+
elif tiles[co] == Tiles.BALL:
67+
ball.set_x_pos(co[0])
68+
69+
ball.update_direction()
70+
71+
if ball.direction == Directions.LEFT and paddle.x > ball.x:
72+
int_code.add_input(Joystick.LEFT)
73+
elif ball.direction == Directions.RIGHT and ball.x > paddle.x:
74+
int_code.add_input(Joystick.RIGHT)
75+
elif paddle.x > ball.x:
76+
int_code.add_input(Joystick.LEFT)
77+
elif paddle.x < ball.x:
78+
int_code.add_input(Joystick.RIGHT)
79+
else:
80+
int_code.add_input(Joystick.NEUTRAL)
81+
82+
int_code.execute_till_halt()
83+
84+
output_instructions = list(int_code.output_buffer.queue)
85+
86+
i = 0
87+
while i + 2 < len(output_instructions):
88+
x, y, id_ = output_instructions[i:i+3]
89+
if x == -1 and y == 0:
90+
score = id_
91+
else:
92+
tiles[(x, y)] = id_
93+
i += 3
94+
95+
print_tiles(tiles)
96+
97+
print('Score after all blocks are broken:', score)
98+
99+
100+
def print_tiles(tiles):
101+
os.system('clear')
102+
min_x = min(tiles.keys(), key=lambda c: c[0])[0]
103+
max_x = max(tiles.keys(), key=lambda c: c[0])[0]
104+
min_y = min(tiles.keys(), key=lambda c: c[1])[1]
105+
max_y = max(tiles.keys(), key=lambda c: c[1])[1]
106+
for y in range(min_y, max_y + 1):
107+
line = ''
108+
for x in range(min_x, max_x + 1):
109+
if (x, y) in tiles.keys():
110+
tile = tiles[(x, y)]
111+
if tile == Tiles.EMPTY:
112+
line += ' '
113+
elif tile == Tiles.WALL:
114+
line += u'\u2588'*2
115+
elif tile == Tiles.BLOCK:
116+
line += u'\u001b[32m\u228f\u2290\u001b[0m' # '\u001b[32m\u2588\u001b[0m'*2
117+
elif tile == Tiles.PADDLE:
118+
line += u'\u001b[34m\u2acd\u2ace\u001b[0m'
119+
elif tile == Tiles.BALL:
120+
line += u'\u001b[31m\u2b24 \u001b[0m'
121+
else:
122+
line += ' '
123+
print(line)
124+
125+
126+
def get_raw_input():
127+
return open(retrieve_input_file_path(), 'r').read()
128+
129+
130+
def retrieve_input_file_path():
131+
return os.path.join(os.path.dirname(__file__), 'input.txt')
132+
133+
134+
if __name__ == '__main__':
135+
main()

0 commit comments

Comments
 (0)