# Tests test_input = """ 1163751742 1381373672 2136511328 3694931569 7463417111 1319128137 1359912421 3125421639 1293138521 2311944581 """.strip() assert_eq(40, lowest_total_risk(parse(test_input))) assert_eq(315, lowest_total_risk(five(parse(test_input)))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = lowest_total_risk() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = lowest_total_risk(five(parse())) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
image = enhance(algorithm, image, tick) return len(image) # Tests test_input = """ ..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# #..#. #.... ##..# ..#.. ..### """.strip() assert_eq(35, pixels_count(parse(test_input))) assert_eq(3351, pixels_count(parse(test_input), 50)) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = pixels_count() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = pixels_count(tick=50) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
4,1 0,13 10,12 3,4 3,0 8,4 1,10 2,14 8,10 9,0 fold along y=7 fold along x=5 """.strip() assert_eq(17, visible_dots(parse(test_input))) assert_eq(16, visible_dots(parse(test_input), first_fold_only=False)) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = visible_dots() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = visible_dots(first_fold_only=False) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
1 12 20 15 19 3 15 0 2 22 9 18 13 17 5 19 8 7 25 23 20 11 10 24 4 14 21 16 12 6 14 21 17 24 4 10 16 15 9 19 18 8 23 26 20 22 11 13 6 5 2 0 12 3 7 """ assert_eq(4512, play(*bingo(test_input.strip()))) assert_eq(1924, play(*bingo(test_input.strip()), let_paul_win=True)) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = play(*bingo()) PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = play(*bingo(), let_paul_win=True) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
return reduce((lambda acc, v: acc * v), subvalues) if type_id == 2: return min(subvalues) if type_id == 3: return max(subvalues) if type_id == 5: return subvalues[0] > subvalues[1] if type_id == 6: return subvalues[0] < subvalues[1] if type_id == 7: return subvalues[0] == subvalues[1] raise # Tests assert_eq(list("110100101111111000101000"), parse("D2FE28")) assert_eq([(6, 4, 2021)], read(parse("D2FE28"))) assert_eq( list("00111000000000000110111101000101001010010001001000000000"), parse("38006F45291200"), ) assert_eq([(1, 6, [(6, 4, 10), (2, 4, 20)])], read(parse("38006F45291200"))) assert_eq([(7, 3, [(2, 4, 1), (4, 4, 2), (1, 4, 3)])], read(parse("EE00D40C823060"))) assert_eq(16, version_sum(read(parse("8A004A801A8002F478")))) assert_eq(12, version_sum(read(parse("620080001611562C8802118E34")))) assert_eq(23, version_sum(read(parse("C0015000016115A2E0802F182340")))) assert_eq(31, version_sum(read(parse("A0016C880162017C3686B18A3D4780"))))
NH -> C HB -> C HC -> B HN -> C NN -> C BH -> H NC -> B NB -> B BN -> B BB -> N BC -> B CC -> N CN -> C """.strip() assert_eq(1588, difference_minmax_frequency_naive(parse(test_input))) assert_eq(2188189693529, difference_minmax_frequency(parse(test_input))) MEMO_FREQ = {} # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = difference_minmax_frequency_naive() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = difference_minmax_frequency() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n")
def distinct_initial_velocities(target=parse()): _, xmax, ymin, _ = target x_highest_trajectory, y_highest_trajectory = highest_trajectory(target)[1] velocities = set() for x in range(x_highest_trajectory, xmax + 1): for y in range(ymin - 1, y_highest_trajectory + 1): if compute_trajectory(target, x, y): velocities.add((x, y)) return len(velocities) # Tests test_input = "target area: x=20..30, y=-10..-5" assert_eq(45, highest_position(parse(test_input))) assert_eq(112, distinct_initial_velocities(parse(test_input))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = highest_position() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = distinct_initial_velocities() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
[0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0], ]) def tick_distrib_with_math(distrib=parse_distrib(DATA)): return sum(matrix_power(DISTRIB_TICK_MATRIX, 256) @ distrib) # Tests test_input = "3,4,3,1,2" assert_eq(5934, tick(parse(test_input))) assert_eq(26984457539, tick_distrib_with_math(parse_distrib(test_input))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = tick() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = tick_distrib_with_math() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
def cost(position, target, inc_cost): distance = abs(position - target) return distance * (distance + 1) / 2 if inc_cost else distance def min_fuel(positions=parse(DATA), inc_cost=False): return min([ sum([cost(p, i, inc_cost) for p in positions]) for i in range(min(positions), max(positions)) ]) # Tests test_input = "16,1,2,0,4,2,7,1,2,14" assert_eq(37, min_fuel(parse(test_input))) assert_eq(168, min_fuel(parse(test_input), True)) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = min_fuel() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = min_fuel(inc_cost=True) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
# Tests test_input = """ be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce """.strip() assert_eq(26, digits_with_unique_segments(parse(test_input.split("\n")))) assert_eq(61229, output_sum(parse(test_input.split("\n")))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = digits_with_unique_segments() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = output_sum() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
left, right = number return 3 * magnitude(left) + 2 * magnitude(right) def largest_magnitude(numbers=parse()): return max([magnitude(sum(list(p))) for p in permutations(numbers, 2)]) # Tests test_input = """ [1,1] [2,2] [3,3] [4,4] """.strip() assert_eq([[[[1, 1], [2, 2]], [3, 3]], [4, 4]], sum(parse(test_input.split("\n")))) test_input = """ [1,1] [2,2] [3,3] [4,4] [5,5] """.strip() assert_eq([[[[3, 0], [5, 3]], [4, 4]], [5, 5]], sum(parse(test_input.split("\n")))) test_input = """ [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]] [7,[[[3,7],[4,3]],[[6,3],[8,8]]]] [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]] [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]] [7,[5,[[3,8],[1,4]]]]
on x=-30..21,y=-8..43,z=-13..34 on x=-22..26,y=-27..20,z=-29..19 off x=-48..-32,y=26..41,z=-47..-37 on x=-12..35,y=6..50,z=-50..-2 off x=-48..-32,y=-32..-16,z=-15..-5 on x=-18..26,y=-33..15,z=-7..46 off x=-40..-22,y=-38..-28,z=23..41 on x=-16..35,y=-41..10,z=-47..6 off x=-32..-23,y=11..30,z=-14..3 on x=-49..-5,y=-3..45,z=-29..18 off x=18..30,y=-20..-8,z=-3..13 on x=-41..9,y=-7..43,z=-33..15 on x=-54112..-39298,y=-85059..-49293,z=-27449..7877 on x=967..23432,y=45373..81175,z=27513..53682 """.strip() assert_eq(590784, on_count(parse(test_input.split("\n")))) test_input = """ on x=-5..47,y=-31..22,z=-19..33 on x=-44..5,y=-27..21,z=-14..35 on x=-49..-1,y=-11..42,z=-10..38 on x=-20..34,y=-40..6,z=-44..1 off x=26..39,y=40..50,z=-2..11 on x=-41..5,y=-41..6,z=-36..8 off x=-43..-33,y=-45..-28,z=7..25 on x=-33..15,y=-32..19,z=-34..11 off x=35..47,y=-46..-34,z=-11..5 on x=-14..36,y=-6..44,z=-16..29 on x=-57795..-6158,y=29564..72030,z=20435..90618 on x=36731..105352,y=-21140..28532,z=16094..90401 on x=30999..107136,y=-53464..15513,z=8553..71215 on x=13528..83982,y=-99403..-27377,z=-24141..23996
# Tests test_input = """ dc-end HN-start start-kj dc-start dc-HN LN-dc HN-end kj-sa kj-HN kj-dc """.strip() assert_eq(19, paths_count(parse(test_input.split("\n")))) assert_eq(103, paths_count(parse(test_input.split("\n")), True)) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = paths_count() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = paths_count(double_small=True) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
# Tests test_input = """ 0,9 -> 5,9 8,0 -> 0,8 9,4 -> 3,4 2,2 -> 2,1 7,0 -> 7,4 6,4 -> 2,0 0,9 -> 2,9 3,4 -> 1,4 0,0 -> 8,8 5,5 -> 8,2 """.strip() assert_eq(5, vented(diagram(vents(test_input.split("\n"))))) assert_eq(12, vented(diagram(vents(test_input.split("\n")), diagonal=True))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = vented(diagram(vents())) PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = vented(diagram(vents(), diagonal=True)) PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
-393,719,612 -211,-452,876 808,-476,-593 -575,615,604 -485,667,467 -680,325,-822 -627,-443,-432 872,-547,-609 833,512,582 807,604,487 839,-516,451 891,-625,532 -652,-548,-490 30,-46,-14 """.strip() assert_eq(79, beacons_count(parse(test_input))) assert_eq(3621, most_distant_scanners(parse(test_input))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = beacons_count() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = most_distant_scanners() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
new_cost = energy + e if old_cost == None or new_cost < old_cost: pq.put((new_cost, b.__hash__(), b)) cost[b] = new_cost # Tests test_input = """ ############# #...........# ###B#C#B#D### #A#D#C#A# ######### """.strip() assert_eq(12521, dijkstra(parse(test_input.split("\n")))) assert_eq(44169, dijkstra(parse(test_input.split("\n"), unfold=True)) + 20) # No idea whatsoever where this 20 comes from MEMO_LEAST_ENERGY = {} # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = "depth_least_energy(parse(), 0)[0]" PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = dijkstra(parse(unfold=True)) + 20 PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000
# Tests test_input = """ 5483143223 2745854711 5264556173 6141336146 6357385478 4167524645 2176841721 6882881134 4846848554 5283751526 """.strip() assert_eq(1656, total_flashes(parse(test_input.split("\n")))) assert_eq(195, all_flash_cycle(parse(test_input.split("\n")))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = total_flashes() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = all_flash_cycle() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
grid.set_southerns(new_southerns) step += 1 return step # Tests test_input = """ v...>>.vv> .vv>>.vv.. >>.>v>...v >>v>>.>.v. v>v.vv.v.. >.>>..v... .vv..>.>v. v.v..>>v.v ....v..v.> """.strip() assert_eq(58, moving_steps(parse(test_input))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = moving_steps() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"Timed Results:\n{PART_1_TIME_MS=:.3f}\n")
# Tests test_input = """ [({(<(())[]>[[{[]{<()<>> [(()[<>])]({[<{<<[]>>( {([(<{}[<>[]}>{[]{[(<()> (((({<>}<{<{<>}{[]{[]{} [[<[([]))<([[{}[[()]]] [{[{({}]{}}([{[{{{}}([] {<[[]]>}<{[{[{[]{()[[[] [<(<(<(<{}))><([]([]() <{([([[(<>()){}]>(<<{{ <{([{{}}[<[[[<>{}]]]>[]] """.strip() assert_eq(26397, syntax_error_score(parse(test_input))) assert_eq(288957, middle_complete_score(parse(test_input))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = syntax_error_score() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = middle_complete_score() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
def three_largest_basins_product(heights=parse(DATA)): basins_size = [len(b) for b in basins(heights)] return reduce(mul, sorted(basins_size, reverse=True)[:3], 1) # Tests test_input = """ 2199943210 3987894921 9856789892 8767896789 9899965678 """.strip() assert_eq(15, risk_sum(parse(test_input.split("\n")))) assert_eq(1134, three_largest_basins_product(parse(test_input.split("\n")))) # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = risk_sum() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = three_largest_basins_product() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__": print(f"{PART_1_ANS=}\n") print(f"{PART_2_ANS=}\n")
outcomes.append([repeat * outcome[0], repeat * outcome[1]]) MEMO_PLAY[memo_key] = [sum(x) for x in zip(*outcomes)] return MEMO_PLAY[memo_key] def quantum_winner_universes_count(positions=parse()): return max(quantum_play(positions, (0, 0), True)) # Tests test_input = """ Player 1 starting position: 4 Player 2 starting position: 8 """.strip() assert_eq(739785, play(parse(test_input.split("\n")))) assert_eq(444356092776315, quantum_winner_universes_count(parse(test_input.split("\n")))) MEMO_PLAY = {} # Answers PART_1_START_TIME = timeit.default_timer() PART_1_ANS = play() PART_1_TIME_MS = (timeit.default_timer() - PART_1_START_TIME) * 1000 PART_2_START_TIME = timeit.default_timer() PART_2_ANS = quantum_winner_universes_count() PART_2_TIME_MS = (timeit.default_timer() - PART_2_START_TIME) * 1000 if __name__ == "__main__":