Example #1
0
def executor():
    console.header('day 2, part 2')

    timer = Timer()
    used_ids = set([])
    pairs = []
    ids = DataProvider.load('day2')

    timer.start()
    for id in ids:
        if id not in used_ids:
            used_ids.add(id)
            for inner in ids:
                if inner in used_ids:
                    continue

                distance = hamming_distance(id, inner)

                if distance == 1:
                    pairs.append((
                        id,
                        inner,
                    ))
    timer.end()

    console.log(' ',
                'Pairs',
                fg='cyan',
                bold=True,
                color_status=True,
                include_brackets=True)
    print(pairs)

    timer.output()
Example #2
0
def executor():
    console.header('day 3, part 1')

    max_x = 1000
    max_y = 1000
    elements = DataProvider.load('day3')
    claim_ids = []
    timer = Timer()

    timer.start()
    claims = numpy.zeros([max_x, max_y])
    for element in elements:
        claim = Claim(element)
        claims = claim.fill_claim(claims)

    for element in elements:
        claim = Claim(element)
        if claim.claim_is_alone(claims):
            claim_ids.append(claim.claim_id)
    timer.end()

    console.log(' ',
                'Claim Ids',
                fg='cyan',
                bold=True,
                color_status=True,
                include_brackets=True)
    print(claim_ids)
    timer.output()
Example #3
0
def executor():
    console.header('day 2, part 1')

    timer = Timer()
    pair_count = 0
    triplet_count = 0

    timer.start()
    for id in DataProvider.load('day2'):
        if has(2, id):
            pair_count += 1
        if has(3, id):
            triplet_count += 1
    timer.end()

    console.log('Pairs',
                str(pair_count),
                fg='cyan',
                bold=True,
                color_status=True,
                include_brackets=True)
    console.log('Triplets',
                str(triplet_count),
                fg='cyan',
                bold=True,
                color_status=True,
                include_brackets=True)
    console.log('Checksum',
                str(pair_count * triplet_count),
                fg='cyan',
                bold=True,
                color_status=True,
                include_brackets=True)

    timer.output()
Example #4
0
def executor():
    console.header('day 1 - part 2')

    master_freq = 0
    master_freq_set = set([master_freq])
    master_freq_list = [master_freq]

    count = 0

    timer = Timer()

    timer.start()
    try:
        while True:
            count += 1
            for freq in DataProvider.load('day1'):
                master_freq = master_freq + int(freq)
                master_freq_set.add(master_freq)
                master_freq_list.append(master_freq)

                if len(master_freq_set) != len(master_freq_list):
                    raise Exception('Found it!')
            if count % 10 == 0:
                console.log(' ', 'Looped {} times so far.'.format(count))
    except:
        pass
    timer.end()

    console.log('Dupe',
                master_freq,
                fg='cyan',
                bold=True,
                color_status=True,
                include_brackets=True)
    timer.output()
Example #5
0
def executor():
    console.header('day 5, part 1')

    timer = Timer()
    polymer = DataProvider.read('day5').strip()

    timer.start()
    polymer = Polymer.react(polymer)
    timer.end()

    console.log('polymer', polymer)
    console.log('length', str(len(polymer)))
    timer.output()
Example #6
0
def executor():
    console.header('day 5, part 2')

    timer = Timer()
    polymer = DataProvider.read('day5').strip()

    timer.start()
    polymer = Polymer.get_best_score(polymer)
    timer.end()

    console.log('Best', polymer)
    console.log('Length', str(len(polymer)))
    timer.output()
