Exemple #1
0
def part2():
    visited = set()
    for path in follow_steps(parse_input(io.read_file("day1/input.txt"))):
        steps = {(x, y) for [x, y] in path}
        for match in steps & visited:
            return sum(abs(x) for x in match)

        visited.update(steps)
Exemple #2
0
def parse_input(path):
    res, rows = [], [x.split() for x in io.read_file(path).splitlines()]
    for x in rows:
        res.append(
            (CommandType.Rect, int(x[1].split('x')[0]), int(x[1].split('x')[1])) if x[0] == 'rect'
            else (CommandType.RotateRow, int(x[2].split('=')[1]), int(x[4])) if x[1] == 'row'
            else (CommandType.RotateCol, int(x[2].split('=')[1]), int(x[4])) if x[1] == 'column'
            else 'ERROR')

    return res
Exemple #3
0
def part1():
    data = parse_input(io.read_file("day22/input.txt"))
    by_used = sorted(data, key=lambda t: used(t))
    by_free = sorted(data, key=lambda t: free(t), reverse=True)

    count = 0
    for x in [x for x in by_used if used(x) != 0]:
        for y in by_free:
            if used(x) > free(y): break
            if x[0] != y[0]:
                count += 1

    return count
Exemple #4
0
def parse_input(path):
    rows = [x.split() for x in io.read_file(path).splitlines(False)]
    return [
        (InstructionType.Init, int(x[5]),
         int(x[1])) if x[2] == 'goes'  # { bot, init-value }
        else (
            InstructionType.Give,
            int(x[1]),  # { bot, give-low, type-low, give-high, type-high }
            int(x[6]),
            DestType.Output if x[5] == 'output' else DestType.Bot,
            int(x[11]),
            DestType.Output if x[10] == 'output' else DestType.Bot)
        if x[2] == 'gives' else "ERROR" for x in rows
    ]
Exemple #5
0
def parse_input(path) -> Tuple[State, Mapping]:
    state: State = []
    mapping: List[str] = ["elevator"]
    positions = {}

    for (i, line) in enumerate(io.read_file(path).splitlines(False)):
        state.append([])
        if 'nothing relevant' not in line:
            for chip in re.findall(r"([a-z]*?)-compatible", line):
                if chip not in mapping: mapping.append(chip)
                positions[(mapping.index(chip), ObjType.Chip)] = i

            for gen in re.findall(r"([a-z]*?) generator", line):
                if gen not in mapping: mapping.append(gen)
                positions[(mapping.index(gen), ObjType.Generator)] = i

    state = [[False for _ in range(len(mapping) * 2 - 1)] for _ in range(len(state))]

    state[0][0] = True  # Elevator
    for e in positions.items():
        state[e[1]][(1 + (e[0][0] - 1) * 2) + (1 if e[0][1] == ObjType.Generator else 0)] = True

    return state, mapping
Exemple #6
0
def parse_input(path):
    return [extract_input(x) for x in io.read_file(path).splitlines(False)]
Exemple #7
0
def parse_input(path):
    return [Instruction(INST_ID[x[0]], [int(y) if is_numeric(y) else y for y in x[1:]]) for x in
            [line.split() for line in io.read_file(path).splitlines(False)]]
Exemple #8
0
def load_data(path):
    return [[try_numeric(x) for x in line.split(',')]
            for line in io.read_file(path).splitlines(False)[1:]]
Exemple #9
0
def part1():
    return len(decompress(io.read_file("day9/input.txt")).strip())
Exemple #10
0
def part1():
    return sum(abs(x) for x in final_pos(parse_input(io.read_file("day1/input.txt"))))
Exemple #11
0
def parse_input(path):
    return [
        list(map(int, x))
        for x in [line.split() for line in io.read_file(path).splitlines()]
    ]
Exemple #12
0
def part2():
    return recursive_decompress(io.read_file("day9/input.txt"))
Exemple #13
0
def part2():
    return valid_count(parse_input(io.read_file("day20/input.txt")),
                       4294967295)
Exemple #14
0
def part1():
    return safe_count(
        generate(parse_input(io.read_file("day18/input.txt")), 40))
Exemple #15
0
def parse_input(
    path
) -> List[Tuple[int, int, int]]:  # [{ ID, PositionCount, StartingPosition }]
    return [(int(x[1][1:]), int(x[3]), int(x[11][:-1])) for x in
            [line.split() for line in io.read_file(path).splitlines(False)]]
Exemple #16
0
 def test_least_freq(self):
     self.assertEqual(
         ''.join([
             Counter(x).most_common()[-1][0]
             for x in zip(*io.read_file("tests.txt").splitlines(False))
         ]), "advent")
Exemple #17
0
def part1():
    return sum(1 for x in io.read_file("day7/input.txt").splitlines(False) if is_tls(x))
Exemple #18
0
 def test_commands(self):
     self.assertEqual(
         'decab', process('abcde', parse_input(io.read_file("tests.txt"))))
Exemple #19
0
def part2():
    instr = parse_input(io.read_file("day21/input.txt"))
    for x in [''.join(x) for x in permutations('abcdefgh')]:
        if process(x, instr) == 'fbgdceah':
            return x
Exemple #20
0
def part1():
    return process('abcdefgh', parse_input(io.read_file("day21/input.txt")))
Exemple #21
0
def part1():
    return first_valid(parse_input(io.read_file("day20/input.txt")))
Exemple #22
0
def parse_input(path):
    return [[
        int(x) if is_location(x) else WALL if x == '#' else EMPTY
        for x in list(x)
    ] for x in io.read_file(path).splitlines(False)]
Exemple #23
0
def part2():
    return ''.join([
        Counter(x).most_common()[-1][0]
        for x in zip(*io.read_file("day6/input.txt").splitlines(False))
    ])