Esempio n. 1
0
                elif k == "iyr":
                    add_key = 2010 <= int(v) <= 2020
                elif k == "eyr":
                    add_key = 2020 <= int(v) <= 2030
                elif k == "hgt":
                    if len(v) > 2:
                        vint = int(v[:-2])
                        if v[-2:] == "cm":
                            add_key = 150 <= vint <= 193
                        elif v[-2:] == "in":
                            add_key = 59 <= vint <= 76
                elif k == "hcl":
                    p = re.compile('#[\\d|a-f]{6}')
                    add_key = p.match(v) is not None
                elif k == "ecl":
                    add_key = v in [
                        "amb", "blu", "brn", "gry", "grn", "hzl", "oth"
                    ]
                elif k == "pid":
                    p = re.compile('\\d{9}')
                    add_key = p.match(v) is not None

                seen[add_key].add(k)

    print("part1:", valid1)
    print("part2:", valid2)


passportlist = read_as_strings("../inputs/2020_04.txt")
part1and2(passportlist)
Esempio n. 2
0
from util.inputReader import read_as_strings


def part1(slope_grid):
    trees = 0
    slope = 1

    for i, line in enumerate(slope_grid):
        if i % 2 == 0 and line[(int(i / 2) * slope) % len(line)] == '#':
            trees += 1
    return trees


grid = read_as_strings("../inputs/2020_03.txt")
print("part1:", part1(grid))
print("part2:", 278 * 90 * 88 * 98 * 45)
Esempio n. 3
0

def part1and2(inputs):
    seats_open = [True for i in range(127 * 8)]
    max_id = 0
    for nav in inputs:
        row = _find_id(127, 0, 0, 7, nav, 'F')
        col = _find_id(7, 0, 7, 10, nav, 'L')
        this_id = row * 8 + col
        max_id = max(max_id, this_id)
        seats_open[this_id] = False
    print("part1: ", max_id)

    first_idx = seats_open.index(False)
    my_seat = seats_open.index(True, first_idx)
    print("part2: ", my_seat)


def _find_id(hi, lo, a, b, nav, ch):
    for i in range(a, b):
        if nav[i] == ch:
            hi = (hi + lo) / 2
        else:
            lo = (hi + lo) / 2
    return int(hi)


my_input = read_as_strings("../inputs/2020_05.txt")
part1and2(my_input)

Esempio n. 4
0
                        lines[ri + 1][ci] = lines[ri + 1][ci] if lines[
                            ri + 1][ci] == 0 else lines[ri + 1][ci] + 1
                        lines[ri - 1][ci - 1] = lines[ri - 1][ci - 1] \
                            if lines[ri - 1][ci - 1] == 0 else lines[ri - 1][ci - 1] + 1
                        lines[ri + 1][ci - 1] = lines[ri + 1][ci - 1] \
                            if lines[ri + 1][ci - 1] == 0 else lines[ri + 1][ci - 1] + 1
                        lines[ri - 1][ci + 1] = lines[ri - 1][ci + 1] \
                            if lines[ri - 1][ci + 1] == 0 else lines[ri - 1][ci + 1] + 1
                        lines[ri + 1][ci + 1] = lines[ri + 1][ci + 1] \
                            if lines[ri + 1][ci + 1] == 0 else lines[ri + 1][ci + 1] + 1

        all_zeroes = True
        for ri in range(1, 11):
            if lines[ri].count(0) != 12:
                all_zeroes = False
        step += 1
    return step


lines = list()
lines.append([0] * 12)
input = read_as_strings("../inputs/2021_11.txt")

for l in input:
    lines.append([0] + list(map(lambda x: int(x), l)) + [0])

lines.append([0] * 12)

print("part1:", part1())  # 1263 too low
print("part2:", part2())
Esempio n. 5
0
            val[1] = '0'
            val = ['0'] * (36 - len(val)) + val

            q = deque()
            q.append(mask)

            while len(q) > 0:
                contains_x = False
                val_i = q.pop()
                for i, c in enumerate(val):
                    if mask[i] == 'X':
                        contains_x = True
                        val_i[i] = '0'
                        q.append(val_i.deepcopy())
                        val_i[i] = '1'
                        q.append(val_i.deepcopy())

                if not contains_x:

                    memory[int(addr)] = int("".join(val), 2)
        else:
            mask = list(parse("mask = {}", comm).fixed[0])

    return sum(memory.values())


