def test_rock_is_wrapped_by_paper():
    assert_equals(WINNER_HAND_2, rock_paper_scissors(ROCK, PAPER))
def test_scissors_blunted_by_rock():
    assert_equals(WINNER_HAND_2, rock_paper_scissors(SCISSORS, ROCK))
def test_paper_wraps_rock():
    assert_equals(WINNER_HAND_1, rock_paper_scissors(PAPER, ROCK))
Beispiel #4
0
            self.run()

        return False


instructions = read_lines("day08")
program = Program(instructions)

# Part one
program.terminates()

aoc_print(
    f"The program gets interrupted with an accumulator of {program.accumulator}."
)
assert_equals(1801, program.accumulator)


# Part two
def replace_all(base_list: [str], old: str, new: str) -> [[str]]:
    total = list()
    index = 0

    while True:
        tup = replace(list(base_list), old, new, index)
        if tup[1] == -1:
            break

        total.append(tup[0])
        index = tup[1]
Beispiel #5
0
    parts = line.split(" ")

    lower_bound = int(parts[0].split("-")[0])
    upper_bound = int(parts[0].split("-")[1])

    letter = parts[1][:-1]

    password = parts[2]

    letter_count = len([x for x in password if x == letter])

    if lower_bound <= letter_count <= upper_bound:
        valid_passwords += 1

aoc_print(f"{valid_passwords} passwords are valid. (old interpretation)")
assert_equals(424, valid_passwords)

# Part two
valid_passwords = 0

for line in lines:
    parts = line.split(" ")

    first_index = int(parts[0].split("-")[0]) - 1
    second_index = int(parts[0].split("-")[1]) - 1

    letter = parts[1][:-1]

    password = parts[2]

    if password[first_index] == letter and password[second_index] != letter\
Beispiel #6
0
from printer import aoc_print

groups = read_split_lines("day06")

# Part one
total_sum = 0
for group in groups:
    votes = set()
    for single_person in group:
        for question in single_person:
            votes.add(question)

    total_sum += len(votes)

aoc_print(f"The count of 'yes' votes is {total_sum}.")
assert_equals(6714, total_sum)

# Part two
total_sum = 0
for group in groups:
    votes = list()

    for single_person in group:
        for question in single_person:
            votes.append(question)

    for vote in set(votes):
        if votes.count(vote) == len(group):
            total_sum += 1

aoc_print(f"The count of 'yes' votes in every group is {total_sum}.")
Beispiel #7
0
from assertions import assert_equals
from input_reader import read_ints
from printer import aoc_print

numbers = read_ints("day09")
preamble = 25  # has to be 5 if you run the test set
start = 0

# Part one
while True:
    pairs = [(x, y) for x in numbers[start:preamble + start] for y in numbers[start:preamble + start] if
             x != y and x + y == numbers[preamble + start]]

    if len(pairs) == 0:
        aoc_print(f"The first number that does not have a pair is {numbers[preamble + start]}.")
        assert_equals(22406676, numbers[preamble + start])
        break

    start += 1

# Part two
invalid_number = numbers[preamble + start]

set_start = -1
set_end = -1

for i in range(0, preamble + start):
    total_sum = 0
    for j in range(i, preamble + start):
        total_sum += numbers[j]
Beispiel #8
0
def test_fizzbuzz_5_is_Buzz():
    assert_equals('Buzz', fizzbuzz(5))
Beispiel #9
0
def test_fizzbuzz_10_is_Buzz():
    assert_equals('Buzz', fizzbuzz(10))
Beispiel #10
0
def test_fizzbuzz_3_is_Fizz():
    assert_equals('Fizz', fizzbuzz(3))
Beispiel #11
0
def test_fizzbuzz_6_is_Fizz():
    assert_equals('Fizz', fizzbuzz(6))
Beispiel #12
0
def test_fizzbuzz_2_is_2():
    assert_equals('2', fizzbuzz(2))
Beispiel #13
0
    rule = rule[:-1].replace("bags", "").replace("bag", "")

    bag = rule.split(" contain ")[0].strip()
    container_bags = []

    container = rule.split(" contain ")[1].strip()
    if "," in container:
        for single_bag in container.split(", "):
            count = int(single_bag.split(" ")[0])
            bag_type = single_bag[len(single_bag.split(" ")[0]):].strip()
            container_bags.append((bag_type, count))
    elif "no " not in container:
        count = int(container.split(" ")[0])
        bag_type = container[
            len(rule.split(" contain ")[1].strip().split(" ")[0]):].strip()
        container_bags.append((bag_type, count))

    bag_rules[bag] = container_bags

# Part one
res = len([bag for bag in bag_rules.keys() if contains_bag(bag, "shiny gold")])

aoc_print(f"{res} bag colors can contain at least one shiny gold.")
assert_equals(302, res)

# Part two
res = count_bags("shiny gold")

aoc_print(f"It requires {res} bags in my shiny gold bag.")
assert_equals(4165, res)
Beispiel #14
0

def calc_seat_id(seat: (int, int)) -> int:
    return seat[0] * 8 + seat[1]


lines = read_lines("day05")

# Calculate seats
seats = list(map(calc_seat, lines))

# Part one
result = max([calc_seat_id(seat) for seat in seats])

aoc_print(f"The highest seat ID on the boarding pass is {result}.")
assert_equals(878, result)

# Part two
sorted_seats = sorted(seats, key=lambda tup: (tup[0], tup[1]))
min_seat = min(sorted_seats)
max_seat = max(sorted_seats)

all_seats = [(row, column) for row in range(0, 128) for column in range(0, 8)]

missing_seat = (0, 0)
for seat in all_seats:
    if (min_seat <= seat <= max_seat) and (seat not in seats):
        missing_seat = seat
        break

aoc_print(f"The seat id of your seat is {calc_seat_id(missing_seat)}.")
def test_rock_draws_with_rock():
    assert_equals(DRAW, rock_paper_scissors(ROCK, ROCK))
Beispiel #16
0
def test_fizzbuzz_15_is_FizzBuzz():
    assert_equals('FizzBuzz', fizzbuzz(15))
def test_rock_blunts_scissors():
    assert_equals(WINNER_HAND_1, rock_paper_scissors(ROCK, SCISSORS))
Beispiel #18
0
def test_fizzbuzz_1_is_1():
    assert_equals('1', fizzbuzz(1))
Beispiel #19
0
passports = []

# Parse passports
passport = {}
for line in read_lines("day04"):
    if not line.strip():
        passports.append(passport)
        passport = {}
        continue

    for pair in line.split(" "):
        passport[pair.split(":")[0]] = pair.split(":")[1]

passports.append(passport)

# Part one
count_valid_passports = len([x for x in passports if is_present(x)])

aoc_print(
    f"The passport list contains {count_valid_passports} valid passports.")
assert_equals(239, count_valid_passports)

# Part two
count_valid_passports = len(
    [x for x in passports if is_present(x) and is_valid(x)])

aoc_print(
    f"The passport list contains {count_valid_passports} valid passports.")
assert_equals(188, count_valid_passports)
Beispiel #20
0
from assertions import assert_equals
from input_reader import read_ints
from printer import aoc_print

ints = read_ints("day01")

# Part one
res = [x * y for x in ints for y in ints if x + y == 2020]
aoc_print(f"The first product is {res[0]}.")

assert_equals(1003971, res[0])

# Part two
res = [
    x * y * z for x in ints for y in ints for z in ints if x + y + z == 2020
]
aoc_print(f"The second product is {res[0]}.")

assert_equals(84035952, res[0])