Example #7
0
def executor():
    console.header('day 11, part 1')

    timer = Timer()
    serial_number = 3214

    GRID_X = 300
    GRID_Y = 300

    timer.start()
    console.log('Task', 'Building empty power grid')
    grid = [[0] * GRID_Y for i in range(GRID_X)]

    console.log('Task', 'Calculating power levels')
    for x in range(GRID_X):
        for y in range(GRID_Y):
            grid[x][y] = power_level(x, y, serial_number)

    max_x = GRID_X - 3
    max_y = GRID_Y - 3

    max_power = -999
    target_x = -1
    target_y = -1

    console.log('Task', 'Finding 3x3 grid with the highest power level')
    for x in range(max_x):
        for y in range(max_y):
            cell = grid[x:x + 3]
            cell = [inner[y:y + 3] for inner in cell]
            power = 0
            for row in cell:
                power += sum(row)

            if power > max_power:
                max_power = power
                target_x = x
                target_y = y
    timer.end()

    console.header('output')
    console.log('Power', max_power)
    console.log('X', target_x)
    console.log('Y', target_y)
    timer.output()
Example #8
0
def executor():
    console.header('day 8, part 1')

    console.log('Task', 'Configure application...')
    timer = Timer()
    data = DataProvider.read('day8').strip()
    #data = '2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2'
    elements = [int(el) for el in data.split(' ')]

    timer.start()
    console.header('executing')
    score, rest = parse(elements)
    timer.end()

    console.header('output')
    console.log('Score', score)
    console.log('Rest', rest)
    timer.output()
Example #9
0
def executor():
    console.header('day 4, part 2')

    timer = Timer()
    days = DataProvider.load('day4')

    fsm = FiniteStateMachine(StartState)

    timer.start()
    days.sort()
    for day in days:
        fsm.execute(day.strip())

    fsm.to(End2State)
    fsm.execute(None)
    timer.end()

    console.log('Minute', '', fg='cyan', bold=True, color_status=True, include_brackets=True)
    timer.output()
Example #10
0
def executor():
    console.header('day 3, part 1')

    max_x = 1000
    max_y = 1000
    count = 0
    timer = Timer()

    timer.start()
    claims = numpy.zeros([max_x, max_y])
    for element in DataProvider.load('day3'):
        claim = Claim(element)
        claims = claim.fill_claim(claims)

    for x in range(max_x):
        for y in range(max_y):
            if claims[x][y] > 1:
                count += 1
    timer.end()

    console.log('Count', str(count), fg='cyan', bold=True, color_status=True, include_brackets=True)
    timer.output()
