Exemplo n.º 1
0
def run_day_16():
    #
    #-----------------------------------------------------------------------------
    programs = [
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
        "o", "p"
    ]
    data = read_file("Day16_Data.txt")
    instructions = []
    for line in data:
        instructions.extend(line.split(","))
    last = 1
    seen = []
    for count in range(0, 1000000000 % 24):
        for item in instructions:
            # Spin moves the last n instructions to the front
            if item[0] == "s":
                chunk_size = item[1:]
                for i in range(0, int(chunk_size)):
                    programs.insert(0, programs.pop(-1))
            elif item[0] == "x":
                parts = item[1:].split("/")
                first = int(parts[0])
                second = int(parts[1])
                programs[first], programs[second] = programs[second], programs[
                    first]
            elif item[0] == "p":
                first = item[1]
                second = item[3]
                a, b = programs.index(first), programs.index(second)
                programs[a], programs[b] = programs[b], programs[a]
    return programs
Exemplo n.º 2
0
def run_day_13():
    #
    #-----------------------------------------------------------------------------
    data = read_file("Day13_Data.txt")

    # find the deepest layer that we need
    deepest = -1
    bits = data[-1].split(": ")
    deepest = int(bits[0])
    # set up our vector of depth / location pairs
    scanners = []
    for i in range(0, deepest + 1):
        scanners.append(scanner())

    for line in data:
        bits = line.split(": ")
        # Get the information out of the file line
        level = int(bits[0].strip())
        depth = int(bits[1].strip())

        # Set the depth and initial position for the scanner
        scanners[level].m_depth = depth
        scanners[level].m_position = 0
    delay = 0
    while (True):
        if (pass_scanners(scanners, delay)):
            return delay
        delay += 1
Exemplo n.º 3
0
def run_day_2():
    #
    #-----------------------------------------------------------------------------
    file = read_file("Day2_Data.txt")
    grid = []
    for line in file:
        str_numbers = line.split("\t")
        row = [(int(x)) for x in str_numbers]
        grid.append(row)
    return part_two(grid)
Exemplo n.º 4
0
def run_day_12():
#
#-----------------------------------------------------------------------------
    data = read_file("Day12_Data.txt")
    reachable = set()
    num_groups = 0
    for i in range(0, len(data)):
        if not i in reachable:
            num_groups += 1
            current_group = set()
            add_node(i, current_group, data)
            reachable = reachable | current_group
    return num_groups
Exemplo n.º 5
0
def run_day_4():
    #
    #-----------------------------------------------------------------------------
    lines = read_file("Day4_Data.txt")
    valid = 0
    for line in lines:
        words = line.split(" ")
        seen = set()
        for word in words:
            foo = "".join(sorted(word))
            if foo not in seen:
                seen.add(foo)
        if (len(words) == len(seen)):
            valid += 1
    return valid
Exemplo n.º 6
0
def run_day_1():
    #
    #-----------------------------------------------------------------------------

    numbers = []
    lines = read_file("Day1_Data.txt")
    for line in lines:
        for char in line:
            numbers.append(int(char))
    sum = 0
    for i in range(0, len(numbers)):
        current = numbers[i]
        count = (int)(i + (len(numbers) / 2)) % len(numbers)
        next = numbers[count]
        if current == next:
            sum += current
    return sum
