def lvl_second():
    seats = records_getter("d11_seating.txt")
    seats = [list(row) for row in seats]
    seats = np.array(seats)
    seats_padded = np.pad(seats, (1, 1), constant_values='.')

    col_number = len(seats_padded[0])
    row_number = len(seats_padded)

    previous_seats = seats_padded.copy()
    stabilized = False
    while not stabilized:
        stabilized = True
        for row in range(1, row_number-1):
            for col in range(1, col_number-1):
                if previous_seats[row][col] == "L":
                    if sight_checker(previous_seats, (row, col)) == 0:
                        seats_padded[row][col] = "#"
                        stabilized = False
                elif previous_seats[row][col] == "#":
                    if sight_checker(previous_seats, (row, col)) >= 5:
                        seats_padded[row][col] = "L"
                        stabilized = False
        previous_seats = seats_padded.copy()

    unique, counts = np.unique(seats_padded, return_counts=True)
    dict_coup_count = dict(zip(unique, counts))
    print("Occupied seats when 5 in sight range:", dict_coup_count['#'])
Ejemplo n.º 2
0
def all_combinations():
    jolts_levels = records_getter("d10_adaptedarray.txt")

    sorted_jolts = [int(i) for i in jolts_levels]
    sorted_jolts.sort()
    # Adding my device
    sorted_jolts.append(sorted_jolts[-1]+3)
    previous_joltage = 0
    branches = 1
    ones_handler = 0
    for jolt in sorted_jolts:
        if jolt - previous_joltage == 1:
            ones_handler += 1
        elif jolt - previous_joltage == 3:
            if ones_handler == 0:
                branches *= 1
            elif ones_handler == 1:
                branches *= 1
            elif ones_handler == 2:
                branches *= 2
            elif ones_handler == 3:
                branches *= 4
            elif ones_handler == 4:
                branches *= 7
            else:
                branches *= 0
            ones_handler = 0
        else:
            print("Error")
        previous_joltage = jolt
    print("Ways to connect:", branches)
def lvl_first():
    seats = records_getter("d11_seating.txt")
    seats = [list(row) for row in seats]
    seats = np.array(seats)
    seats_padded = np.pad(seats, (1, 1), constant_values='.')

    col_number = len(seats_padded[0])
    row_number = len(seats_padded)
    unique, counts = np.unique(seats_padded, return_counts=True)
    dict_coup_count = dict(zip(unique, counts))
    # print("Empty space:", dict_coup_count['.'],
    #       ", seats:", dict_coup_count['L'])
    previous_seats = seats_padded.copy()
    stabilized = False

    while not stabilized:
        stabilized = True
        for row in range(1, row_number-1):
            for col in range(1, col_number-1):
                if previous_seats[row][col] == "L":
                    if kernel(previous_seats, (row, col)) == 0:
                        seats_padded[row][col] = "#"
                        stabilized = False
                elif previous_seats[row][col] == "#":
                    if kernel(previous_seats, (row, col)) >= 4:
                        seats_padded[row][col] = "L"
                        stabilized = False
        previous_seats = seats_padded.copy()

    unique, counts = np.unique(seats_padded, return_counts=True)
    dict_coup_count = dict(zip(unique, counts))
    print("Occupied when (4 empty nearby):", dict_coup_count['#'])
Ejemplo n.º 4
0
def second_lvl():
    orders = records_getter('d12_rain_risk.txt')
    face = 1
    # 1st - N/S (+/-)
    # 2st - E/W (+/-)
    position = (0, 0)
    # Waypoint relative to ship
    waypoint = (1, 10)

    for order in orders:
        command = order[0]
        value = int(order[1:])

        if command == "N":
            waypoint = (waypoint[0] + value, waypoint[1])
        elif command == "S":
            waypoint = (waypoint[0] - value, waypoint[1])
        elif command == "E":
            waypoint = (waypoint[0], waypoint[1] + value)
        elif command == "W":
            waypoint = (waypoint[0], waypoint[1] - value)
        elif command == "R":
            for step in range(0, int(value / 90)):
                waypoint = (-waypoint[1], waypoint[0])
        elif command == "L":
            for step in range(0, int(value / 90)):
                waypoint = (waypoint[1], -waypoint[0])
        elif command == "F":
            position = (position[0] + waypoint[0] * value,
                        position[1] + waypoint[1] * value)

    print("Final coordinate:", print_coordinate(*position))
    print("Manhatan distance:", abs(position[0]) + abs(position[1]))
def prg_runner(fix_step=-1):
    commands = records_getter("d8_gameboy.txt")
    line = 0
    acc = 0
    executed_line_iter = 0
    while not commands[line] == "ed":
        cmd = commands[line][:3]
        line_save = line
        if executed_line_iter == fix_step:
            if cmd == "nop":
                cmd = "jmp"
            elif cmd == "jmp":
                cmd = "nop"

        if cmd == "nop":
            line += 1
        elif cmd == "acc":
            if commands[line][4] == "+":
                m = 1
            else:
                m = -1
            acc += m * int(commands[line][5:])
            line += 1
        elif cmd == "jmp":
            if commands[line][4] == "+":
                m = 1
            else:
                m = -1
            line += m * int(commands[line][5:])
        commands[line_save] = "ed"
        if line == len(commands):
            return True, acc
        executed_line_iter += 1
    return False, acc