Example #11
0
def executor(ctx):
    console.header('day 10, both parts')

    timer = Timer()
    data = DataProvider.load('day10')

    # data = [
    #     'position=< 9,  1> velocity=< 0,  2>',
    #     'position=< 7,  0> velocity=<-1,  0>',
    #     'position=< 3, -2> velocity=<-1,  1>',
    #     'position=< 6, 10> velocity=<-2, -1>',
    #     'position=< 2, -4> velocity=< 2,  2>',
    #     'position=<-6, 10> velocity=< 2, -2>',
    #     'position=< 1,  8> velocity=< 1, -1>',
    #     'position=< 1,  7> velocity=< 1,  0>',
    #     'position=<-3, 11> velocity=< 1, -2>',
    #     'position=< 7,  6> velocity=<-1, -1>',
    #     'position=<-2,  3> velocity=< 1,  0>',
    #     'position=<-4,  3> velocity=< 2,  0>',
    #     'position=<10, -3> velocity=<-1,  1>',
    #     'position=< 5, 11> velocity=< 1, -2>',
    #     'position=< 4,  7> velocity=< 0, -1>',
    #     'position=< 8, -2> velocity=< 0,  1>',
    #     'position=<15,  0> velocity=<-2,  0>',
    #     'position=< 1,  6> velocity=< 1,  0>',
    #     'position=< 8,  9> velocity=< 0, -1>',
    #     'position=< 3,  3> velocity=<-1,  1>',
    #     'position=< 0,  5> velocity=< 0, -1>',
    #     'position=<-2,  2> velocity=< 2,  0>',
    #     'position=< 5, -2> velocity=< 1,  2>',
    #     'position=< 1,  4> velocity=< 2,  1>',
    #     'position=<-2,  7> velocity=< 2, -2>',
    #     'position=< 3,  6> velocity=<-1, -1>',
    #     'position=< 5,  0> velocity=< 1,  0>',
    #     'position=<-6,  0> velocity=< 2,  0>',
    #     'position=< 5,  9> velocity=< 1, -2>',
    #     'position=<14,  7> velocity=<-2,  0>',
    #     'position=<-3,  6> velocity=< 2, -1>',
    # ]

    lines = [[int(i) for i in re.findall(r'-?\d+', line)] for line in data]
    index = -1
    size = -1

    console.log('Task', 'Finding smallest sized object to work with')
    timer.start()
    for i in range(20000):
        minx = min(x + i * vx for (x, y, vx, vy) in lines)
        maxx = max(x + i * vx for (x, y, vx, vy) in lines)
        miny = min(y + i * vy for (x, y, vx, vy) in lines)
        maxy = max(y + i * vy for (x, y, vx, vy) in lines)

        test_size = maxx - minx + maxy - miny

        if size == -1 or test_size < size:
            size = test_size
            index = i

    console.log('Found', 'Min Size {} for index {}'.format(size, index))

    console.log('Task', 'Building data map')
    my_map = [[' '] * 200 for j in range(400)]
    for (x, y, vx, vy) in lines:
        my_map[y + index * vy][x + index * vx - 250] = '*'

    console.log('Task', 'Saving map to file')
    with open(join(ctx.obj['BASE_PATH'], 'output.txt'), 'w+') as handle:
        for m in my_map:
            handle.write(''.join(m) + '\n')

    timer.end()

    console.header('output')
    timer.output()
Example #12
0
def executor():
    console.header('day 7, part 1')

    timer = Timer()
    steps = DataProvider.load('day7')
    # steps = [
    #     'Step C must be finished before step A can begin.',
    #     'Step C must be finished before step F can begin.',
    #     'Step A must be finished before step B can begin.',
    #     'Step A must be finished before step D can begin.',
    #     'Step B must be finished before step E can begin.',
    #     'Step D must be finished before step E can begin.',
    #     'Step F must be finished before step E can begin.',
    # ]

    timer.start()

    console.log('Task', 'Build nodes')
    node_names = set([])
    for step in steps:
        [parent, child] = re.match(r'Step ([A-Z]) .*? step ([A-Z]).*',
                                   step).group(1, 2)
        node_names.add(parent)
        node_names.add(child)

    console.log('Info', 'Creating {} nodes'.format(len(node_names)))
    nodes = []
    for node_name in node_names:
        nodes.append(Node(node_name))

    console.log('Info', 'Executing steps')
    count = 0
    for step in steps:
        count += 1
        console.log('Info', 'Step {} / {}'.format(count, len(steps)))

        [parent, child] = re.match(r'Step ([A-Z]) .*? step ([A-Z]).*',
                                   step).group(1, 2)

        parent = find(parent, nodes)
        child = find(child, nodes)

        parent.add_child(child)

    console.log('Info', 'Creating root node and children')
    root_node = Node('-')
    for node in [node for node in nodes if node.is_root()]:
        root_node.add_child(node)

    console.header('node picker')
    available_picks = root_node.child_nodes
    output = ''
    while available_picks:
        picks = set(sorted([n.name for n in available_picks]))
        pick = min(picks)

        console.log(
            'Info', 'Picking {} from {{ {} }}'.format(
                pick, ', '.join([n.name for n in available_picks])))

        node = find(pick, nodes)
        node.triggered = True
        output += node.name
        available_picks = list(
            filter(lambda n: n.name != pick, available_picks)) + [
                n for n in node.child_nodes if n.can_trigger()
            ]

    timer.end()

    console.header('output')
    console.log('Output', output)
    timer.output()