Beispiel #1
0
    # test MYInt class
    i1 = MyInt(8)
    i2 = MyInt(5)
    assert 13 == len(i1 + i2)
    assert 40 == len(i1 * i2)
    assert 40 == len(i1 - i2)

    testcases = []
    testcases.append(("2 * 3 + (4 * 5)", 26, 46))
    testcases.append(("5 + (8 * 3 + 9 + 3 * 4 * 3)", 437, 1445))
    testcases.append(
        ("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))", 12240, 669060))
    testcases.append(
        ("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2", 13632, 23340))

    for t in testcases:
        assert t[1] == calculate(t[0]), f"Test case {t} failed"

    from aoc_utils import load_list
    homework = load_list("input/d18_pt1.txt")
    ans1 = sum(calculate(s) for s in homework)
    print(f"Answer to part 1: {ans1}")

    # part 2: easy-peasy! just use truediv for +
    assert 13 == len(i1 / i2)

    for t in testcases:
        assert t[2] == calculate(t[0], True), f"Test case {t} failed"

    ans2 = sum(calculate(s, True) for s in homework)
    print(f"Answer to part 2: {ans2}")
Beispiel #2
0
    test_data = """F10
N3
F7
R90
F11"""

    test_route = test_data.split()

    testship = Ship()
    for s in test_route:
        testship.execute_step(s)
        # print(s,testship.p)
    assert 25 == testship.get_manhattan_dist(), "Test case pt1 failed"

    from aoc_utils import load_list
    route = load_list("input/d12_pt1.txt")
    ship = Ship(p0=[0, 0])
    for s in route:
        ship.execute_step(s)
    ans1 = ship.get_manhattan_dist()
    print(f"Answer part 1: {ans1}")

    testship2 = Ship(wp0=[10, 1])
    for s in test_route:
        testship2.execute_step(s)
        # print(s,testship2.p,testship2.wp)
    assert 286 == testship2.get_manhattan_dist(), "Test case pt2 failed"

    ship2 = Ship(wp0=[10, 1])
    for s in route:
        ship2.execute_step(s)
Beispiel #3
0

def check_validity2(t):
    # t is a tuple with (char,pos1,pos2,pw)
    # note: pos 1-indexed
    return ((t[3][t[1]] == t[0]) + (t[3][t[2]] == t[0])) == 1


if __name__ == "__main__":
    t1_data = """1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc""".split("\n")
    t1_valid = 2

    parsed_pws_t = parse_data(t1_data)
    print(parsed_pws_t)
    assert sum(map(check_validity,
                   parsed_pws_t)) == t1_valid, "test case 1 failed"

    from aoc_utils import load_list
    data = load_list("input/d02_p1.txt")

    parsed_pws = parse_data(data)
    ans = sum(map(check_validity, parsed_pws))
    print(f"Answer part 1: {ans}")

    ## Part 2
    assert sum(map(check_validity2, parsed_pws_t)) == 1, "test case 2 failed"

    ans2 = sum(map(check_validity2, parsed_pws))
    print(f"Answer part 2: {ans2}")
Beispiel #4
0
    all_ingredients, all_allergens, allergens_to_ingredients = parse_foods(foodlist)
    
    # print(allergens_to_ingredients)
    possibly_unsafe_ingredients = set()
    for ingredients in allergens_to_ingredients.values():
        possibly_unsafe_ingredients |= ingredients
    # print(possibly_unsafe_ingredients)
    
    return len(all_ingredients) - sum(all_ingredients.count(ingredient) for ingredient in possibly_unsafe_ingredients)

if __name__ == "__main__":
    from aoc_utils import load_list
    
    testcase = """mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)""".splitlines()
    
    assert 5 == count_safe_ingredients(testcase), "Test case part 1 failed"
    
    real_foodlist = load_list("input/d21_pt1.txt")
    
    ans1 = count_safe_ingredients(real_foodlist)
    print(f"Answer to part 1: {ans1}")
    
    # Part 2: figure out which is which
    assert "mxmxvkd,sqjhc,fvjkl" == get_canonical_list(testcase), "Test case part 2 failed"
    
    ans2 = get_canonical_list(real_foodlist)
    print(f"Answer to part 2: {ans2}")
    
Beispiel #5
0
    return int(
        s.replace("B", "1").replace("F", "0").replace("L",
                                                      "0").replace("R", "1"),
        2)


if __name__ == "__main__":
    testseat = "FBFBBFFRLR"
    assert parse_row(testseat) == 44, "rowtest failed"
    assert parse_column(testseat) == 5, "columntest failed"
    assert get_seat_id(testseat) == 357, "seat test failed"

    assert get_seat_id("BFFFBBFRRR") == 567, "test2 failed"
    assert get_seat_id("FFFBBBFRRR") == 119, "test3 failed"
    assert get_seat_id("BBFFBBFRLL") == 820, "test4 failed"

    # Part 1: get highest seat id
    from aoc_utils import load_list
    passes = load_list("input/d05_pt1.txt")
    ans = max(map(get_seat_id, passes))
    print(f"Answer part 1: {ans}")

    # Part 2: get own seat that is a gap between two others
    all_seats = sorted(list(map(get_seat_id, passes)))
    for i, seat in enumerate(all_seats):
        if all_seats[i + 1] - seat == 2:
            print(
                f"Seats {seat} and {all_seats[i+1]} occupied: my seat is {seat+1}"
            )
            break