def creating_rules_list():
    rules = records_getter("d7_bags.txt")
    bags = []
    for rule in rules:
        r = rule.split(" bags contain ")
        r = dict(name=r[0],
                 containing=re.findall(r"(\d+) (([\w\s]+) bags?)", r[1]))
        bags.append(r)
    return bags
Ejemplo n.º 7
0
def newPass():
    passwords = records_getter('d2_passwords.txt')
    correct_passwords = 0
    for passw in passwords:
        l_lim, h_lim, letter, code = re.findall(
            "^(\d+)-(\d+)\s(\w):\s(\w+)$", passw)[0]
        if (code[int(l_lim)-1] == letter) ^ (code[int(h_lim)-1] == letter):
            correct_passwords += 1
    print("New password is:", correct_passwords)
def second_lvl():
    numbers = records_getter("d9_XMAS.txt")
    invalid_no = first_lvl()
    for window in range(2, 40):
        for idx in range(0, len(numbers)-window):
            handler = [int(i) for i in numbers[idx:idx+window]]
            suma = sum(handler)
            if suma == invalid_no:
                print("Biggest + smallest numbers: ",
                      max(handler)+min(handler))
def second_lvl():
    strings = records_getter("d1_elves.txt")
    digits = [int(i) for i in strings]

    for idx, digit in enumerate(digits):
        for idxx, d in enumerate(digits[idx+1:]):
            for dd in digits[idxx+2:]:
                if digit + d + dd == 2020:
                    print(digit * d * dd)
                    break
Ejemplo n.º 10
0
def oldPass():
    passwords = records_getter('d2_passwords.txt')
    correct_passwords = 0
    for passw in passwords:
        l_lim, h_lim, letter, code = re.findall(
            "^(\d+)-(\d+)\s(\w):\s(\w+)$", passw)[0]
        occurs = code.count(letter)
        if int(l_lim) <= occurs <= int(h_lim):
            correct_passwords += 1
    print("Old password is:", correct_passwords)
def fixing_loop():
    commands = records_getter("d8_gameboy.txt")
    save_commands = commands.copy()
    fixing_set = 0
    terminate = False
    acc = 0
    # Second star
    while not terminate:
        terminate, acc = prg_runner(fixing_set)
        # Reset and next command fixing
        commands = save_commands.copy()
        fixing_set += 1
    print("An accumultor-fixed value: ", acc)
Ejemplo n.º 12
0
def cmd_getter():
    commands = records_getter("d14_docking_data.txt")
    # Cmd is list of command tuples (mem adres/-1 if mask, value/mask)
    # Data recognition
    cmd = []
    for command in commands:
        if command[0:3] == 'mas':
            cmd.append((-1, command.split(" = ")[1]))
        else:
            # create a mask instruction
            cmd_temp = re.findall(r"^\w+\[(\d+)\]\D*(\d+)", command)
            cmd.append(cmd_temp[0])
    return cmd
def first_lvl():
    numbers = records_getter("d9_XMAS.txt")
    window = 25
    for idx, number in enumerate(numbers[window:]):
        checker = False
        for a in numbers[idx:idx+window-1]:
            for b in numbers[idx+1:idx+window]:
                if (int(a) + int(b) == int(number)):
                    checker = True
        if not checker:
            invalid_no = int(number)
            break
    return invalid_no
Ejemplo n.º 14
0
def boarding():
    seat_keys = records_getter('d5_airplane.txt')

    highest_seat = 0
    not_mine_seat = []

    for seat_key in seat_keys:
        start = 0
        end = 127
        row = 0

        # Row seeker
        for d in seat_key[:7]:
            if d == 'F':
                end = int((start + end) / 2)
            elif d == 'B':
                start = int((start + end + 1) / 2)
            else:
                break
        if start == end:
            row = start
        else:
            print("Err on row:", seat_key)

        # Col seeker
        start = 0
        end = 7
        col = 0
        for d in seat_key[7:]:
            if d == 'L':
                end = int((start + end) / 2)
            elif d == 'R':
                start = int((start + end + 1) / 2)
            else:
                break
        if start == end:
            col = start
        else:
            print("Err on col:", seat_key)

        not_mine_seat.append(8 * row + col)

        if highest_seat < 8 * row + col:
            highest_seat = 8 * row + col

        #print("Seat_key:", seat_key, ": Row:", row, ", Col:", col)
    return highest_seat, not_mine_seat
Ejemplo n.º 15
0
def any_one():
    lines = records_getter("d6_questionaire.txt")
    all_answers = ""
    total = 0
    for line in lines:
        if line == "":
            uniqe = []
            for a in all_answers:
                if a not in uniqe:
                    uniqe.append(a)
            answers_no = len(uniqe)
            total += answers_no
            # print(answers_no)
            all_answers = ""
        else:
            all_answers += line
    print("Anyone:", total)
