21201, 1, 0, -4, 21102, 1, 1, -1, 2207, -4, -2, 10, 1006, 10, 624, 21102, 1, 0, -1, 22202, -2, -1, -2, 2107, 0, -3, 10, 1006, 10, 646, 22101, 0, -1, 1, 21102, 646, 1, 0, 106, 0, 521, 21202, -2, -1, -2, 22201, -4, -2, -4, 109, -5, 2106, 0, 0 ] machine = IntCode(data) painted_color = defaultdict(int) pos = (0, 0) direction = (0, 1) # for puzzle 2: painted_color[pos] = 1 while not machine.is_finished(): machine.set_inputs([painted_color[pos]]) out = machine.get_outputs() painted_color[(pos)] = out[0] direction = rotate(direction, not out[1]) pos = (pos[0] + direction[0], pos[1] + direction[1]) # Output for puzzle 1: print(len(painted_color)) min_x = min(painted_color.keys(), key=lambda x: x[0])[0] max_x = max(painted_color.keys(), key=lambda x: x[0])[0] min_y = min(painted_color.keys(), key=lambda x: x[1])[1]
# build up the game for s in split(states, 3): if s[2] == 4: ball_pos_x = s[0] elif s[2] == 3: last_js_x = s[0] field[(s[0], s[1])] = s[2] # Solution for puzzle 1 print(len(list(filter(lambda x: x == 2, field.values())))) # play the game states = game.get_outputs() while not game.is_finished() or len(states) > 0: move = 0 for s in split(states, 3): if s[:2] == [-1, 0]: # Solution for puzzle 2 print("\rScore: {}".format(s[2]), end="") continue # update ball and paddle position if s[2] == 4: ball_pos_x = s[0] elif s[2] == 3: last_js_x = s[0] # paddle movement