コード例 #1
0
    @staticmethod
    def _parse_database_entry(database_entry: str) -> [int, int, str, str]:
        temp, password = database_entry.split(": ")
        temp, symbol = temp.split(" ")
        par1, par2 = temp.split("-")
        return int(par1), int(par2), symbol, password


class SumPasswordValidator(PasswordValidator):
    @staticmethod
    def check_password(minimum, maximum, symbol, password):
        return minimum <= password.count(symbol) <= maximum


class PositionalPasswordValidator(PasswordValidator):
    @staticmethod
    def check_password(index1, index2, symbol, password):
        return (password[index1 - 1] == symbol
                and password[index2 - 1] != symbol
                or password[index1 - 1] != symbol
                and password[index2 - 1] == symbol)


if __name__ == "__main__":
    data = DataReader.read_txt("day2.txt", str)
    solution = SumPasswordValidator().count_valid_passwords(data)
    print("Number of valid passwords method sum: {}".format(solution))

    solution = PositionalPasswordValidator().count_valid_passwords(data)
    print("Number of valid passwords method position: {}".format(solution))
コード例 #2
0
ファイル: day1.py プロジェクト: maelic13/adventofcode2020
import numpy as np
from typing import List

from src.year2020.infra.api import DataReader


def count_increases(measurements: List[int]) -> int:
    increased = 0
    last_measurement = np.Inf
    for measurement in measurements:
        if measurement > last_measurement:
            increased += 1
        last_measurement = measurement
    return increased


if __name__ == "__main__":
    data = DataReader.read_txt("day1.txt", int)

    single_increases = count_increases(data)
    print(f"Measurement increased {single_increases} times in single mode.")

    sliding_window_sum: List[int] = list()
    for i in range(3, len(data) + 3):
        sliding_window_sum.append(sum(data[(i - 3):i]))

    sliding_window_increases = count_increases(sliding_window_sum)
    print(
        f"Measurement increased {sliding_window_increases} times in sliding window mode."
    )
コード例 #3
0
ファイル: day3.py プロジェクト: maelic13/adventofcode2020
def count_trees_hit(slope_map: List, current_pos: np.ndarray,
                    slope: np.ndarray) -> int:
    slope = slope / gcd(slope[0], slope[1])
    trees_hit = 0
    while current_pos[1] < len(slope_map):
        x = int(current_pos[1])
        y = int(current_pos[0]) % len(slope_map[x])
        if slope_map[x][y] == "#":
            trees_hit += 1
        current_pos = np.add(current_pos, slope)
    return trees_hit


if __name__ == "__main__":
    input_data = DataReader.read_txt("day3.txt", str)

    single_slope = np.array((3, 1))
    result = count_trees_hit(input_data, np.array((0, 0)), single_slope)
    print("With slope {} you hit {} trees.".format((3, 1), result))

    result_list = [result]
    slopes = [
        np.array((1, 1)),
        np.array((5, 1)),
        np.array((7, 1)),
        np.array((1, 2))
    ]
    for single_slope in slopes:
        result = count_trees_hit(input_data, np.array((0, 0)), single_slope)
        result_list.append(result)
コード例 #4
0
ファイル: day4.py プロジェクト: maelic13/adventofcode2020
        ]
        return np.all([character in allowed for character in field_value])
    if field_name == "ecl":
        return field_value in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
    if field_name == "pid":
        try:
            int(field_value)
            success = True
        except ValueError:
            success = False
        return len(field_value) == 9 and success
    if field_name == "cid":
        return True
    return False


if __name__ == "__main__":
    input_data = DataReader.read_txt_in_batch("day4.txt")
    required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
    valid_passports = count_valid_passports(input_data, required_fields)
    print(
        "Number of valid passports counting in those without cid: {}.".format(
            valid_passports))

    valid_strict = count_valid_passports(input_data,
                                         required_fields,
                                         strict=True)
    print(
        "Number of strictly valid passports counting in those without cid: {}."
        .format(valid_strict))