def part_2(): test_data = { ('R75, D30, R83, U83, L12, D49, R71, U7, L72', 'U62, R66, U55, R34, D71, R55, D58, R83'): 610, ('R98, U47, R26, D63, R33, U87, L62, D20, R33, U53, R51', 'U98, R91, D20, R16, D67, R40, U7, R15, U6, R7'): 410 } for paths, expected in test_data.items(): points = [] for path in paths: points.append(get_points(path)) cross_points = get_cross_points(*points) totals = [] for cross_point in cross_points: # adding 1 because of 0 index p1 = points[0].index(cross_point) + 1 p2 = points[1].index(cross_point) + 1 totals.append(p1 + p2) min_total = min(totals) assert min_total == expected points = [] paths = readlines(INPUT_FILEPATH) for path in paths: points.append(get_points(path)) cross_points = get_cross_points(*points) totals = [] for cross_point in cross_points: # adding 1 because of 0 index p1 = points[0].index(cross_point) + 1 p2 = points[1].index(cross_point) + 1 totals.append(p1 + p2) min_total = min(totals) print('part_2 -> {}'.format(min_total))
def part_1(): test_data = {12: 2, 14: 2, 1969: 654, 100756: 33583} for mass, expected in test_data.items(): actual = calculate_module_fuel(mass) assert actual == expected masses = readlines(INPUT_FILEPATH) total_fuel = sum([calculate_module_fuel(mass) for mass in masses]) print('part_1 -> {}'.format(total_fuel))
def part_2(): test_data = {14: 2, 1969: 966, 100756: 50346} for mass, expected in test_data.items(): actual = calculate_additional_module_fuel(mass) assert actual == expected masses = readlines(INPUT_FILEPATH) total_fuel = sum( [calculate_additional_module_fuel(mass) for mass in masses]) print('part_2 -> {}'.format(total_fuel))
def part_1(): test_data = { '1,0,0,0,99': [2, 0, 0, 0, 99], '2,3,0,3,99': [2, 3, 0, 6, 99], '2,4,4,5,99,0': [2, 4, 4, 5, 99, 9801], '1,1,1,4,99,5,6,0,99': [30, 1, 1, 4, 2, 5, 6, 0, 99] } for program, expected in test_data.items(): result = run(program) assert result == expected program = readlines(INPUT_FILEPATH)[0] result = run(program)
def part_1(): test_data = { ('COM)B', 'B)C', 'C)D', 'D)E', 'E)F', 'B)G', 'G)H', 'D)I', 'E)J', 'J)K', 'K)L'): 42 } for data, expected in test_data.items(): G = create_graph(data) i, d = compute_jumps(G) assert i + d == expected data = readlines(INPUT_FILEPATH) G = create_graph(data) i, d = compute_jumps(G) print(i, d, i + d)
def part_1(): test_data = { (1, 0, 0, 0, 99): [2, 0, 0, 0, 99], (2, 3, 0, 3, 99): [2, 3, 0, 6, 99], (2, 4, 4, 5, 99, 0): [2, 4, 4, 5, 99, 9801], (1, 1, 1, 4, 99, 5, 6, 0, 99): [30, 1, 1, 4, 2, 5, 6, 0, 99] } for program, expected in test_data.items(): p = list(program) run(p) assert p == expected program = [int(num) for num in readlines(INPUT_FILEPATH)[0].split(',')] program[NOUN_POS] = 12 program[VERB_POS] = 2 output = run(program) print(output)
def part_2(): expected = 19690720 initial_program = [ int(num) for num in readlines(INPUT_FILEPATH)[0].split(',') ] noun_range = range(0, 100) verb_range = range(0, 100) program = list(initial_program) for noun in noun_range: for verb in verb_range: program[NOUN_POS] = noun program[VERB_POS] = verb output = run(program) if output == expected: print('noun -> {}, verb -> {}, total -> {}'.format( noun, verb, 100 * noun + verb)) return program = list(initial_program)
def part_1(): test_data = { ('R75, D30, R83, U83, L12, D49, R71, U7, L72', 'U62, R66, U55, R34, D71, R55, D58, R83'): 159, ('R98, U47, R26, D63, R33, U87, L62, D20, R33, U53, R51', 'U98, R91, D20, R16, D67, R40, U7, R15, U6, R7'): 135 } for paths, expected in test_data.items(): points = [] for path in paths: points.append(get_points(path)) cross_points = get_cross_points(*points) min_dist = min_distance(ORIGIN, cross_points) assert min_dist == expected points = [] paths = readlines(INPUT_FILEPATH) for path in paths: points.append(get_points(path)) cross_points = get_cross_points(*points) min_dist = min_distance(ORIGIN, cross_points) print('part_1 -> {}'.format(min_dist))
import reader as reader with open('text.txt', 'r') as reader: content = reader.readlines() reversed(content) with open('text.txt', 'w') as writer: for line in reversed(content): print(writer.write(line))
def part_2(): program = readlines(INPUT_FILEPATH)[0] result = run(program)