import advent_utils def puzzle_1(input_numbers): answers = set() for x in range(0, len(input_numbers)): for y in range(0, len(input_numbers)): if x != y and (input_numbers[x] + input_numbers[y] == 2020): answers.add(input_numbers[x] * input_numbers[y]) print(answers) def puzzle_2(input_numbers): answers = set() for x in range(0, len(input_numbers)): for y in range(0, len(input_numbers)): for z in range(0, len(input_numbers)): if x != y and x != z and (input_numbers[x] + input_numbers[y] + input_numbers[z] == 2020): answers.add(input_numbers[x] * input_numbers[y] * input_numbers[z]) print(answers) if __name__ == '__main__': input_data = advent_utils.load_input_from_file('inputs/input_01.txt') input_numbers = sorted([int(line) for line in input_data]) puzzle_1(input_numbers) puzzle_2(input_numbers)
def puzzle_2(the_map): r1_d1 = puzzle_1(the_map, 1, 1) r3_d1 = puzzle_1(the_map, 3, 1) r5_d1 = puzzle_1(the_map, 5, 1) r7_d1 = puzzle_1(the_map, 7, 1) r1_d2 = puzzle_1(the_map, 1, 2) print(r1_d1 * r3_d1 * r5_d1 * r7_d1 * r1_d2) def move_right(curr_x, end_x, right_increment): if curr_x + right_increment > end_x: steps = (curr_x + right_increment) - end_x return steps return curr_x + right_increment def move_down(curr_y, down_increment): return curr_y + down_increment if __name__ == '__main__': the_map = advent_utils.load_input_from_file('inputs/input_03.txt') # the_map = advent_utils.load_input_from_file('inputs/test_03.txt') print(puzzle_1(the_map, right_increment=3, down_increment=1)) puzzle_2(the_map)
for dz in (-1, 0, 1): if dx == dy == dz == 0: continue if grid.get((x + dx, y + dy, z + dz), False): neighbors += 1 return neighbors def count_active_neighbors_4d(grid, x, y, z, w): neighbors = 0 for dx in (-1, 0, 1): for dy in (-1, 0, 1): for dz in (-1, 0, 1): for dw in (-1, 0, 1): if dx == dy == dz == dw == 0: continue if grid.get((x + dx, y + dy, z + dz, w + dw), False): neighbors += 1 return neighbors if __name__ == '__main__': # input_data = advent_utils.load_input_from_file('inputs/input_17.txt') input_data = advent_utils.load_input_from_file('inputs/test_17.txt') print('puzzle_1: {s}'.format(s=puzzle_1(input_data))) print('puzzle_2: {s}'.format(s=puzzle_2(input_data)))
def get_occupied_neighbors(floor_map, x, y): neighbors = set() for xx in range(x - 1, x + 2): for yy in range(y - 1, y + 2): if 0 <= xx < len(floor_map) and 0 <= yy < len( floor_map[0]) and (xx != x or yy != y): neighbors.add(tuple([xx, yy])) occupied_neighbors = 0 for x, y in neighbors: if floor_map[x][y] == '#': occupied_neighbors += 1 return occupied_neighbors def flatten(floor_map): return ''.join(''.join(row) for row in floor_map) if __name__ == '__main__': # input_data = [list(line) for line in advent_utils.load_input_from_file('inputs/input_11.txt')] input_data = [ list(line) for line in advent_utils.load_input_from_file('inputs/test_11.txt') ] print('puzzle_1: {s}'.format(s=puzzle_1(input_data))) print('puzzle_2: {s}'.format(s=puzzle_2(input_data)))
def rotate_waypoint(waypoint, move_instr, magnitude): turns = int(magnitude / 90) wpx, wpy = waypoint for turn in range(turns): if move_instr == 'L': # Turn Left new_wpx, new_wpy = -wpy, wpx elif move_instr == 'R': # Turn Right new_wpx, new_wpy = wpy, -wpx wpx, wpy = new_wpx, new_wpy return (wpx, wpy) def move_2(coords, waypoint, magnitude): return ((coords[0] + waypoint[0] * magnitude), (coords[1] + waypoint[1] * magnitude)) if __name__ == '__main__': input_data = [ (x[0], int(x[1:])) for x in advent_utils.load_input_from_file('inputs/input_12.txt') ] # input_data = [(x[0], int(x[1:])) for x in advent_utils.load_input_from_file('inputs/test_12.txt')] print('puzzle_1: {s}'.format(s=puzzle_1(input_data))) print('puzzle_2: {s}'.format(s=puzzle_2(input_data)))
if adapter - current_joltage == 1: jump_1 += 1 current_joltage = adapter elif adapter - current_joltage == 3: jump_3 += 1 current_joltage = adapter return jump_1 * (jump_3 + 1) def puzzle_2(input_data): input_data = [0] + input_data + [input_data[-1] + 3] path = [1] for i in range(1, len(input_data)): answer = 0 for j in range(i): if input_data[j] + 3 >= input_data[i]: answer += path[j] path.append(answer) return path[-1] if __name__ == '__main__': # input_data = sorted([int(line) for line in advent_utils.load_input_from_file('inputs/input_10.txt')]) input_data = sorted([int(line) for line in advent_utils.load_input_from_file('inputs/test_10_1.txt')]) print('puzzle_1: {s}'.format(s=puzzle_1(input_data))) print('puzzle_2: {s}'.format(s=puzzle_2(input_data)))