|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, grid, graph, dot, assembly, re, itertools |
| 3 | +from collections import Counter, deque, defaultdict |
| 4 | + |
| 5 | +from compass import * |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +def lmap(func, *iterables): |
| 10 | + return list(map(func, *iterables)) |
| 11 | + |
| 12 | + |
| 13 | +def ints(s: str): |
| 14 | + return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +def positive_ints(s: str): |
| 18 | + return lmap(int, re.findall(r"\d+", s)) # thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +def floats(s: str): |
| 22 | + return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s)) |
| 23 | + |
| 24 | + |
| 25 | +def positive_floats(s: str): |
| 26 | + return lmap(float, re.findall(r"\d+(?:\.\d+)?", s)) |
| 27 | + |
| 28 | + |
| 29 | +def words(s: str): |
| 30 | + return re.findall(r"[a-zA-Z]+", s) |
| 31 | + |
| 32 | + |
| 33 | +test_data = {} |
| 34 | + |
| 35 | +test = 1 |
| 36 | +test_data[test] = { |
| 37 | + "input": """FBFBBFFRLR |
| 38 | +BFFFBBFRRR |
| 39 | +FFFBBBFRRR |
| 40 | +BBFFBBFRLL""", |
| 41 | + "expected": ["357, 567, 119, 820", "Unknown"], |
| 42 | +} |
| 43 | + |
| 44 | +test = "real" |
| 45 | +input_file = os.path.join( |
| 46 | + os.path.dirname(__file__), |
| 47 | + "Inputs", |
| 48 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 49 | +) |
| 50 | +test_data[test] = { |
| 51 | + "input": open(input_file, "r+").read(), |
| 52 | + "expected": ["878", "504"], |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +# -------------------------------- Control program execution ------------------------- # |
| 57 | + |
| 58 | +case_to_test = "real" |
| 59 | +part_to_test = 2 |
| 60 | + |
| 61 | +# -------------------------------- Initialize some variables ------------------------- # |
| 62 | + |
| 63 | +puzzle_input = test_data[case_to_test]["input"] |
| 64 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 65 | +puzzle_actual_result = "Unknown" |
| 66 | + |
| 67 | + |
| 68 | +# -------------------------------- Actual code execution ----------------------------- # |
| 69 | + |
| 70 | +if part_to_test == 2: |
| 71 | + seat_list = list(range(127 * 8 + 7 + 1)) |
| 72 | + |
| 73 | +max_seat_id = 0 |
| 74 | +for seat in puzzle_input.split("\n"): |
| 75 | + row = 0 |
| 76 | + column = 0 |
| 77 | + row_power = 6 |
| 78 | + col_power = 2 |
| 79 | + for letter in seat: |
| 80 | + if letter == "F": |
| 81 | + row_power = row_power - 1 |
| 82 | + elif letter == "B": |
| 83 | + row = row + 2 ** row_power |
| 84 | + row_power = row_power - 1 |
| 85 | + |
| 86 | + elif letter == "L": |
| 87 | + col_power = col_power - 1 |
| 88 | + elif letter == "R": |
| 89 | + column = column + 2 ** col_power |
| 90 | + col_power = col_power - 1 |
| 91 | + |
| 92 | + seat_id = row * 8 + column |
| 93 | + max_seat_id = max(seat_id, max_seat_id) |
| 94 | + |
| 95 | + if part_to_test == 2: |
| 96 | + seat_list.remove(seat_id) |
| 97 | + |
| 98 | +if part_to_test == 1: |
| 99 | + puzzle_actual_result = max_seat_id |
| 100 | +else: |
| 101 | + seat_list = [x for x in seat_list if x <= max_seat_id] |
| 102 | + |
| 103 | + puzzle_actual_result = max(seat_list) |
| 104 | + |
| 105 | + |
| 106 | +# -------------------------------- Outputs / results --------------------------------- # |
| 107 | + |
| 108 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 109 | +print("Expected result : " + str(puzzle_expected_result)) |
| 110 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments