Ejemplo n.º 1
0
# -*- coding: utf-8 -*-
from advent_tools import get_input_string, print_answer


def direction(instruction):
    if instruction == '(':
        return 1

    if instruction == ')':
        return -1

    raise ValueError('Instruction has to be either "(" or ")"')


input_string = get_input_string()

first_answer = input_string.count('(') - input_string.count(')')

print_answer(1, first_answer)

current_floor = 0

for idx, instruction in enumerate(input_string):
    current_floor += direction(instruction)

    if current_floor < 0:
        print_answer(2, idx + 1)
        break

if current_floor >= 0:
    print('Second answer: Negative floor was never reached')
Ejemplo n.º 2
0
# -*- coding: utf-8 -*-
from advent_tools import get_input_string, print_answer

shifts = {
    '^': (0, 1),
    'v': (0, -1),
    '>': (1, 0),
    '<': (-1, 0)
}


def move(current_pos, direction):
    shift = shifts[direction]
    return (current_pos[0] + shift[0], current_pos[1] + shift[1])

instructions = get_input_string()

position = (0, 0)
visited = {position}

for direction in instructions:
    position = move(position, direction)
    visited.add(position)

print_answer(1, len(visited))

santa = (0, 0)
robo_santa = (0, 0)

visited = {(0, 0)}