def main(): inputs = util.get_puzzle_input() width = 25 height = 6 num_layers = len(inputs) // (width * height) layers = [] for i in range(num_layers): layers.append(inputs[i * width * height:(i + 1) * width * height]) checksum_layer = min(layers, key=lambda t: t.count('0')) print('Checksum =', checksum_layer.count('1') * checksum_layer.count('2')) layers = [np.array([int(c) for c in layer]) for layer in layers] running_image = np.zeros(shape=(width * height)) running_image.fill(2) for layer in layers: running_image[running_image == 2] = layer[running_image == 2] running_image = running_image.reshape((height, width)) for i in range(height): for j in range(width): char = '#' if running_image[i, j] else ' ' print(char, end='') print()
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] # Part A # Hole exists with a place to land # (NOT A OR NOT B OR NOT C) AND D commands = inspect.cleandoc(""" OR A J AND B J AND C J NOT J J AND D J WALK """) + '\n' springbot(memory, commands) # Part B # Hole exists, with a place to land # After landing, can either step forward or immediately jump again # NOT (A AND B AND C) AND (D AND (E OR H)) commands = inspect.cleandoc(""" OR A J AND B J AND C J NOT J J OR E T OR H T AND D T AND T J RUN """) + '\n' springbot(memory, commands)
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] game = TextGame(memory) game.explore_all() game.security_checkpoint()
def parse_input(): inputs = [ line.strip() for line in util.get_puzzle_input().split('\n') if line.strip() ] outputs = {} for line in inputs: reaction = {} reactants, product = line.split('=>') num_product, product = product.strip().split() num_product = int(num_product) reaction['product'] = product reaction['num_product'] = num_product reaction['reactants'] = [] reaction['line'] = line.strip() for reactant in reactants.split(','): num_reactant, reactant = reactant.strip().split() num_reactant = int(num_reactant) reaction['reactants'].append( dict(num=num_reactant, reactant=reactant)) outputs[product] = reaction return outputs
def parse_input(): """ Returns a list of tuples, each one representing a shuffle. Each tuple (a,b) gives the location of a card after shuffling, y = (a*x + b) % deck_size. This uses the correct % operator, which returns a value on [0,deck_size), not the broken % operator, which returns a value on (-deck_size,deck_size). When I make my C++ version, it will need to be y = (((a*x + b) % deck_size) + deck_size) % deck_size """ lines = [line.strip() for line in util.get_puzzle_input().split('\n') if line.strip()] output = [] for line in lines: if line=='deal into new stack': output.append( (-1,-1) ) elif line.startswith('deal with increment'): n = int(line.split()[-1]) output.append( (n,0) ) elif line.startswith('cut'): n = int(line.split()[-1]) output.append( (1,-n) ) else: raise ValueError('Unknown shuffle type: "{}"'.format(line)) return output
def part_a(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] game = Game() interp = Interpreter(memory, input_callback=game.best_dir, output_callback=game.set_tile) interp.iterate_until_done() print('Num block:', sum(1 for tile in game.tiles.values() if tile == 2))
def main(): inputs = util.get_puzzle_input() maze = Maze.parse_input(inputs) print('Part a:', maze.fastest_solution()) maze.adjust_start() #maze.draw() print('Part b:', maze.fastest_solution())
def part_b(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] memory[0] = 2 game = Game() interp = Interpreter(memory, input_callback=game.best_dir, output_callback=game.set_tile) interp.iterate_until_done() print('Winning score:', game.score)
def get_edges(): inputs = util.get_puzzle_input().split('\n') edges = [] for i in inputs: if i: i = i.split(')') edges.append((i[0], i[1])) return set(edges)
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] print(apply_func(12, 2, memory)) for a in range(100): for b in range(100): output = apply_func(a, b, memory) if output == 19690720: print(a, b)
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] best = max(itertools.permutations(list(range(5))), key=lambda seq: test_sequence(memory, seq)) print(best, test_sequence(memory, best)) best = max(itertools.permutations(list(range(5, 10))), key=lambda seq: test_sequence(memory, seq)) print(best, test_sequence(memory, best))
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] interp = Interpreter(memory, input_val = 1) interp.iterate_until_done() print(interp.output_val) interp = Interpreter(memory, input_val = 2) interp.iterate_until_done() print(interp.output_val)
def part_a(): # 17 minutes inputs = util.get_puzzle_input() vals = [int(c) for c in inputs if c.strip()] length = len(vals) vals_a = vals[:] for i in range(100): vals = fft_iter(vals) first_8 = ''.join(map(str, vals[:8])) print(first_8)
def main(): inputs = util.get_puzzle_input() field = [list(line) for line in inputs.split('\n') if line.strip()] asteroids = [(j, i) for i, line in enumerate(field) for j, char in enumerate(line) if char == '#'] best = max(asteroids, key=lambda pos: num_visible(asteroids, pos)) print(best) print(num_visible(asteroids, best)) order = list(order_destroyed(asteroids, best)) print(order[199])
def part_b(): inputs = util.get_puzzle_input() message_input = int(inputs[:7]) vals = [int(c) for c in inputs if c.strip()] vals = vals * 10000 for i in range(100): vals = fft_iter_second_half(vals) output = ''.join(map(str, vals)) print(output[message_input:message_input + 8])
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] print('Part 1') interp = Interpreter(memory[:], input_val=1, output_callback=print) interp.iterate_until_done() print('Part 2') interp = Interpreter(memory[:], input_val=5, output_callback=print) interp.iterate_until_done() print('Done')
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] s = Scaffolds(memory) print(sum(x * y for (x, y) in s.intersections())) s.give_directions( main='A,C,C,B,C,B,C,B,A,A', a='L,6,R,8,L,4,R,8,L,12', b='L,12,L,6,L,4,L,4', c='L,12,R,10,L,4', feed=False, )
def main(): wire_a, wire_b, *rest = util.get_puzzle_input().split('\n') path_a = parse_wire(wire_a) path_b = parse_wire(wire_b) intersections = set(path_a) & set(path_b) manhattan = lambda p: abs(p[0]) + abs(p[1]) distance = lambda p: path_a[p] + path_b[p] closest = min(intersections, key=manhattan) print(closest, manhattan(closest)) closest = min(intersections, key=distance) print(closest, distance(closest))
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] robot = Robot() def output_callback(val): robot.command(val) def input_callback(): return robot.camera() interp = Interpreter(memory, input_callback=input_callback, output_callback=output_callback) interp.iterate_until_done() print(len(robot.colors)) robot.display()
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] # Explore until the oxygen system is found bot = RepairBot(memory) found_oxygen = lambda bot: any(t == 3 for t in bot.tiles.values()) bot.fully_explore(stop_func=found_oxygen) goal = [pos for pos, t in bot.tiles.items() if t == 3][0] print(len(bot.path_from_start[goal])) # Start the bot exploring again, but starting at the oxygen system. bot.goto(goal) bot.tiles = {goal: 3} bot.path_from_start = {goal: []} bot.fully_explore() print(max(len(path) for path in bot.path_from_start.values())) bot.display()
def part_a_by_array(): lines = [line.strip() for line in util.get_puzzle_input().split('\n') if line.strip()] num_cards = 10007 deck = list(range(num_cards)) for line in lines: if line=='deal into new stack': deck = stack(deck) elif line.startswith('deal with increment'): n = int(line.split()[-1]) deck = increment(deck, n) elif line.startswith('cut'): n = int(line.split()[-1]) deck = cut(deck, n) else: raise ValueError('Unknown shuffle type: "{}"'.format(line)) print(deck.index(2019))
def main(): inputs = [int(x) for x in util.get_puzzle_input().split()] total_fuel = sum(fuel(x) for x in inputs) print(total_fuel) total_fuel = sum(fuel(x, tyranny=True) for x in inputs) print(total_fuel)
def main(): text = util.get_puzzle_input() part_a(text) part_b(text)
x = int(a == b) self.set_param(3, x) self.ip += 4 def op_adjust_relative_base(self): a = self.get_param(1) self.relative_base += a self.ip += 2 def iterate_until_done(self): while not self.done and not self.paused: self.iteration() memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] @functools.lru_cache(maxsize=None) def is_tractor(x, y): interp = Interpreter(memory) interp.input_val = x interp.iterate_until_done() interp.input_val = y interp.iterate_until_done() return bool(interp.output_val) @functools.lru_cache(maxsize=None) def tractor_xrange(y): if y < 10:
def main(): inputs = util.get_puzzle_input() maze = Maze.parse_maze(inputs) dist = maze.fastest_solution() print(dist)
def main(): memory = [int(x.strip()) for x in util.get_puzzle_input().split(',')] network = Network(memory) import sys print('First repeated y NAT val:', network.run(int(sys.argv[-1])))
def main(): inputs = util.get_puzzle_input()