def puzzle11_two(file_name): lines = [line for line in striplines(file_name)] matrix = make_matrix(len(lines), len(lines[0]), lambda i, j: lines[i][j]) while True: matrix2 = deepcopy(matrix) occupied_seats = 0 for i in range(0, len(matrix)): for j, cell in enumerate(matrix[i]): touched = False if cell == "L": count = seats_in_line_of_site(matrix, i, j, 1) if count == 0: matrix2[i][j] = "#" occupied_seats = occupied_seats + 1 touched = True elif cell == "#": count = seats_in_line_of_site(matrix, i, j, 5) if count >= 5: matrix2[i][j] = "L" touched = True occupied_seats = occupied_seats - 1 if not touched: occupied_seats += 1 if (matrix[i][j] == "#") else 0 if matrix2 == matrix: break else: matrix = matrix2 return occupied_seats
def puzzle11_one(file_name): lines = [line for line in striplines(file_name)] matrix = make_matrix(len(lines), len(lines[0]), lambda i, j: lines[i][j]) while True: matrix2 = deepcopy(matrix) occupied_seats = 0 for i in range(0, len(matrix2)): for j in range(0, len(matrix2[i])): touched = False if matrix[i][j] == "L": items = adjacents(matrix, i, j) if items["#"] == 0: matrix2[i][j] = "#" occupied_seats = occupied_seats + 1 touched = True elif matrix[i][j] == "#": items = adjacents(matrix, i, j) if items["#"] >= 4: matrix2[i][j] = "L" touched = True occupied_seats = occupied_seats - 1 if not touched: occupied_seats += 1 if (matrix[i][j] == "#") else 0 if matrix2 == matrix: break else: matrix = matrix2 print(bool(matrix == matrix2)) print_matrix(matrix) print(occupied_seats) return matrix2
def puzzle_one(file_name): mask = "" mem = {} for line in striplines(file_name): lvalue, rvalue = line.split(" = ") if lvalue == "mask": mask = rvalue else: m = re.search("(\d+)", lvalue) mem[f"{m.group(0)}"] = apply_value(rvalue, mask) return sum([x for x in mem.values()])
def parse_bags(file_name): for line in striplines(file_name): words = line.split() children = ( [] if words[4] == "no" else [ Bag(words[i + 1], words[i + 2], int(words[i]), []) for i in range(4, len(words), 4) ] ) yield Bag(words[0], words[1], 1, children)
def product_of_all_slopes(file_name): lines = [line for line in striplines(file_name)] return reduce( lambda x, y: x * y, ( compute_trees(lines, right=1, down=1), compute_trees(lines, right=3, down=1), compute_trees(lines, right=5, down=1), compute_trees(lines, right=7, down=1), compute_trees(lines, right=1, down=2), ), )
def puzzle_one(file_name): timestamp, bus_ids = striplines(file_name) bus_ids = [int(bus) for bus in bus_ids.split(",") if bus != "x"] timestamp = int(timestamp) chosen_bus = None smallest_time = None for bus in bus_ids: wait_time = bus - (timestamp % bus) if not smallest_time or wait_time < smallest_time: smallest_time = wait_time chosen_bus = bus return chosen_bus * smallest_time
def puzzle_two(file_name): mask = "" mem = {} for line in striplines(file_name): lvalue, rvalue = line.split(" = ") if lvalue == "mask": mask = rvalue else: m = re.search("(\d+)", lvalue) address = int(m.group(0)) com = combos(address, mask) for target_address in com: mem[target_address] = int(rvalue) return sum([x for x in mem.values()])
def compute_trees_from_file(file_name): return compute_trees([line for line in striplines(file_name)], right=3, down=1)
def highest_boarding_pass(file_name): highest = 0 for line in striplines(file_name): highest = max(seat_id(line), highest) return highest
def puzzle_two(file_name): _, bus_ids = striplines(file_name) bus_ids = bus_ids.split(",") return puzzle_two_as_list(bus_ids)
def get_program(file_name): return [line.split(" ") for line in striplines(file_name)]
def instructions(file_name): for line in striplines(file_name): yield line[0], int(line[1:])