Beispiel #6
0
    return s


def count_children(parent):
    if parent not in p_to_c:
        return 0
    s = 0
    for child in p_to_c[parent]:
        n = p_to_c[parent][child]
        s += n + n * count_children(child)
    return s


if __name__ == "__main__":
    from aoc_utils import load_list
    testrules = load_list("input/d07_test.txt")
    # print(testrules)

    p_to_c, c_to_p = make_rule_dicts(testrules)
    # print(p_to_c)
    # print(c_to_p)
    # print(get_all_parents("shiny gold"))
    print(count_parents("shiny gold"))
    assert 4 == count_parents("shiny gold"), "num parents test failed"

    rules = load_list("input/d07_pt1.txt")

    p_to_c, c_to_p = make_rule_dicts(rules)

    ans1 = count_parents("shiny gold")
    print(f"Answer part 1: {ans1}")
Beispiel #7
0
a
b
c

ab
ac

a
a
a
a

b"""
    import re
    testgroups = re.split("\n\n", testinput)
    assert 11 == sum(map(count_unique_answers_for_group,
                         testgroups)), "test case 1 failed"

    from aoc_utils import load_list
    groups = load_list("input/d06_pt1.txt", "\n\n")
    # print(groups)

    ans1 = sum(map(count_unique_answers_for_group, groups))
    print(f"Answer part 1: {ans1}")

    ## Part 2
    assert 6 == sum(map(count_all_answers_for_group,
                        testgroups)), "test case 2 failed"

    ans2 = sum(map(count_all_answers_for_group, groups))
    print(f"Answer part 2: {ans2}")
Beispiel #8
0
def sum_invalid_fields(rules, tickets):
    # return the sum of all fields that do not fit in any of the rules
    all_valid_numbers = set(inner for outer in rules.values()
                            for inner in outer)
    invalid_sum = sum(val for ticket in tickets for val in ticket
                      if val not in all_valid_numbers)
    return invalid_sum


def validate_ticket(all_valid_numbers, ticket):
    return all(val in all_valid_numbers for val in ticket)


if __name__ == "__main__":
    from aoc_utils import load_list
    testinput = load_list("input/d16_test.txt")

    rules, mine, others = extract_input(testinput)
    assert 71 == sum_invalid_fields(rules, others), "Test case pt1 failed"

    rules, mine, others = extract_input(load_list("input/d16_pt1.txt"))
    ans1 = sum_invalid_fields(rules, others)
    print(f"Answer pt1: {ans1}")

    # Pt 2: first discard invalid tickets
    all_valid_numbers = set(inner for outer in rules.values()
                            for inner in outer)
    valid_tickets = [
        ticket for ticket in others
        if validate_ticket(all_valid_numbers, ticket)
    ]
Beispiel #9
0
        x += dx
    return s


def generate_chart_counter(chart):
    def chart_counter(slope):
        return count_trees(chart, slope)

    return chart_counter


if __name__ == "__main__":
    from aoc_utils import load_list

    test_check = 7
    test_data = load_list("input/d03_test.txt")
    # print(test_data)

    test_ans = count_trees(test_data, (3, 1))
    assert test_ans == test_check, "test case 1 failed"

    data = load_list("input/d03_p1.txt")
    ans1 = count_trees(data, (3, 1))
    print(f"Answer to part 1: {ans1}")

    ## Part 2
    slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]

    test_counts_check = [2, 7, 3, 4, 2]
    test_prod = 336
Beispiel #10
0
if __name__ == "__main__":
    t0, buses = testinput.split()
    t0 = int(t0)
    buses = [int(b) for b in buses.split(",") if b != "x"]

    # print(t0,buses)

    # Part 1 is simple: b - t % b is the remaining wait time
    tw = [b - (t0 % b) for b in buses]
    minT = min(tw)
    minB = buses[tw.index(minT)]
    assert minT * minB == 295, "test part 1 failed"

    from aoc_utils import load_list
    t0, buses_txt = load_list("input/d13_pt1_laura.txt")
    t0 = int(t0)
    buses = [int(b) for b in buses_txt.split(",") if b != "x"]
    tw = [b - (t0 % b) for b in buses]
    minT = min(tw)
    minB = buses[tw.index(minT)]
    print(minT, minB)
    ans1 = minT * minB
    print(f"Answer to part 1: {ans1}")

    # Part 2 is hard! modulo and MMI is not fun
    from aoc_utils import prime_factors
    # print(prime_factors(1068781+4))

    t0_min = 100000000000000
Beispiel #11
0
    test_input = """.#.
..#
###""".splitlines()
    testcells = init_conway(test_input)
    print_conway(testcells)

    # cells = step_conway(testcells)
    # print_conway(cells)

    cells = testcells.copy()
    for i in range(6):
        cells = step_conway(cells)
    assert 112 == len(cells), "Test case part 1 failed"

    realcells = init_conway(load_list("input/d17_pt1.txt"))

    cells = realcells.copy()
    for i in range(6):
        cells = step_conway(cells)
    ans1 = len(cells)
    print(f"Answer part 1: {ans1}")

    testcells4d = init_conway(test_input, dims=4)
    # print(testcells4d)
    for i in range(6):
        testcells4d = step_conway4(testcells4d)
    assert 848 == len(testcells4d), "Test case pt2 failed"

    realscells4d = init_conway(load_list("input/d17_pt1.txt"), dims=4)
    for i in range(6):