Example #1
0
from collections import Counter

from aoc import get_input_as, submit


inp = get_input_as(int, sep=',')
vals = Counter()

for i in inp:
    vals[i] += 1

maxval = max(inp)

most_common = vals.most_common()
weight = Counter()
for nn, (val, count) in enumerate(most_common):
    for i in range(maxval):
        weight[i] += abs(val - i) * count


submit(weight.most_common()[-1][1])
Example #2
0
from collections import defaultdict
from aoc import get_input_as, submit


inp = get_input_as(int, sep=',')

lanternfish = defaultdict(int)
triggers = defaultdict(list)

for i in inp:
    lanternfish[i + 1] += 1
counter = len(inp)

for i in range(1, 256 + 1):
    if i in lanternfish:
        counter += lanternfish[i]
        lanternfish[i + 7] += lanternfish[i]
        lanternfish[i + 9] += lanternfish[i]
        del lanternfish[i]

submit(counter)
Example #3
0
    clean3 = clean_func(three)
    clean9 = clean_func(in_9)
    assert len(clean9) == 3

    c_val = [x for x in clean9 if x not in clean5]
    assert len(c_val) == 1
    mapping['c'] = c_val[0]

    b_val = [x for x in clean9 if x not in clean3]
    assert len(b_val) == 1
    mapping['b'] = b_val[0]

    used_letters = []
    for k, v in mapping.items():
        if isinstance(v, str):
            used_letters.append(v)
    d_val = [x for x in 'abcdefg' if x not in used_letters]
    assert len(d_val) == 1
    mapping['d'] = d_val[0]

    flip_mapping = {}
    for k, v in mapping.items():
        flip_mapping[v] = k
    curr_ans = ''
    for x in output.split(' '):
        hashed_val = hash(''.join(sorted(flip_mapping[u] for u in x)))
        curr_ans += str(F_SEGMENTS[hashed_val])
    ans += int(curr_ans)

submit(ans)
Example #4
0
from aoc import get_input_as, submit

inp = get_input_as(int, sep=',')


class Lanternfish:
    def __init__(self, age):
        self.age = age

    def pass_day(self):
        self.age -= 1
        if self.age < 0:
            self.age = 6
            newfish = Lanternfish(8)
            return [newfish]
        return []

    def __repr__(self) -> str:
        return str(self.age)


fishes = list(map(Lanternfish, inp))

for i in range(80):
    for f in list(fishes):
        fishes.extend(f.pass_day())

answer = len(fishes)
submit(answer)
Example #5
0
min_delay_at = None

at = np.array([0, 0])
step = 0

for direction, length in cable2:
    delta = directions[direction]
    for _ in range(length):
        at += delta
        step += 1
        if at.tostring() in grid.keys():
            dist = np.abs(at).sum()
            if dist < min_dist:
                min_dist = dist
                min_at = at

            delay = step + grid[at.tostring()]
            if delay < min_delay:
                min_delay = delay
                min_delay_at = at

print(min_dist)

correct = aoc.submit(min_dist, day)
print(f'Answer 1 correct: {correct}')

print(min_delay)

correct = aoc.submit(min_delay, day, 2)
print(f'Answer 2 correct: {correct}')
Example #6
0
    x2, y2 = map(int, c2.split(','))
    if x1 == x2:
        yf = min((y1, y2))
        yl = max((y1, y2))
        for y in range(yf, yl + 1):
            if (x1, y) in lines:
                overlaps.add((x1, y))
            lines.add((x1, y))
    elif y1 == y2:
        xf = min((x1, x2))
        xl = max((x1, x2))
        for x in range(xf, xl + 1):
            if (x, y1) in lines:
                overlaps.add((x, y1))
            lines.add((x, y1))
    else:
        dist = abs(x1 - x2) + 1

        currx = x1
        curry = y1
        xmag = int(math.copysign(1, x2 - x1))
        ymag = int(math.copysign(1, y2 - y1))
        for i in range(dist):
            if (currx, curry) in lines:
                overlaps.add((currx, curry))
            lines.add((currx, curry))
            currx += xmag
            curry += ymag

submit(len(overlaps))
Example #7
0
import aoc

day = 0
lines = aoc.get_input(day)

correct = aoc.submit(result, day)
print(f'Answer 1 correct: {correct}')

correct = aoc.submit(result2, day, 2)
print(f'Answer 2 correct: {correct}')
Example #8
0
import aoc

day = 1
lines = aoc.get_input(day)


def get_fuel(mass: int) -> int:
    return mass // 3 - 2


total_fuel = sum([get_fuel(int(line)) for line in lines])

correct = aoc.submit(total_fuel, day)
print(f'Answer correct: {correct}')


def get_fuel_2(mass: int) -> int:
    fuel = mass // 3 - 2
    if fuel < 0:
        return 0

    fuel += get_fuel_2(fuel)

    return fuel


total_fuel_2 = sum([get_fuel_2(int(line)) for line in lines])

correct = aoc.submit(total_fuel_2, day, 2)
print(f'Answer 2 correct: {correct}')
Example #9
0
from aoc import get_input_as, submit

inp = get_input_as()

SEGMENTS = {
    0: 'abcefg',
    1: 'cf',
    2: 'acdeg',
    3: 'acdfg',
    4: 'bcdf',
    5: 'abdfg',
    6: 'abdefg',
    7: 'acf',
    8: 'abcdefg',
    9: 'abcdfg'
}

count = 0
for i in inp:
    patterns, output = i.split(' | ')
    for x in output.split(' '):
        if len(x) in [len(SEGMENTS[h]) for h in (1, 4, 7, 8)]:
            count += 1

submit(count)