Exemplo n.º 7
0
def run_day_8():
    #
    #-----------------------------------------------------------------------------
    file = read_file("Day8_Data.txt")
    instructions = []
    registries = []
    for line in file:
        instructions.append(line.split(" "))

    running_max = -1000
    for feed in instructions:
        for value in registries:
            if (value[1] > running_max):
                running_max = value[1]
        change_reg = reg_for_letter(feed[0], registries)
        plus_minus = 1
        if (feed[1] == "dec"):
            plus_minus = -1
        change_diff = int(feed[2]) * plus_minus
        check_reg = reg_for_letter(feed[4], registries)
        check_condition = feed[5]
        check_num = int(feed[6])
        edit_reg = True
        if (check_condition == "=="):
            edit_reg = (registries[check_reg][1] == check_num)
        elif (check_condition == "!="):
            edit_reg = (registries[check_reg][1] != check_num)
        elif (check_condition == "<"):
            edit_reg = (registries[check_reg][1] < check_num)
        elif (check_condition == "<="):
            edit_reg = (registries[check_reg][1] <= check_num)
        elif (check_condition == ">"):
            edit_reg = (registries[check_reg][1] > check_num)
        elif (check_condition == ">="):
            edit_reg = (registries[check_reg][1] >= check_num)
        else:
            print("Unexpeced condition " + check_condition)
            exit()
        if (edit_reg):
            registries[change_reg][1] += change_diff
    print(registries)
    max_current = -1000
    for value in registries:
        if (value[1] > max_current):
            max_current = value[1]
    return running_max
Exemplo n.º 8
0
def run_day_7():
    #
    #-----------------------------------------------------------------------------
    lines = read_file("Day7_Data.txt")

    list_of_unlinked_nodes = []
    for line in lines:
        list_of_unlinked_nodes.append(line_to_node(line))

    # Now link the list
    for parent_node in list_of_unlinked_nodes:
        for child_name in parent_node.m_child_names:
            for child_node in list_of_unlinked_nodes:
                if child_name == child_node.m_name:
                    parent_node.append_child_node(child_node)
                    child_node.add_parent(parent_node)

    # We can find the bottom node from any point by going back through parents
    start = list_of_unlinked_nodes[0]
    while (start.m_parent != None):
        start = start.m_parent
    start.calculate_tower_weight()

    # Now find the node that needs to change
    current = start
    while (True):
        results = current.find_problem_child()
        if (results[1]):
            print("The node that needs to change is " + results[0].m_name)
            # Problems weigth is
            problem_total_weigth = current.m_tower_weight
            problem_weight = current.m_weight
            #Sibling total weight
            weights = [child.m_tower_weight for child in current.m_children]
            counting = Counter(weights)
            # Expected weight of the stacks
            common = max(counting, key=counting.get)
            for child in results[0].m_children:
                common -= child.m_tower_weight
            print("It should weight " + str(common))
            return common
        else:
            current = results[0]
    return 0
Exemplo n.º 9
0
def run_day_5():
    #
    #-----------------------------------------------------------------------------
    file = read_file("Day5_Data.txt")
    jump_list = []
    for num in file:
        jump_list.append(int(num))
    current = 0
    steps = 0
    last = 0
    while (True):
        last = current
        current += jump_list[current]
        steps += 1
        if (current > len(jump_list) - 1) or current < 0:
            return steps
        if (jump_list[last] >= 3):
            jump_list[last] -= 1
        else:
            jump_list[last] += 1
Exemplo n.º 10
0
from FileReader import read_file
import matplotlib.pyplot as plt

x, y = read_file("../results/ID3")
x2, y2 = read_file("../results/RandomForest")
plt.plot(x, y, label = "ID3")
plt.plot(x2, y2, label = "RandomForest")
plt.ylabel('Precisión')
plt.xlabel('Porcentaje de testeo')
plt.legend()
plt.show()

x, y = read_file("../results/ID3WithDepthLimit")
x2, y2 = read_file("../results/ID3WithDepthLimitTraining")
plt.plot(x, y, label="Test")
plt.plot(x2, y2, label="Conjunto de entrenamiento")
plt.title("ID3 con límite de profundidad")
plt.ylabel('Precisión')
plt.xlabel('Límite de profundidad')
plt.legend()
plt.show()

x, y = read_file("../results/ID3WithLimit")
x2, y2 = read_file("../results/ID3WithLimitTraining")
plt.plot(x, y)
plt.plot(x, y, label="Test")
plt.plot(x2, y2, label="Conjunto de entrenamiento")
plt.title("ID3 con límite de observaciones")
plt.ylabel('Precisión')
plt.xlabel('Mínimo de observaciones')
plt.legend()