예제 #1
0
def pzl1(d):
    c = 0
    for x in d:
        if int(x[0]) <= x[3].count(x[2]) <= int(x[1]):
            c += 1
    return c


def pzl2(d):
    c = 0
    for x in d:
        p1 = int(x[0]) - 1
        p2 = int(x[1]) - 1
        ch = x[2]
        s = x[3]
        if (s[p1] == ch) ^ (s[p2] == ch):
            c += 1
    return c


ptn = r'(\d+)-(\d+) (\w+): (\w+)'
tst = read_re('02.tst', ptn)

assert pzl1(tst) == 2
assert pzl2(tst) == 1

dat = read_re('02.dat', ptn)

print("day 2 puzzle 1 =", pzl1(dat))
print("day 2 puzzle 2 =", pzl2(dat))
예제 #2
0
파일: 02.py 프로젝트: pahanini/aoc

def pzl1(d) -> int:
    pos = np.array([0, 0])
    for d, c in d:
        pos += np.array(dirs[d]) * np.array([int(c), int(c)])
    return np.prod(pos)


def pzl2(d) -> int:
    pos = np.array([0, 0])
    aim = 0
    for d, c in d:
        c = int(c)
        aim += dirs[d][1] * c
        f = dirs[d][0] * c
        pos += np.array([f, aim * f])
    return np.prod(pos)


tst = read_re('02.tst', r"(.+) (.)")

assert pzl1(tst) == 150
assert pzl2(tst) == 900


dat = read_re('02.dat', r"(.+) (.)")

print("day 2 puzzle 1 =", pzl1(dat))
print("day 2 puzzle 2 =", pzl2(dat))
예제 #3
0
                    x[i].discard(a)
        for i in ii:
            c[i] += 1
    return sum([(c[i]) for i, a in x.items() if len(a) == 0]), x


def pzl2(x):
    u = set()
    e = len([i for i, a in x.items() if len(a) == 0])
    while len(u) + e < len(x):
        for i, a in x.items():
            if len(a) == 1:
                u.update(a)
            if len(a) > 1:
                x[i] = x[i] - u
    x = {i: a.pop() for i, a in x.items() if a}
    return ','.join([i for i, a in sorted(x.items(), key=lambda v: v[1])])


tst = parse(read_re('21.tst', r'([\w ]+) \(contains ([\w, ]+)\)'))
res = pzl1(*tst)

assert res[0] == 5
assert pzl2(res[1]) == 'mxmxvkd,sqjhc,fvjkl'

dat = parse(read_re('21.dat', r'([\w ]+) \(contains ([\w, ]+)\)'))
res = pzl1(*dat)

print("day 21 puzzle 1 =", res[0])
print("day 21 puzzle 2 =", pzl2(res[1]))
예제 #4
0
파일: 08.py 프로젝트: pahanini/aoc
        elif instruction.operation == 'nop':
            self.pointer += 1
        elif instruction.operation == 'jmp':
            self.pointer += instruction.operand
        instruction.executed += 1

    def find_inf(self):
        for instruction in self.next():
            if instruction.executed == 1:
                return self.acc
            self.exec(instruction)

    def fix(self):
        while self.find_inf() is not None:
            self.mutate()
        return self.acc


ptn = r'^(nop|acc|jmp) ([\+\-0-9]+)$'
tst = read_re('08.tst', ptn)
cons = Console(tst)

assert cons.find_inf() == 5
assert cons.fix() == 8

dat = read_re('08.dat', ptn)
cons = Console(dat)

print("day 8 puzzle 1 =", cons.find_inf())
print("day 8 puzzle 2 =", cons.fix())
예제 #5
0
def read(f):
    return [tuple(map(int, x)) for x in read_re(f, r"(\d+),(\d+) -> (\d+),(\d+)")]
예제 #6
0
def read(f):
    t = read_re(f, r'x=(\d+)..(\d+), y=(-\d+)..(-\d+)')[0]
    return list(map(int, t))