コード例 #1
0
def eval_choice(row) -> float:
    """ Calculates binary metric for 1-of-n type of questions.
    The metric equals to the indicator function of answer correctness.

    Args:
        row (dict or pd.Series): row with predictions and correct answers.

    Return:
        value of binary metric (either 0 or 1) multiplied by the point score of the question.
    """
    if not pd.isnull(row["gt_unique"]):
        correct_answers = [read_str(row["gt_unique"])]
    elif not pd.isnull(row["gt_variants"]):
        correct_answers = read_str(row["gt_variants"])
    else:
        return np.NaN

    my_answers = read_str(row["prediction"])
    if not isinstance(my_answers, list):
        return np.NaN
    if my_answers in correct_answers:
        return float(row["score"])
    else:
        return 0.
コード例 #2
0
def eval_text_word(row) -> float:
    """ Calculates binary metric for plain text word type of questions.
    The metric equals to the indicator function of answer correctness
    (whether the predicted string fully matches the target).

    Args:
        row (dict or pd.Series): row with predictions and correct answers.

    Return:
        value of binary metric (either 0 or 1) multiplied by the point score of the question.
    """
    if not pd.isnull(row["gt_unique"]):
        correct_answers = [row["gt_unique"]]
    elif not pd.isnull(row["gt_variants"]):
        correct_answers = read_str(row["gt_variants"])
    else:
        return np.NaN

    if str(row["prediction"]) in correct_answers:
        return float(row["score"])
    else:
        return 0.
コード例 #3
0
    while True:
        x += dx
        y += dy
        if y > h - 1:
            return c
        if x > w - 1:
            x -= w
        if d[y][x] == '#':
            c += 1


def pzl2(d, d_list):
    res = 1
    for dx, dy in d_list:
        res *= pzl1(d, dx, dy)
    return res


tst = read_str('03.tst')

deltas = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]

assert pzl1(tst, deltas[1][0], deltas[1][1]) == 7
assert pzl2(tst, deltas) == 336


dat = read_str('03.dat')

print("day 3 puzzle 1 =", pzl1(dat, deltas[1][0], deltas[1][1]))
print("day 3 puzzle 2 =", pzl2(dat, deltas))
コード例 #4
0
def pzl1(d):
    t = {}
    for c, v in d:
        if c == 'mask':
            m = v
        else:
            t[c] = mask1(m, v)
    return sum(t.values())


def pzl2(d):
    t = {}
    for c, v in d:
        if c == 'mask':
            m = v
        else:
            for a in mask2(m, c):
                t[a] = v
    return sum(t.values())


tst = parse(read_str('14.1.tst'))
assert pzl1(tst) == 165
tst = parse(read_str('14.2.tst'))
assert pzl2(tst) == 208

dat = parse(read_str('14.dat'))
print("day 14 puzzle 1 =", pzl1(dat))
print("day 14 puzzle 2 =", pzl2(dat))
コード例 #5
0
def read(f):
    return [int(x) for x in read_str(f)[0].split(',')]
