示例#1
0
文件: 09.py 项目: hltk/adventofcode
def main():
    from aocd import data

    # removes all escaped characters
    data = re.sub(r'!.', '', data)

    # for part 2
    olen, cnt = len(data), data.count('>')

    data = re.sub(r'<[^>]*>', '', data)

    s = h = 0
    for c in data:
        if c == '{':
            h += 1
            s += h
        elif c == '}':
            h -= 1

    # part 1
    print(s)

    # part 2
    # amount of removed characters excluding the brackets
    print(olen - len(data) - cnt * 2)
示例#2
0
def parsed(data):
    asteroids = []
    rows = data.splitlines()
    for y, row in enumerate(rows):
        for x, val in enumerate(row):
            if val == "#":
                asteroids.append((x, y))
    assert len(asteroids) == data.count("#")
    return asteroids
示例#3
0
def get_distance_matrix(data):
    n_rows = 1 + data.count('\n')
    maze = np.fromstring(data.replace('\n', ''),
                         dtype='S1').reshape(n_rows, -1).astype('U1')
    targets = sorted([x for x in data if x.isdigit()])
    n = len(targets)

    states = []
    for target in targets:
        [row], [col] = np.where(maze == target)
        states.append(complex(col, -row))

    d = np.zeros((n, n), dtype=int)
    for a, b in combinations(range(n), 2):
        d[a, b] = d[b, a] = bfs(states[a], states[b], maze)

    return d
示例#4
0
def parsed(data):
    zs = ZGrid(data).z(val="#", first=False)
    xys = [(int(z.real), int(z.imag)) for z in zs]
    assert len(xys) == data.count("#")
    return xys
示例#5
0
"""
--- Day 11: Hex Ed ---
https://adventofcode.com/2017/day/11
"""
from collections import Counter

from aocd import data


def norm(data, steps=None):
    c = Counter(data.split(",")[:steps])
    c["ne"] -= c.pop("sw", 0)
    c["nw"] -= c.pop("se", 0)
    c["s"] -= c.pop("n", 0)
    d = sum(abs(x) for x in c.values()) - abs(sorted(c.values())[1])
    return d


print(norm(data))
print(max(norm(data, steps=i) for i in range(data.count(","))))
示例#6
0
文件: 8.py 项目: rpearl/advent
def b():
    s = 2 * len(lines) + data.count('"') + data.count("\\")
    return s
示例#7
0
"""
--- Day 6: Lanternfish ---
https://adventofcode.com/2021/day/6
"""
from aocd import data


def rotate(fish, n=1):
    for _ in range(n):
        fish[:] = fish[1:] + fish[:1]
        fish[6] += fish[-1]
    return sum(fish)


fish = [data.count(f) for f in "012345678"]
print("part a:", rotate(fish, n=80))
print("part b:", rotate(fish, n=256 - 80))
示例#8
0
from aocd import data
from collections import Counter

tests = {
    'ne,ne,ne': 3,
    'ne,ne,sw,sw': 0,
    'ne,ne,s,s': 2,
    'se,sw,se,sw,sw': 3,
}


def norm(data, steps=None):
    c = Counter(data.split(',')[:steps])
    c['ne'] -= c.pop('sw', 0)
    c['nw'] -= c.pop('se', 0)
    c['s'] -= c.pop('n', 0)
    d = sum(abs(x) for x in c.values()) - abs(sorted(c.values())[1])
    return d


for k, v in tests.items():
    assert norm(k) == v

print(norm(data))  # part a: 794
print(max(norm(data, steps=i) for i in range(data.count(','))))  # part b: 1524