Exemple #1
0
import input

bps = input.lines(5)


def seatId(code: str) -> int:
    return int(
        code.replace('F', '0').replace('B',
                                       '1').replace('L',
                                                    '0').replace('R', '1'), 2)


ids = [seatId(x) for x in bps]

print(max(ids))

print([
    x for x in range(971) if x not in ids and x - 1 in ids and x + 1 in ids
][0])
Exemple #2
0
import input

grid = [[x == '#' for x in row] for row in input.lines(3)]
width = len(grid[0])


def trees(xstep, ystep):
    x = 0
    y = 0
    trees = 0
    while y < len(grid):
        if grid[y][x % width]:
            trees += 1
        x += xstep
        y += ystep
    return trees


print(trees(3, 1))
print(trees(1, 1) * trees(3, 1) * trees(5, 1) * trees(7, 1) * trees(1, 2))
Exemple #3
0
import input
import re

rules = input.lines(7)

# build a dict of sets of parents and children
allowed_parents = {}
child_bags = {}
for rule in rules:
    (parent, children) = rule.rstrip('.').split(' bags contain ')
    child_bags[parent] = []

    if children == 'no other bags':
        continue
    
    children = children.split(', ')
    for child in [re.match(r"(\d*) (.*) bags?", x) for x in children]:
        if child.group(2) not in allowed_parents:
            allowed_parents[child.group(2)] = set()
        allowed_parents[child.group(2)].add(parent)

        child_bags[parent].append((int(child.group(1)), child.group(2)))

can_contain_mine = set()
def pop_can_contain(colour):
    for x in allowed_parents.get(colour, set()):
        can_contain_mine.add(x)
        pop_can_contain(x)

pop_can_contain('shiny gold')
print(len(can_contain_mine))
Exemple #4
0
import input

numbers = [int(x) for x in input.lines(9)]

preamble_size = 25
i = preamble_size

while i < len(numbers):
    target = numbers[i]
    for first in range(i - preamble_size, i):
        for second in range(first + 1, i):
            if numbers[first] + numbers[second] == target:
                break
        else:
            continue
        break
    else:
        print(target)
        break
    i += 1

# Part 2
# Use sliding window
start = 0
end = 1
total = numbers[start] + numbers[end]
while start < len(numbers) - 1:
    if total == target:
        print(max(numbers[start:end + 1]) + min(numbers[start:end + 1]))
        break
    elif total < target:
Exemple #5
0
    def __init__(self, program: list):
        self.program = program

    def step(self):
        if self.pos > len(self.program):
            return True
        (cmd, param) = self.program[self.pos].split(' ')
        param = int(param)
        if cmd == 'acc':
            self.acc += param
        self.pos += param if cmd == 'jmp' else 1
        return False


program = input.lines(8)
computer = Computer(program)

pos = 0
jmpsAndNops = []
while (True):
    if computer.program[pos].split(' ')[0] in {'jmp', 'nop'}:
        jmpsAndNops.append(pos)
    computer.step()
    newPos = computer.pos
    computer.program[pos] = None
    pos = newPos
    if computer.program[pos] == None:
        break

print(computer.acc)
Exemple #6
0
import input

directions = input.lines(12)

facing = 0  # degrees from east
pos = [0, 0]  # east, north

for dir in directions:
    inst = dir[0]
    amount = int(dir[1:])
    if inst == 'N':
        pos[1] += amount
    elif inst == 'S':
        pos[1] -= amount
    elif inst == 'E':
        pos[0] += amount
    elif inst == 'W':
        pos[0] -= amount
    elif inst == 'F':
        if facing == 0:
            pos[0] += amount
        elif facing == 90:
            pos[1] -= amount
        elif facing == 180:
            pos[0] -= amount
        elif facing == 270:
            pos[1] += amount
    elif inst == 'R':
        facing = (facing + amount) % 360
    elif inst == 'L':
        facing = (360 + facing - amount) % 360
Exemple #7
0
import input

(time, buses) = input.lines(13)
time = int(time)
buses = [int(x) for x in buses.split(',') if x != 'x']
print(buses)
print([x - (time % x) for x in buses])
Exemple #8
0
import input
import re

lines = input.lines(2)

def isValid1(line):
    (policy, password) = line.split(': ')
    (counts, letter) = policy.split(' ')
    (num1, num2) = [int(x) for x in counts.split('-')]
    matches = re.findall(letter, password)
    return len(matches) >= num1 and len(matches) <= num2

def isValid2(line):
    (policy, password) = line.split(': ')
    (counts, letter) = policy.split(' ')
    (num1, num2) = [int(x) for x in counts.split('-')]
    return (password[num1 - 1] == letter) != (password[num2 - 1] == letter)

print(len([x for x in lines if isValid1(x)]))
print(len([x for x in lines if isValid2(x)]))