Ejemplo n.º 16
0
def first_combination():
    jolts_levels = records_getter("d10_adaptedarray.txt")

    sorted_jolts = [int(i) for i in jolts_levels]
    sorted_jolts.sort()
    # Adding my device
    sorted_jolts.append(sorted_jolts[-1]+3)

    previous_joltage = 0
    adapters = [0, 0, 0]

    for jolt in sorted_jolts:
        adapters[jolt - previous_joltage - 1] += 1
        previous_joltage = jolt

    print("Voltage 1:", adapters[0], "| Voltage 2:",
          adapters[1], "| Voltage 3:", adapters[2])
    print("Mulitiplication:", adapters[0] * adapters[2])
Ejemplo n.º 17
0
def every_one():
    lines = records_getter("d6_questionaire.txt")
    team_member_answers = []
    total = 0
    for line in lines:
        if line == "":
            if len(team_member_answers) == 1:
                answers_no = len(team_member_answers[0])
            else:
                left_answers = team_member_answers[0]
                for tma in team_member_answers[1:]:
                    left_answers = [i for i in left_answers if i in tma]
                answers_no = len(left_answers)

            total += answers_no
            # print(answers_no)
            team_member_answers = []
        else:
            team_member_answers.append(line)

    print("Everyone:", total)
Ejemplo n.º 18
0
def first_lvl():
    orders = records_getter('d12_rain_risk.txt')
    face = 1
    # 1st - N/S (+/-)
    # 2st - E/W (+/-)
    position = (0, 0)

    for order in orders:
        command = order[0]
        value = int(order[1:])

        if command == "N":
            position = (position[0] + value, position[1])
        elif command == "S":
            position = (position[0] - value, position[1])
        elif command == "E":
            position = (position[0], position[1] + value)
        elif command == "W":
            position = (position[0], position[1] - value)
        elif command == "R":
            value /= 90
            face = (face + value) % 4
        elif command == "L":
            value /= 90
            face = (face - value + 4) % 4
        elif command == "F":
            if face == 0:
                position = (position[0] + value, position[1])
            elif face == 2:
                position = (position[0] - value, position[1])
            elif face == 1:
                position = (position[0], position[1] + value)
            elif face == 3:
                position = (position[0], position[1] - value)

    print("Final coordinate:", print_coordinate(*position))
    print("Manhatan distance:", abs(position[0]) + abs(position[1]))
def data_parser():
    data = records_getter("d16_input.txt")
    # Unpack rules
    rules = {}
    i = 0
    while data[i] != "":
        rule = re.findall(r'([\w\s]+):', data[i])
        values = re.findall(r'(\d+)\-(\d+)', data[i])
        rules[rule[0]] = [(int(tup[0]), int(tup[1])) for tup in values]
        i += 1
    # Unpack my ticket
    while data[i] != "your ticket:":
        i += 1
    i += 1
    my_ticket = [int(i) for i in data[i].split(',')]
    # Unpack nearby tickets
    while data[i] != "nearby tickets:":
        i += 1
    i += 1
    nearby_tickets = []
    for dat in data[i:]:
        nearby_tickets.append([int(i) for i in dat.split(',')])

    return rules, my_ticket, nearby_tickets
import re
import sys
from utils import records_getter, lvl_runner

lines = records_getter('d4_passports.txt')
fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
fields_optional = ['cid']


def passport_veryfier(data):
    passport_data = re.findall(r"(\w+)\:(\#?\w+)+", data)
    passport_data_dict = dict(passport_data)
    # general validation
    for field in fields:
        if passport_data_dict.get(field) == None:
            return False
    # specyfi validation
    v = passport_data_dict.get('byr')
    if int(v) < 1920 or 2002 < int(v):
        return False
    v = passport_data_dict.get('iyr')
    if int(v) < 2010 or 2020 < int(v):
        return False
    v = passport_data_dict.get('eyr')
    if int(v) < 2020 or 2030 < int(v):
        return False
    v = re.findall(r"(\d+)\s*(\w+)", passport_data_dict.get('hgt'))
    if v:
        v = v[0]
    else:
        return False
Ejemplo n.º 21
0
def data_aug():
    shuttle = records_getter("d13_shuttle.txt")

    arrival_time = int(shuttle[0])
    lines = [int(i) for i in shuttle[1].split(',') if i != "x"]
    return shuttle, arrival_time, lines
import sys
from utils import records_getter, lvl_runner

rows = records_getter("d3_forest.txt")

pattern_len = rows[0].__len__()


def slop_counter(right, down=1):
    f = 0
    encounter = 0

    for row in rows[::down]:
        if row[f % pattern_len] == '#':
            encounter += 1
        f += right
    return encounter


def first_slop():
    print(f"Tobbogan encounter {slop_counter(3)} trees")


def couple_slides():
    traverses = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]]

    mult = 1
    for travers in traverses:
        mult *= slop_counter(*travers)

    print("Multiplication of slides:", mult)