コード例 #1
0
def part2():
    box_ids = list(read_file(2))
    for box_id in box_ids:
        for box_id2 in box_ids:
            result = id_match(box_id, box_id2)
            if result:
                return result
コード例 #2
0
def part1():
    sum2 = 0
    sum3 = 0
    lines = read_file(2)
    for result in count(lines):
        if result[0]:
            sum2 += 1
        if result[1]:
            sum3 += 1
    return sum2 * sum3
コード例 #3
0
ファイル: day1.py プロジェクト: Zomis/AdventOfCode
from days import read_file

value: int = 0
for line in read_file(1):
    value += int(line)
print(value)


def repeat(input: list) -> int:
    value = 0
    seen: set = set()
    while True:
        for num in input:
            value += int(num)
            if value in seen:
                return value
            seen.add(value)

mylist = list(read_file(1))
print(repeat(mylist))
コード例 #4
0
ファイル: day4.py プロジェクト: Zomis/AdventOfCode
def day4() -> (int, int):
    events = sorted(list(Event(datetime.strptime(line[1:17], "%Y-%m-%d %H:%M"), get_guard(line), event_type(line)) for line in read_file(4)), key=operator.attrgetter("time"))

    guard = -1
    guardsleep = defaultdict(lambda: GuardSleep(0, 0, []))
    for event in events:
        if event.guard >= 0:
            guard = event.guard

        if event.event == EventType.FALLS_ASLEEP:
            guardsleep[guard].last_sleep = event.time.minute

        if event.event == EventType.WAKES_UP:
            guardsleep[guard].sleep_total += event.time.minute - guardsleep[guard].last_sleep
            guardsleep[guard].add_sleeps(event.time.minute)

    most_sleepy_guard_number = max(guardsleep, key=(lambda key: guardsleep[key].sleep_total))

    most_sleepy_guard = guardsleep[most_sleepy_guard_number]
    part1_result = most_sleepy_guard_number * mode(sorted(most_sleepy_guard.sleeps))

    # Part 2
    MostSleepy = namedtuple('MostCommon', ['id', 'minute', 'amount'])
    most_sleepy = MostSleepy(0, 0, 0)
    for k in guardsleep:
        current_guard = guardsleep[k]
        try:
            most_common_minute = mode(sorted(current_guard.sleeps))
            amount = len(list((m for m in current_guard.sleeps if m == most_common_minute)))
            if amount > most_sleepy.amount:
                most_sleepy = MostSleepy(k, most_common_minute, amount)
        except statistics.StatisticsError:
            print("No unique most common minute for " + str(k))

    return part1_result, most_sleepy.id * most_sleepy.minute
コード例 #5
0
from days import read_file
from collections import defaultdict


def make_rect(line: str):
    split1 = line.strip().split(" @ ")
    split2 = split1[1].split(": ")
    positions = split2[0].split(",")
    size = split2[1].split("x")
    id = split1[0].split("#")[1]
    return tuple(
        map(lambda x: int(x),
            (id, positions[0], positions[1], size[0], size[1])))


boxes = list(make_rect(line) for line in read_file(3))


def filled_box(box):
    for x in range(box[3]):
        for y in range(box[4]):
            yield str(box[1] + x) + "_" + str(box[2] + y)


world = defaultdict(lambda: 0)
for box in boxes:
    print(box)
    for pos in filled_box(box):
        world[pos] += 1
print(len(list(v for v in world.values() if v > 1)))