# too high 16363683105667
commands = read_as_strings("../inputs/2020_14.txt")
# print("part1:", part1(commands))
print("part2:", part2(commands))
Esempio n. 6
0
            if number.startswith(ogr_str):
                ogr_count_0 += 1 if number[i] == '0' else 0
                ogr_count_1 += 1 if number[i] == '1' else 0
            if number.startswith(co2_str):
                co2_count_0 += 1 if number[i] == '0' else 0
                co2_count_1 += 1 if number[i] == '1' else 0

        if ogr_count_1 + ogr_count_0 == 1:  # one number left
            ogr_str = next(filter(lambda x: x.startswith(ogr_str), numbers))
            ogr_bin = int(ogr_str, 2)
        if co2_count_1 + co2_count_0 == 1:  # one number left
            co2_str = next(filter(lambda x: x.startswith(co2_str), numbers))
            co2_bin = int(co2_str, 2)

        ogr_str += '1' if ogr_count_1 >= ogr_count_0 else '0'
        co2_str += '0' if co2_count_1 >= co2_count_0 else '1'

    if ogr_bin == 0:
        ogr_bin = int(ogr_str, 2)
    if co2_bin == 0:
        co2_bin = int(co2_str, 2)

    return ogr_bin * co2_bin


lines = read_as_strings("../inputs/2021_03.txt")
print("part1:", part1(lines))
print("part2:", part2(lines))

# part2 12723489 too high
Esempio n. 7
0
                if r < total_rows - 1:
                    seats[inputs[r + 1][c]] += 1
                    if c > 0:
                        seats[inputs[r + 1][c - 1]] += 1
                    if c < total_cols - 1:
                        seats[inputs[r + 1][c + 1]] += 1
                if seats['#'] == 0 and inputs[r][c] == 'L':
                    changed = inputs[r][c] != '#'
                    next_map[r][c] = '#'
                elif seats['#'] >= 4 and inputs[r][c] == '#':
                    changed = inputs[r][c] != 'L'
                    next_map[r][c] = 'L'

        inputs = next_map

    count = 0
    for i in inputs:
        count += i.count('#')
    return count


def _print_seat_map(inputs):
    for i in inputs:
        print(" ".join(i))
    print()


seat_map = read_as_strings("../inputs/2020_11.txt")
print("part1:", part1(seat_map))
# print("part2:", part2(inputs))
Esempio n. 8
0
    y_dist = 0

    facing = 1  # east
    for instructions in list_of_strings:
        (direction, distance) = p.findall(instructions)[0]
        if direction == 'R':
            facing = int((facing + int(distance) / 90) % 4)
        elif direction == 'L':
            facing = int((facing + 4 - int(distance) / 90) % 4)
        else:
            move_direction = facing
            if direction in rotations:
                move_direction = rotations.index(direction)

            move = cardinals[rotations[move_direction]]
            x_dist += move[0] * int(distance)
            y_dist += move[1] * int(distance)

    return abs(x_dist) + abs(y_dist)


p = re.compile('(\\w)(\\d+)')
cardinals = {'N': (0, 1), 'S': (0, -1), 'E': (1, 0), 'W': (-1, 0)}
rotations = ['N', 'E', 'S', 'W']

lines = read_as_strings("../inputs/2020_12.txt")
print("part1:", part1(lines))
# print("part2:", part2(lines))

# 689 too low
Esempio n. 9
0
        elif inst == "jmp":
            if inst_replace == "jmp":
                ct += 1
                if inst_count != ct:
                    i += n - 1
            else:
                i += n - 1
        else:
            if inst_replace == "nop":
                ct += 1
                if inst_count == ct:
                    i += n - 1
        i += 1
    return acc


def part2(my_input):
    for i in range("".join(my_input).count("nop")):
        res = part1(my_input, "nop", i)
        if res != -1:
            return res

    for i in range("".join(my_input).count("jmp")):
        res = part1(my_input, "jmp", i)
        if res != -1:
            return res


inputs = read_as_strings("../inputs/2020_08.txt")
print(part2(inputs))