コード例 #6
0
    score1 = (3, 57, 1197, 25137)
    for ch in chs:
        st = []
        for c in ch:
            if c in opn:
                st.append(c)
                continue
            p = st.pop()
            i = cls.index(c)
            if opn.index(p) != i:
                p1 += score1[i]
                break
        else:
            st.reverse()
            t = 0
            for p in st:
                t = t * 5 + opn.index(p) + 1
            p2.append(t)
    p2.sort()
    return p1, p2[len(p2) // 2]


r = read_str('10.tst')
assert pzl(r) == (26397, 288957)

r = read_str('10.dat')
d1, d2 = pzl(r)

print("day 10 puzzle 1 =", d1)
print("day 10 puzzle 2 =", d2)
コード例 #7
0
                n[p] = '#'
                t += 1
            elif v == '#' and c >= limit:
                n[p] = 'L'
                t += 1
        if t == 0:
            return len([s for _, s in n.items() if s == '#'])


def parse(d):
    r = defaultdict(str)
    for y, s in enumerate(d):
        for x, c in enumerate(s):
            r[(x, y)] = c
    return r


tst = parse(read_str('11.1.tst'))
assert cnt(tst, (3, 4), False) == 2
assert cnt(tst, (3, 4), True) == 8

tst = parse(read_str('11.tst'))

assert pzl(tst.copy(), 4, False) == 37
assert pzl(tst.copy(), 5, True) == 26

dat = parse(read_str('11.dat'))

print("day 11 puzzle 1 =", pzl(dat.copy(), 4, False))
print("day 11 puzzle 2 =", pzl(dat.copy(), 5, True))
コード例 #8
0
def read(f):
    r = defaultdict(list)
    for a, b in [ln.split('-') for ln in read_str(f)]:
        r[a].append(b)
        r[b].append(a)
    return r
コード例 #9
0
ファイル: 17.py プロジェクト: pahanini/aoc
def parse(d):
    st = set()
    for y, s in enumerate(d):
        for x, v in enumerate(s):
            if v == '#':
                st.add((x, y))
    return st


def pzl1(st, dim):
    st = set([v + (dim - 2) * (0, ) for v in st])
    dd = [x for x in product((-1, 0, 1), repeat=dim) if x != dim * (0, )]
    for _ in range(6):
        cnt = Counter([tuple(map(sum, zip(c, d))) for c in st for d in dd])
        st = [
            k for k, v in cnt.items()
            if (k not in st and v == 3) or (k in st and v in (2, 3))
        ]
    return len(st)


tst = parse(read_str('17.tst'))

assert pzl1(tst, 3) == 112
assert pzl1(tst, 4) == 848

dat = parse(read_str('17.dat'))

print("day 17 puzzle 1 =", pzl1(dat, 3))
print("day 17 puzzle 2 =", pzl1(dat, 4))
コード例 #10
0
def read(f):
    return [list(map(int, list(x))) for x in read_str(f)]
コード例 #11
0
def read(f):
    ln = read_str(f)
    return np.array([list(map(int, list(v))) for v in ln])
コード例 #12
0
class Op(int):
    def __mul__(self, b):
        return Op(int(self) + b)

    def __add__(self, b):
        return Op(int(self) + b)

    def __sub__(self, b):
        return Op(int(self) * b)


def pzl1(expr):
    expr = re.sub(r"(\d+)", r"Op(\1)", expr)
    expr = expr.replace("*", "-")
    return eval(expr, {}, dict(Op=Op))


def pzl2(expr):
    expr = re.sub(r"(\d+)", r"Op(\1)", expr)
    expr = expr.replace("*", "-")
    expr = expr.replace("+", "*")
    return eval(expr, {}, dict(Op=Op))


assert pzl1('((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2') == 13632
assert pzl2('((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2') == 23340

dat = read_str('18.dat')

print("day 18 puzzle 1 =", sum([pzl1(s) for s in dat]))
print("day 18 puzzle 1 =", sum([pzl2(s) for s in dat]))
コード例 #13
0
        else:
            p += d * v
    return abs(p.real) + abs(p.imag)


def pzl2(x):
    p = 0
    w = 10 + 1j
    for c, v in x:
        if c in dirs:
            w += dirs[c] * v
        elif c in rot:
            w *= rot[c]**(v / 90)
        else:
            p += w * v
    return abs(p.real) + abs(p.imag)


def parse(d):
    return [(s[0], int(s[1:])) for s in d]


tst = parse(read_str('12.tst'))
assert pzl1(tst) == 25
assert pzl2(tst) == 286

dat = parse(read_str('12.dat'))

print("day 12 puzzle 1 =", pzl1(dat))
print("day 12 puzzle 2 =", pzl2(dat))
コード例 #14
0
ファイル: 03.py プロジェクト: pahanini/aoc
def read(f):
    return np.array([[int(c) for c in row] for row in read_str(f)])
コード例 #15
0
    c = s
    return r, c


def seat_id(p):
    r, c = search(p)
    return r * 8 + c


assert seat_id('BFFFBBFRRR') == 567


def pzl1(d):
    return max([seat_id(p) for p in d])


assert pzl1(['BFFFBBFRRR', 'FFFBBBFRRR', 'BBFFBBFRLL']) == 820


def pzl2(d):
    t = [seat_id(p) for p in d]
    t.sort()
    for i, v in enumerate(t):
        if v + 1 != t[i + 1]:
            return v + 1


dat = read_str('05.dat')
print("day 5 puzzle 1 =", pzl1(dat))
print("day 5 puzzle 2 =", pzl2(dat))
コード例 #16
0
ファイル: 08.py プロジェクト: pahanini/aoc
def read(f):
    return [[list(filter(len, y.split(' '))) for y in x.split('|')]
            for x in read_str(f)]
コード例 #17
0
assert crt([(2, 3), (3, 5), (2, 7)]) == 23


def pzl1(d):
    t, bb = d
    mt, mb = 0, 0
    for _, b in bb:
        bt = b - t % b
        if mt > bt or mt == 0:
            mt, mb = bt, b
    return mt * mb


def pzl2(d):
    v = []
    for i, n in d[1]:
        v.append((i, n))
    return crt(v)


tst = parse(read_str('13.tst'))

assert pzl1(tst) == 295
assert pzl2(tst) == 1068781

dat = parse(read_str('13.dat'))

print("day 13 puzzle 1 =", pzl1(dat))
print("day 13 puzzle 2 =", pzl2(dat))