コード例 #1
0
ファイル: 20.py プロジェクト: andvagorion/adventofcode
def parse_pixels(file):
    lines = aoc.read_lines(file)[2:]
    pixels = {}
    for row in range(len(lines)):
        line = lines[row]
        for col in range(len(line)):
            pos = (col, row)
            val = to_bool(line[col])
            pixels[pos] = val
    return pixels
コード例 #2
0
ファイル: 22.py プロジェクト: andvagorion/adventofcode
def part1(filename):
    lines = aoc.read_lines(filename)
    reactor = cuboid(-50, 50, -50, 50, -50, 50)
    reactor_cubes = {(x, y, z): False for x in range(-50, 51) for y in range(-50, 51) for z in range(-50, 51)}
    
    for line in lines:
        parts = re.match(PATTERN, line)
        if parts:
            op, x0, x1, y0, y1, z0, z1 = parts.groups()
            cube = cuboid(x0, x1, y0, y1, z0, z1)
            
            if not math3.intersect_cuboid(reactor, cube):
                continue
        
            process(reactor_cubes, op, cube)
    
    print(sum(1 for val in reactor_cubes.values() if val))
コード例 #3
0
def calc(filename, times):
    lines = aoc.read_lines(filename)

    initial = [c for c in lines[0]]
    formula = dict()
    for i in range(len(initial) - 1):
        key = initial[i] + initial[i + 1]
        formula[key] = formula.get(key, 0) + 1

    counts = {key: initial.count(key) for key in set(initial)}

    rules = get_rules(lines)

    for _ in range(times):
        formula, counts = step(formula, rules, counts)

    mn = min(counts.values())
    mx = max(counts.values())

    return mx - mn
コード例 #4
0
ファイル: 19.py プロジェクト: andvagorion/adventofcode
def parse(file):
    lines = aoc.read_lines(file)
    scanners = {}

    scanner_num = -1
    for line in lines:
        if pattern_scanner.match(line):
            scanner_num = int(line.split()[2])
            scanners[scanner_num] = {
                'id': scanner_num,
                'pos': point3(0, 0, 0),
                'rot': [0, 0, 0, 0, 0, 0, 0, 0, 0],
                'ref': 0,
                'beacons': []
            }
        elif pattern_point.match(line):
            assert scanner_num >= 0
            x, y, z = [int(n) for n in line.split(',')]
            scanners[scanner_num]['beacons'].append(point3(x, y, z))

    return scanners
コード例 #5
0
ファイル: 12.py プロジェクト: andvagorion/adventofcode
from aoc import aoc
from aoc.point import point
from aoc.grid import grid
from itertools import permutations

lines = aoc.read_lines('data/12.txt')

caves = set()
for line in lines:
    c0 = line.split('-')[0]
    caves.add(c0)
    c1 = line.split('-')[1]
    caves.add(c1)
caves = list(caves)

connections = {}
for cave in caves: connections[cave] = []

for line in lines:
    c0 = line.split('-')[0]
    c1 = line.split('-')[1]
    if c1 != 'start':
        connections[c0].append(c1)
    if c1 != 'end' and c0 != 'start':
        connections[c1].append(c0)

def visit1(cave, node_count, total_count):
    pos = caves.index(cave)

    if cave.islower() and node_count[pos] == 1:
        return
コード例 #6
0
from aoc import aoc
from functools import reduce

lines = aoc.read_lines('data/08.txt')

def unique_patterns(line):
    first = line.split(' | ')[0]
    return [''.join(sorted(p)) for p in first.split()]

def find_mapping(line):
    patterns = unique_patterns(line)
    
    one = next(p for p in patterns if len(p) == 2)
    seven = next(p for p in patterns if len(p) == 3)
    four = next(p for p in patterns if len(p) == 4)
    eight = next(p for p in patterns if len(p) == 7)
    
    nine = next(p for p in patterns if len(p) == 6 and set(four).issubset(set(p)))
    six = next(p for p in patterns if len(p) == 6 and not set(one).issubset(set(p)))
    zero = next(p for p in patterns if len(p) == 6 and p != nine and p != six)

    two = next(p for p in patterns if len(p) == 5 and not set(p).issubset(set(nine)))
    five = next(p for p in patterns if len(p) == 5 and set(p).issubset(set(six)))
    three = next(p for p in patterns if len(p) == 5 and p != two and p != five)

    return [zero, one, two, three, four, five, six, seven, eight, nine]

def coded_numbers(line):
    second = line.split(' | ')[1]
    return [''.join(sorted(p)) for p in second.split()]
コード例 #7
0
from aoc import aoc

lines = [int(i) for i in aoc.read_lines('data/01.txt')]


def one():
    count = 0
    for i in range(len(lines) - 1):
        win = lines[i:i + 2]
        if win[1] > win[0]: count += 1
    return count


def two():
    prev = -1
    count = 0
    for i in range(len(lines) - 2):
        win = lines[i:i + 3]
        sum = win[0] + win[1] + win[2]
        if prev > 0 and sum > prev: count += 1
        prev = sum
    return count


print(one())
print(two())
コード例 #8
0
ファイル: 15.py プロジェクト: andvagorion/adventofcode
        neighbors = get_neighbors(current[X], current[Y])
        for neighbor in neighbors:
            risk_value = cave[neighbor[Y]][neighbor[X]]
            tentative = distances[current] + risk_value
            if tentative < distances[neighbor]:
                distances[neighbor] = tentative
                prev[neighbor] = current

            if neighbor not in visited:
                unvisited.put((distances[neighbor], neighbor))

    minimum_risk = distances[end]
    print(minimum_risk)


PART1 = 'data/15.txt'
PART2 = 'data/15-2.txt'

lines = aoc.read_lines(PART2)

ROWS = len(lines)
COLS = len(lines[0])

start = (0, 0)
end = (ROWS - 1, COLS - 1)

cave = [[int(i) for i in line] for line in lines]

solve(cave, start, end)
コード例 #9
0
ファイル: 25.py プロジェクト: andvagorion/adventofcode
        x = (boi.x + 1) % cucumbers.width
        if cucumbers.get(point(x, boi.y)) == '.':
            step.set(point(boi.x, boi.y), '.')
            step.set(point(x, boi.y), '>')
    return step


def move_south(cucumbers: grid) -> grid:
    step: grid = cucumbers.clone()
    eastie_bois = cucumbers.find_all(lambda cell: cell == 'v')
    for boi in eastie_bois:
        y = (boi.y + 1) % cucumbers.height
        if cucumbers.get(point(boi.x, y)) == '.':
            step.set(point(boi.x, boi.y), '.')
            step.set(point(boi.x, y), 'v')
    return step


lines = aoc.read_lines(DATA)

cucumbers = grid.with_values(lines)

i = 0
prev = None
while prev != cucumbers:
    prev = cucumbers.clone()
    cucumbers = move_east(cucumbers)
    cucumbers = move_south(cucumbers)
    i += 1

print(i)
コード例 #10
0
ファイル: 20.py プロジェクト: andvagorion/adventofcode
def parse_algorith(file):
    lines = aoc.read_lines(file)
    return [to_bool(c) for c in lines[0]]