Exemple #1
0
from shared import read_puzzle_input
import re
import functools
import string

req_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]

puzzle_input = read_puzzle_input(4, False)


def advent4_1():
    return len(filter_required())


def advent4_2():
    return len([
        z for z in [[
            x for x in y
            if (x[0] == "byr" and
                (len(x[1]) == 4 and 1920 <= int(x[1]) <= 2002)) or (
                    x[0] == "iyr" and
                    (len(x[1]) == 4 and 2010 <= int(x[1]) <= 2020)) or
            (x[0] == "eyr" and (len(x[1]) == 4 and 2020 <= int(x[1]) <= 2030))
            or (x[0] == "hgt" and
                ("cm" in x[1] and 150 <= int(x[1].split("cm")[0]) <= 193)) or (
                    x[0] == "hgt" and
                    ("in" in x[1] and 59 <= int(x[1].split("in")[0]) <= 76)) or
            (x[0] == "hcl" and (x[1][0] == "#" and len(x[1].split("#")[1]) == 6
                                and all(c in string.hexdigits
                                        for c in x[1].split("#")[1]))) or
            (x[0] == "ecl" and
Exemple #2
0
from shared import read_puzzle_input
import math

puzzle_input = read_puzzle_input(2)
puzzle_input = puzzle_input.splitlines()


def advent2_1():
    return sum([
        2 * y[0] * y[1] + 2 * y[1] * y[2] + 2 * y[2] * y[0] +
        math.prod(sorted(y)[0:2])
        for y in [list(map(int, x.split("x"))) for x in puzzle_input]
    ])


def advent2_2():
    return sum([
        y[0] * 2 + y[1] * 2 + math.prod(y)
        for y in [sorted(list(map(int, x.split("x")))) for x in puzzle_input]
    ])


print("\n\n*Advent 2:*")
print("\nPart 1:")
print(advent2_1())
print("\nPart 2:")
print(advent2_2())
Exemple #3
0
from shared import read_puzzle_input

puzzle_input = list(map(int, read_puzzle_input(1)))


def advent1_1():
    return [x*y for x in puzzle_input for y in puzzle_input if x + y == 2020][0]


def advent1_2():
    return [x*y*z for x in puzzle_input for y in puzzle_input for z in puzzle_input if x + y + z == 2020][0]


print("\n\n*Advent 1:*")
print("\nPart 1:")
print(advent1_1())
print("\nPart 2:")
print(advent1_2())