def part_two(input_data: List[str]): code = [] jmp_nop_locs = [] for i, line in enumerate(input_data): cmd, arg = line.split() arg = int(arg) if any(cmd == x for x in ["jmp", "nop"]): jmp_nop_locs.append(i) code.append(Command(cmd, arg)) for loc in jmp_nop_locs: if code[loc].cmd == "jmp": code[loc].cmd = "nop" result = run_code(code) if result != -1: return result code[loc].cmd = "jmp" elif code[loc].cmd == "nop": code[loc].cmd = "jmp" result = run_code(code) if result != -1: return result code[loc].cmd = "nop" else: raise SolutionNotFoundException(2020, 8, 2) raise SolutionNotFoundException(2020, 8, 2)
def part_one(input_data: List[str]): input_data = [int(x) for x in input_data] for num in input_data: if 2020 - num in input_data: return num * (2020 - num) raise SolutionNotFoundException(2020, 1, 1)
def part_two(input_data: List[str]): answer = increment_password_part_two(input_data[0]) if not answer: raise SolutionNotFoundException(2015, 11, 2) return answer
def part_one(input_data: List[str]): answer = find_combinations(input_data) if not answer: raise SolutionNotFoundException(2015, 17, 1) return answer
def part_one(input_data: List[str], c1: int = 61, c2: int = 17): bots = defaultdict(list) outputs = defaultdict(int) instructions = input_data remaining = [] while len(instructions) > 0: for instruction in instructions: i_split = instruction.split() if i_split[0] == "value": bots[i_split[5]].append(int(i_split[1])) elif i_split[0] == "bot": if len(bots[i_split[1]]) == 2: if bots[i_split[1]] == [c1, c2] or bots[i_split[1]] == [c2, c1]: return int(i_split[1]) low = min(bots[i_split[1]]) high = max(bots[i_split[1]]) bots[i_split[1]].clear() if i_split[5] == "bot": bots[i_split[6]].append(low) else: outputs[i_split[6]] = low if i_split[10] == "bot": bots[i_split[11]].append(high) else: outputs[i_split[11]] = high else: remaining.append(instruction) instructions = remaining.copy() remaining = [] raise SolutionNotFoundException(2016, 10, 1)
def part_one(input_data: List[str], screen_width: int = 50, screen_height: int = 6): screen = [["." for x in range(screen_width)] for y in range(screen_height)] for command_str in input_data: command = command_str.split() if command[0] == "rect": size = command[1].split("x") for x in range(int(size[0])): for y in range(int(size[1])): screen[y][x] = "#" elif command[0] == "rotate": x = int(command[2].split("=")[-1]) amount = int(command[-1]) new_col = [] for i in range(screen_height): new_col.append(screen[i][x]) for i in range(screen_height): screen[i][x] = new_col[(i - amount) % screen_height] elif command[0] == "rotate": y = int(command[2].split("=")[-1]) amount = int(command[-1]) new_row = [] for j in range(screen_width): new_row.append(screen[y][j]) for j in range(screen_width): screen[y][j] = new_row[(j - amount) % screen_width] else: raise SolutionNotFoundException(2016, 8, 1) # print_screen(screen) lit_pixels = 0 for row in screen: lit_pixels += row.count("#") return lit_pixels
def part_two(input_data: List[str]): answer = find_different_ways(input_data) if not answer: raise SolutionNotFoundException(2015, 17, 2) return answer
def part_two(input_data: List[str]): for code in input_data: if is_real(code): name = decrypt_name(code) if name == "northpole object storage": return int(code.split("-")[-1].split("[")[0]) raise SolutionNotFoundException(2016, 4, 2)
def part_two(input_data: List[str]): codes = [] for code in input_data: codes.append(get_seat_from_code(code)) codes.sort() for i in range(len(codes) - 1): if codes[i + 1] - codes[i] == 2: return codes[i] + 1 raise SolutionNotFoundException(2020, 5, 2)
def part_two(input_data: List[str]): input_data = [int(x) for x in input_data] for num1 in input_data: for num2 in input_data: if 2020 - num1 - num2 in input_data: return num1 * num2 * (2020 - num1 - num2) raise SolutionNotFoundException(2020, 1, 2)
def part_one(input_data: List[str]): acc = 0 ip = 0 visited = set() while ip < len(input_data): if ip in visited: return acc visited.add(ip) cmd_split = input_data[ip].split() if cmd_split[0] == "acc": acc += int(cmd_split[1]) ip += 1 elif cmd_split[0] == "jmp": ip += int(cmd_split[1]) elif cmd_split[0] == "nop": ip += 1 else: raise SolutionNotFoundException(2020, 8, 1) raise SolutionNotFoundException(2020, 8, 1)
def part_one(input_data: List[str], preamble_length: int = 25): nums = [int(x) for x in input_data] cur_pos = preamble_length while cur_pos < len(nums): if is_valid(nums[cur_pos - preamble_length:cur_pos], nums[cur_pos]): cur_pos += 1 else: return nums[cur_pos] raise SolutionNotFoundException(2020, 9, 1)
def part_two(input_data: List[str]): floor = 0 for i in range(len(input_data[0])): if input_data[0][i] == "(": floor += 1 if input_data[0][i] == ")": floor -= 1 if floor == -1: return i + 1 raise SolutionNotFoundException(2015, 1, 2)
def part_two(input_data: List[str], times=40): value = input_data[0] for i in range(0, 50): value = lookAndSay(value) answer = len(value) if not answer: raise SolutionNotFoundException(2015, 10, 2) return answer
def part_one(input_data: List[str]): pos = 0 depth = 0 for command_str in input_data: command = command_str.split() if command[0] == "forward": pos += int(command[1]) elif command[0] == "up": depth -= int(command[1]) elif command[0] == "down": depth += int(command[1]) else: raise SolutionNotFoundException(2021, 2, 1) return pos * depth
def part_one(input_data: List[str]): nums = [int(x) for x in input_data[0].split(",")] input_data = input_data[2:] boards = list(parse_boards(input_data)) for i in range(len(nums)): for board in boards: if isWinner(board, nums[:i + 1]): boardSum = 0 for row in board: for num in row: if num not in nums[:i + 1]: boardSum += num return boardSum * nums[i] raise SolutionNotFoundException(2021, 4, 1)
def part_two(input_data: List[str], preamble_length: int = 25): nums = [int(x) for x in input_data] cur_pos = preamble_length while cur_pos < len(nums): if is_valid(nums[cur_pos - preamble_length:cur_pos], nums[cur_pos]): cur_pos += 1 else: break cont_sum = find_continuous_sum(nums, nums[cur_pos]) if len(cont_sum) == 0: raise SolutionNotFoundException(2020, 9, 2) return min(cont_sum) + max(cont_sum)
def run_code(code: List[Command]): acc = 0 ip = 0 visited = set() while ip < len(code): if ip in visited: return -1 visited.add(ip) cmd = code[ip] if cmd.cmd == "acc": acc += cmd.arg ip += 1 elif cmd.cmd == "jmp": ip += cmd.arg elif cmd.cmd == "nop": ip += 1 else: raise SolutionNotFoundException(2020, 8, 2) return acc
def wrapper(*args, **kwargs): try: start = time.perf_counter() solution = func(*args, **kwargs) if solution is None: raise SolutionNotFoundException(year, day, part) diff = (time.perf_counter() - start) * 1000 if not RUNNING_BENCHMARKS: console.print(f"{prefix}{solution} in {diff:.2f} ms") except (ValueError, ArithmeticError, TypeError): console.print_exception() except SolutionNotFoundException: console.print(f"{prefix}[red]solution not found") else: if RUNNING_BENCHMARKS: return diff return solution