def part1(): wire_directions1 = load_txt_data("Day3/part1_data1.txt")[0].split(",") wire_directions2 = load_txt_data("Day3/part1_data2.txt")[0].split(",") # we save all wire positions and perform set operations afterwards: wire1_positions = [(0, 0)] wire2_positions = [(0, 0)] for wire_direction in wire_directions1: direction, amount = wire_direction[0], int(wire_direction[1:]) for i in range(amount): if direction == "U": wire1_positions.append( (wire1_positions[-1][0], wire1_positions[-1][1] + 1)) elif direction == "D": wire1_positions.append( (wire1_positions[-1][0], wire1_positions[-1][1] - 1)) elif direction == "R": wire1_positions.append( (wire1_positions[-1][0] + 1, wire1_positions[-1][1])) elif direction == "L": wire1_positions.append( (wire1_positions[-1][0] - 1, wire1_positions[-1][1])) for wire_direction in wire_directions2: direction, amount = wire_direction[0], int(wire_direction[1:]) for i in range(amount): if direction == "U": wire2_positions.append( (wire2_positions[-1][0], wire2_positions[-1][1] + 1)) elif direction == "D": wire2_positions.append( (wire2_positions[-1][0], wire2_positions[-1][1] - 1)) elif direction == "R": wire2_positions.append( (wire2_positions[-1][0] + 1, wire2_positions[-1][1])) elif direction == "L": wire2_positions.append( (wire2_positions[-1][0] - 1, wire2_positions[-1][1])) wire1_positions = set(wire1_positions) wire2_positions = set(wire2_positions) intersections = wire1_positions.intersection(wire2_positions) distances = {} for intersection in intersections: distances[intersection] = abs(intersection[0]) + abs(intersection[1]) print(list(sorted(distances, key=lambda k: distances[k])))
def part2(): data = util.load_txt_data("Day2/data1.txt") counter = 0 for line in data: rule, pwd = line.split(": ") if check_if_pwd_rule_applies_part2(rule, pwd): counter += 1 print("There are %d valid passwords" % counter)
def part1(): # load data data = util.load_txt_data("Day1/data1.txt") # transform to int: data = list(map(int, data)) # simply iterate over array and check if the pair matches the criteria: for i in range(len(data)): for j in range(i, len(data)): if data[i] + data[j] == 2020: print("At %d and %d, the sum is 2020" % (i, j), data[i] * data[j])
def part2(): # load data data = util.load_txt_data("Day1/data1.txt") # transform to int: data = list(map(int, data)) # same logic as in part 1 for i in range(len(data)): for j in range(i, len(data)): for k in range(j, len(data)): if data[i] + data[j] + data[k] == 2020: print("At %d and %d and %d, the sum is 2020" % (i, j, k), data[i] * data[j] * data[k])
def part2(): data = map(int, load_txt_data("Day1/part1_data.txt")) # we will store the data in a queue (fifo) data_queue = queue.Queue() [data_queue.put(weight) for weight in data] _sum = 0 while not data_queue.empty(): module_weight = data_queue.get() amount_fuel = int(module_weight / 3) - 2 if amount_fuel > 0: # if the fuel is above zero, we append it to the end _sum += amount_fuel data_queue.put(amount_fuel) print(_sum)
def part2(): data = load_txt_data("Day3/part2.txt") wire_directions1 = data[0].split(",") wire_directions2 = data[1].split(",") # first, make a list of all coordinates of wire 1: wire1_coords = [(0, 0)] for wire_direction in wire_directions1: direction, amount = wire_direction[0], int(wire_direction[1:]) for i in range(amount): if direction == "U": wire1_coords.append( (wire1_coords[-1][0], wire1_coords[-1][1] + 1)) elif direction == "D": wire1_coords.append( (wire1_coords[-1][0], wire1_coords[-1][1] - 1)) elif direction == "R": wire1_coords.append( (wire1_coords[-1][0] + 1, wire1_coords[-1][1])) elif direction == "L": wire1_coords.append( (wire1_coords[-1][0] - 1, wire1_coords[-1][1])) # next, we make the same for the wire2, but check if an intersection happend: wire2_coords = [(0, 0)] for wire_direction in wire_directions2: direction, amount = wire_direction[0], int(wire_direction[1:]) for i in range(amount): if direction == "U": wire2_coords.append( (wire2_coords[-1][0], wire2_coords[-1][1] + 1)) elif direction == "D": wire2_coords.append( (wire2_coords[-1][0], wire2_coords[-1][1] - 1)) elif direction == "R": wire2_coords.append( (wire2_coords[-1][0] + 1, wire2_coords[-1][1])) elif direction == "L": wire2_coords.append( (wire2_coords[-1][0] - 1, wire2_coords[-1][1])) # check if the last coord is in the wire 1: if wire2_coords[-1] in wire1_coords: # we found the first intersection! steps_wire_1 = wire1_coords.index(wire2_coords[-1]) + 1 steps_wire_2 = len(wire2_coords) print("Intersection in", steps_wire_1 + steps_wire_2 - 2, "steps")
import util # load the data: seat_codes = util.load_txt_data("Day5/data1.txt") def decode_seat_code(seat_code): # split row encoding from column encoding: row_code, column_code = seat_code[0:7], seat_code[7:10] # the codes are basically a binary number where B=0 and F=1, or L=0 and R=1 row_code = row_code.replace("F", "0").replace("B", "1") column_code = column_code.replace("L", "0").replace("R", "1") # convert to integer and determine the id: return int(row_code, 2) * 8 + int(column_code, 2) def part1(): # map all seat codes from input to their id: seat_ids = list(map(decode_seat_code, seat_codes)) # find the maximum seat id: print("highest seat id: %d" % max(seat_ids)) print(decode_seat_code("BFFFBBFRRR")) #part1() def part2():
import util tree_map = util.load_txt_data("Day3/data1.txt") TREE_MAP_HEIGHT = len(tree_map) TREE_MAP_WIDTH = len(tree_map[0]) def part1(): slope = [1, 3] start_coords = [0, 0] # top left corner (row, col) current_coords = start_coords encountered_trees = 0 while current_coords[ 0] < TREE_MAP_HEIGHT: # while not at the bottom of the map # check if there is a tree at current_coords if tree_map[current_coords[0]][current_coords[1]] == "#": encountered_trees += 1 # update the current position current_coords[0] += slope[0] current_coords[1] += slope[1] current_coords[1] %= TREE_MAP_WIDTH print("encountered %d trees" % encountered_trees) #part1() def check_encountered_trees(slope):
def part1(): data = map(int, load_txt_data("Day1/part1_data.txt")) _sum = 0 for module_weight in data: _sum += int(module_weight / 3) - 2 print(_sum)