Exemple #1
0
def parse_file(fn):
    rows = U.read_file(fn, 2017, do_strip=False)
    return {
        Coord(c_num, r_num): v
        for r_num, r in enumerate(rows) for c_num, v in enumerate(r)
        if v not in set(' \n')
    }
Exemple #2
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    fn = int(prob_num)
    if debug:
        fn = f'{prob_num}.test'

    return [Data.from_str(s) for s in read_file(fn, 2021)]
Exemple #3
0
def parse_data(*, debug=False):
    if debug:
        fn = '06.test'
    else:
        fn = 6
    data = read_file(fn, 2021)
    return mapt(int, first(data).split(','))
Exemple #4
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    def to_int_maybe(v):
        try:
            return int(v)
        except:
            return v

    funcs = dict(AND=lambda a, b: and_(a, b) & 0xFFFF,
                 OR=lambda a, b: or_(a, b) & 0xFFFF,
                 LSHIFT=lambda a, b: lshift(a, b) & 0xFFFF,
                 RSHIFT=lambda a, b: rshift(a, b) & 0xFFFF)

    def invert(v):
        return inv(v) & 0xFFFF

    def _get_circuit(l):
        left, right = l.split(' -> ')
        match left.split():
            case ('NOT', x):
                left = invert, to_int_maybe(x)
            case x, cmd, y:
                left = funcs[cmd], to_int_maybe(x), to_int_maybe(y)
            case (x, ):
                left = to_int_maybe(x)
            case _:
                raise Exception('wth')
        return left, to_int_maybe(right)

    return [_get_circuit(l) for l in read_file(filename, 2015)]
Exemple #5
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    return list(map(RD.from_s, read_file(filename, 2015)))
Exemple #6
0
def __main():
    strs = first(read_file(1, 2016)).split(',')
    instructions = [Inst.from_str(s.strip()) for s in strs]
    print(instructions)
    print(aoc01_a(instructions))
    print(aoc01_b(instructions))
    pass
Exemple #7
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    return [[int(v) for v in l] for l in read_file(filename, 2021)]
Exemple #8
0
def parse_map(fname=11, include_floor=False):
    res = {}
    for r, row in enumerate(read_file(fname, 2020)):
        for c, val in enumerate(row):
            if include_floor or val != '.':
                res[RC(r, c)] = val
    return res
Exemple #9
0
def init_state_and_state_map():
    f = iter(read_file(12, 2018))
    init_state = next(f).strip().split()[-1]
    next(f)
    lines = (l.strip().split() for l in f)
    state_map = {state: output for state, _, output in lines}
    return init_state, state_map
Exemple #10
0
 def from_file(cls, filename):
     res = {
         RC(row, col): t
         for row, l in enumerate(U.read_file(filename))
         for col, t in enumerate(l)
     }
     return cls.from_data(res)
Exemple #11
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    return [Ing.from_str(s) for s in read_file(filename, 2015)]
Exemple #12
0
def parse_data(*, debug=False, cls=None):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    return mapt(cls.from_str, read_file(filename, 2021))
Exemple #13
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    return read_file(filename, 2015)[0]
Exemple #14
0
def parse_data(*, debug=False, part=1):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    res = []
    to_rc = lambda s: eval(f'RC({s})')
    for l in read_file(filename, 2015):
        tokens = l.split()
        s, e = tokens[-3], tokens[-1]

        start_rc = to_rc(s)
        end_rc = to_rc(e)
        if part == 1:
            match tokens[1]:
                case 'on':
                    fn = _turn_on
                case 'off':
                    fn = _turn_off
                case _:
                    fn = _toggle
        else:
            match tokens[1]:
                case 'on':
                    fn = _inc
                case 'off':
                    fn = _dec
                case _:
                    fn = partial(_inc, amount=2)

        res.append(partial(fn, start_rc=start_rc, end_rc=end_rc))
    return res
Exemple #15
0
def parse_data(filename) -> Dict[StateVal, Inst]:
    lines = iter(U.read_file(filename, 2017))
    res = {}
    for l in lines:
        if l.startswith('In state'):
            cur_state = State[l[-2]]
            _add_to_dict(res, cur_state, lines)
    return res
