コード例 #1
0
# -- PART 1 -- #
def escape(word: str):
    ret_string = word[1:-1]

    ret_string = ret_string.replace('\\\\', '¡').replace('\\"', '"')

    while '\\x' in ret_string:
        scape_index = ret_string.index('\\x')
        ret_string = ret_string[:scape_index] + '@' + ret_string[scape_index +
                                                                 4:]

    return ret_string


print_part_1(
    sum([len(word) for word in santa_list]) -
    sum([len(escape(word)) for word in santa_list]))


# -- PART 2 -- #
def encode(word: str):
    ret_string = '?' + word + '?'

    ret_string = ret_string.replace('\\', '¡¡').replace('"', '@@')

    return ret_string


print_part_2(
    sum([len(encode(word))
         for word in santa_list]) - sum([len(word) for word in santa_list]))
コード例 #2
0
from utils import read_file, print_part_1, print_part_2

input = read_file('input.txt')[0]

# -- PART 1 -- #
print_part_1(input.count('(') - input.count(')'))

# -- PART 2 -- #
floor = 0
for index, character in enumerate(input, start=1):
    if character == '(':
        floor += 1
    else:
        floor -= 1

    if floor == -1:
        break

print_part_2(index)
コード例 #3
0
    for letter in string:
        if letter == previous_letter:
            has_doubles = True

        previous_letter = letter

    return has_doubles


nice_strings = 0
for string in strings:
    if is_nice(string):
        nice_strings += 1

print_part_1(nice_strings)


# -- PART 2 -- #
def is_nice_v2(string):
    has_pairs = False
    for i in range(len(string) - 3):
        if string[i:i + 2] in string[i + 2:]:
            has_pairs = True

    if not has_pairs:
        return False

    has_doubles = False
    previous_letter = ''
    two_previous_letter = ''
コード例 #4
0
        return int(output)
    if output in values:
        return values[output]

    input = circuit[output]

    if not isinstance(input, list):
        values[output] = int(input)
    elif len(input) == 1:
        values[output] = get_value(input[0])
    elif input[0] == 'NOT':
        values[output] = complement(get_value(input[1]))
    elif input[1] == 'AND':
        values[output] = get_value(input[0]) & get_value(input[2])
    elif input[1] == 'OR':
        values[output] = get_value(input[0]) | get_value(input[2])
    elif input[1] == 'LSHIFT':
        values[output] = get_value(input[0]) << int(input[2])
    elif input[1] == 'RSHIFT':
        values[output] = get_value(input[0]) >> int(input[2])

    return get_value(output)


print_part_1(get_value('a'))

# -- PART 2 -- #
values = {'b': get_value('a')}

print_part_2(get_value('a'))
コード例 #5
0
from itertools import groupby

from utils import read_file, print_part_1, print_part_2

start_sequence = read_file('input.txt')[0]


# -- PART 1 -- #
def look_and_say(sequence: str):
    return ''.join(str(len(list(g))) + k for k, g in groupby(sequence))


sequence = start_sequence
for i in range(40):
    sequence = look_and_say(sequence)

print_part_1(len(sequence))

# -- PART 2 -- #
for i in range(10):
    sequence = look_and_say(sequence)

print_part_2(len(sequence))
コード例 #6
0
for instruction in instructions:
    command, coordinates = list(instruction.items())[0]
    row_start = coordinates[0][0]
    col_start = coordinates[0][1]
    row_end = coordinates[1][0] + 1
    col_end = coordinates[1][1] + 1

    coordinates = np.index_exp[row_start:row_end, col_start:col_end]
    if command == 'on':
        lights_grid[coordinates] = 1
    elif command == 'off':
        lights_grid[coordinates] = 0
    else:
        lights_grid[coordinates] = np.bitwise_not(lights_grid[coordinates])

print_part_1(np.sum(lights_grid))

# -- PART 2 -- #
lights_grid = np.zeros((1000, 1000), dtype=int)

for instruction in instructions:
    command, coordinates = list(instruction.items())[0]
    row_start = coordinates[0][0]
    col_start = coordinates[0][1]
    row_end = coordinates[1][0] + 1
    col_end = coordinates[1][1] + 1

    coordinates = np.index_exp[row_start:row_end, col_start:col_end]
    if command == 'on':
        lights_grid[coordinates] += 1
    elif command == 'off':
コード例 #7
0
# -- PART 1 -- #
feet_of_wrapping_paper = 0
for dimension in dimensions:
    l, w, h = dimension

    feet_of_wrapping_paper += 2 * (l * w + w * h + h * l)

    if max(l, w, h) == l:
        feet_of_wrapping_paper += w * h
    elif max(l, w, h) == w:
        feet_of_wrapping_paper += l * h
    else:
        feet_of_wrapping_paper += l * w

print_part_1(feet_of_wrapping_paper)

# -- PART 2 -- #
feet_of_ribbon = 0
for dimension in dimensions:
    l, w, h = dimension

    feet_of_ribbon += l * w * h

    if max(l, w, h) == l:
        feet_of_ribbon += 2 * (w + h)
    elif max(l, w, h) == w:
        feet_of_ribbon += 2 * (l + h)
    else:
        feet_of_ribbon += 2 * (l + w)
コード例 #8
0
from utils import read_file, print_part_1, print_part_2
from hashlib import md5

secret_key = read_file('input.txt')[0]

# -- PART 1 -- #
num = 0
while 1:
    hash = md5((secret_key + str(num)).encode('utf-8')).hexdigest()
    if hash[:5] == '00000':
        break

    num += 1

print_part_1(num)

# -- PART 2 -- #
while 1:
    hash = md5((secret_key + str(num)).encode('utf-8')).hexdigest()
    if hash[:6] == '000000':
        break

    num += 1

print_part_2(num)
コード例 #9
0
from tsp_solver.greedy import solve_tsp
from tsp_solver.util import path_cost

from utils import read_file, print_part_1, print_part_2

distances_unparsed = [
    int(line.split(' = ')[1]) for line in read_file('input.txt')
]

# -- PART 1 -- #
distances = [[] for _ in range(8)]

for i in range(7):
    for j in range(7 - i):
        distances[i + 1 + j].append(distances_unparsed[j])
    distances_unparsed = distances_unparsed[7 - i:]

path = solve_tsp(distances)

print_part_1(path_cost(distances, path))

# -- PART 2 -- #
for i in range(len(distances)):
    for j in range(len(distances[i])):
        distances[i][j] = 1000 - distances[i][j]

path = solve_tsp(distances)

print_part_2(1000 * (len(path) - 1) - path_cost(distances, path))
コード例 #10
0
houses_visited = set()
houses_visited.add((0, 0))

for instruction in instructions:
    if instruction == '^':
        x_position += 1
    elif instruction == 'v':
        x_position -= 1
    elif instruction == '>':
        y_position += 1
    elif instruction == '<':
        y_position -= 1

    houses_visited.add((x_position, y_position))

print_part_1(len(houses_visited))

# -- PART 2 -- #
santa_x_position = 0
santa_y_position = 0
robot_x_position = 0
robot_y_position = 0

houses_visited = set()
houses_visited.add((0, 0))

for i in range(0, len(instructions), 2):
    instruction = instructions[i]

    if instruction == '^':
        santa_x_position += 1