コード例 #1
0
def main():
    tickets = get_input(INPUT_PATH)
    highest_seat_id = 0
    lowest_seat_id = -1
    sum_seat_id = 0
    seats = []

    for t in tickets:
        row, col, seat_id = get_row_col_id(t)
        sum_seat_id += seat_id
        if seat_id > highest_seat_id:
            highest_seat_id = seat_id
        if seat_id < lowest_seat_id or lowest_seat_id == -1:
            lowest_seat_id = seat_id
        seats.append((row, col, seat_id))

    part2 = (highest_seat_id*(highest_seat_id+1)/2)-(lowest_seat_id*(lowest_seat_id+1)/2) - sum_seat_id
    sorted_seats = sorted(seats, key=lambda x: x[2])
    missing_seat = 0
    for i, s in enumerate(sorted_seats):
        if sorted_seats[i+1][2] - s[2] > 1:
            missing_seat = s[2] + 1
            break

    print(f"Part1: highest seat id: {highest_seat_id}")
    print(f"Part2: your seat id: {missing_seat}")
コード例 #2
0
def main():
    jolts = [int(x) for x in get_input(INPUT_PATH)]

    used_jolts, diffs = get_joltage_diffs(jolts)
    part1 = calculate_part1_diffs(diffs)

    print(f"part1: # of 1 volt diffs * # of 3 volt diffs: {part1}")

    combos = get_joltage_combos(jolts)
    print(f"part2: # of different combinations: {combos}")
コード例 #3
0
def main():
    xmas_input = [int(x) for x in get_input(INPUT_PATH)]
    part1_res = check_validity(xmas_input)
    print(
        f"Part 1: first number that is not sum of 2 of 25 precursors: {part1_res[1]}"
    )
    part2_res = sorted(find_contiguous_sum(xmas_input, part1_res[1]))
    print(
        f"Part 2: min {part2_res[0]} + max {part2_res[-1]} of contiguous sum - {part2_res[0] + part2_res[-1]}"
    )
コード例 #4
0
def main():
    initial = get_input(INPUT_PATH)
    seats = SeatLayout(initial)

    seats.get_final_layout(1)
    print(f"part 1: occupied seats in final layout: {seats.occupied_seats()}")
    seats_2 = SeatLayout(initial)
    seats_2.get_final_layout(2)
    print(
        f"part 2: occupied seats in final_layout: {seats_2.occupied_seats()}")
コード例 #5
0
def main():
    rules = get_input(INPUT_PATH)

    bag_graph = make_graph(rules)

    shiny_gold_bag = bag_graph.get('shiny gold')
    shiny_gold_containers = shiny_gold_bag.count_containers()
    shiny_gold_contains = shiny_gold_bag.count_contains()
    print(f"part 1: {shiny_gold_containers} bags can contain a shiny gold bag")
    print(
        f"part 2: {shiny_gold_contains} bags are required inside of a shiny gold bag"
    )
コード例 #6
0
def main():
    dayOneInput = [int(x) for x in get_input(INPUT_PATH)]
    # sort array. then we can know if we've gone too far
    sortedInput = sorted(dayOneInput)

    answer = find_sum_two(sortedInput, 2020)
    if answer:
        print(f"Two entries that sum to 2020: {answer[0], answer[1]}")
        print(f"Part 1 answer: {answer[0] * answer[1]}")
    else:
        print("couldnt find a match")
    part2 = find_sum_three(sortedInput, 2020)
    if part2:
        print(f"three entries that sum to {part2}")
        print(f"part two answer: {part2[0] * part2[1] * part2[2]}")
コード例 #7
0
def main():
    day2Input = get_input(INPUT_PATH)

    valid_count_1 = 0
    valid_count_2 = 0
    for item in day2Input:
        lower_bound, upper_bound, char, pw = parse_pw_and_policy(item)
        is_valid_1 = validate_pw_count(char, lower_bound, upper_bound, pw)
        is_valid_2 = validate_pw_index(char, lower_bound, upper_bound, pw)
        if is_valid_1:
            valid_count_1 += 1
        if is_valid_2:
            valid_count_2 += 1

    print(f"Part 1: valid password count: {valid_count_1}")
    print(f"Part 2: valid password count: {valid_count_2}")
コード例 #8
0
def main():
    instructions = get_input(INPUT_PATH)
    p = Program(instructions)

    part_1 = p.run()
    print(
        f"Part 1: value in the accumulator prior to any command being run twice: {part_1}"
    )

    part2_instructions = instructions.copy()
    part2_instructions[
        265] = "nop -174"  # I just guessed. it was a big negative jump near the end
    p2_prog = Program(part2_instructions)
    part_2 = p2_prog.run()
    print(
        f"Part 2: value in accumulator after program terminates successfully: {part_2}"
    )
コード例 #9
0
def main():
    str_input = get_input(INPUT_PATH)

    trees = TreeCollection(str_input)
    vectors = [
        Vector(3, 1),
        Vector(1, 1),
        Vector(5, 1),
        Vector(7, 1),
        Vector(1, 2)
    ]
    collisions = [find_intersections(trees, x) for x in vectors]

    print(f"Part 1: you would hit {collisions[0]} trees")
    print(
        f"part 2: multiplied all together this is {functools.reduce(lambda a,b: a * b, collisions)}"
    )