Exemple #16
0
def parse_data(*, debug=False) -> tuple[Pair, ...]:
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    data = read_file(filename, 2021)
    return mapt(Pair.from_str, data)
Exemple #17
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    replace = {'(': 1, ')': -1}
    return [replace[c] for c in read_file(filename, 2015)[0]]
Exemple #18
0
def __main():
    insts = first(read_file(16, 2017)).split(',')
    for dc in Dance, DanceList, DanceStr:
        print(dc)
        with localtimer():
            print(part1(insts, dc))
            print(part2(insts, dc))
        print()
Exemple #19
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    res = [[int(c) for c in row] for row in read_file(filename, 2021)]
    return res
Exemple #20
0
def parse_file(name) -> Tuple[ParentChildrenMap, ChildParentMap]:
    """return {parent: {children}} map and {child: parent} map"""
    res_pc, res_cp = defaultdict(set), {}
    for l in U.read_file(name):
        parent, child = l.split(')')
        res_pc[parent].add(child)
        res_cp[child] = parent
    return res_pc, res_cp
Exemple #21
0
def __main():
    strs = U.read_file(24, 2017)
    comps = [Comp.from_str(s) for s in strs]
    comp_map = CompMap.from_comps(comps)

    with U.localtimer():
        print(calc_longest_bridge(comp_map, maxkey=itemgetter(1)))
        print(calc_longest_bridge(comp_map, maxkey=U.identity))
Exemple #22
0
def part2():
    opcodes = _determine_opcodes()
    i = Interpreter()
    regs = [0, 0, 0, 0]
    for inst in map(Inst.from_str, read_file('16.second', 2018)):
        regs = getattr(i, opcodes[inst.opcode])(inst, regs)

    return regs[0]
Exemple #23
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    parse = lambda l: tuple(sorted(map(int, l.split('x'))))
    return [parse(l) for l in read_file(filename, 2015)]
Exemple #24
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    data = read_file(filename, 2015)
    return 25 if debug else 150, sorted(map(int, data), reverse=True)
Exemple #25
0
def __main():
    comps = [Comp.from_str(s) for s in read_file(24, 2017)]
    # comps = [Comp(0, 2), Comp(2, 2), Comp(2, 2)]
    cm = CompMap()
    for c in comps:
        cm.add(c)
    with localtimer():
        print(calc_strongest_bridge(cm))
        print(calc_strongest_bridge(cm, lambda x: x))
Exemple #26
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    fn = int(prob_num)
    if debug:
        fn = f'{prob_num}.test'

    topo = [[9] + list(map(int, l.strip())) + [9] for l in read_file(fn, 2021)]
    num_cols = len(topo[0])
    return [[9] * num_cols, *topo, [9] * num_cols]
Exemple #27
0
def parse_data(fname=22):
    def _parse(l):
        if 'cut' in l:
            return cut, int(l.split()[-1])
        if 'increment' in l:
            return increment, int(l.split()[-1])
        return deal_into,

    return list(map(_parse, read_file(fname, 2019)))
Exemple #28
0
def part2_sara():
    answer = 0

    for line in U.read_file(2, 2017):
        arr = [int(z) for z in line.split("\t")]
        arr.sort(reverse=True)
        answer += sum(
            int(c[0] / c[1]) for c in combinations(arr, 2)
            if (c[0] % c[1]) == 0)
    print(answer)
Exemple #29
0
def _parse_data(fname='20.test'):
    res = defaultdict(list)
    cur_img = None
    for line in read_file(fname, 2020):
        if line.startswith('Tile'):
            cur_img = res[int(line.split()[-1][:-1])]
        elif line:
            cur_img.append(line)

    return list(starmap(Tile.from_data, res.items()))
Exemple #30
0
def parse_data(*, debug=False):
    prob_num = __file__[-5:-3]
    filename = int(prob_num)
    if debug:
        filename = f'{prob_num}.test'

    data = iter(read_file(filename, 2021))
    points = set()
    while l := next(data):
        points.add(eval(l))