Пример #1
0
import sys
from collections import defaultdict
from lib import util

scanners = []
for line in sys.stdin.readlines():
    [d, r] = util.get_ints(line)
    scanners.append((d, r))

scanners.sort(key=lambda x: x[1])

#print(scanners)

delay = 0
while True:
    cost = 0
    caught = False
    for (d, r) in scanners:
        cycle_len = r+r-2
        if (d + delay) % cycle_len == 0:
            cost += d*r
            caught = True
            if delay > 0:
                break
    if delay == 0:
        print(cost)
    if not caught:
        print(delay)
        break
    delay += 1
Пример #2
0
import sys
from lib import util

data = util.get_ints(sys.stdin.readline())
seen = {}
cnt = 0
while str(data) not in seen:
    seen[str(data)] = cnt
    cnt += 1
    x = data.index(max(data))
    y = data[x]
    data[x] = 0
    for i in range(y):
        data[(x+i+1) % len(data)] += 1

print(cnt)
print(cnt - seen[str(data)])

Пример #3
0
import sys
import re
import itertools
from collections import defaultdict
from lib import util

input = sys.stdin
input = open('year2016/day8.in')
#input = open('year2016/day8.sample.in')

screen = [['.'] * 50 for x in range(6)]

for line in input.readlines():
    [a, b] = util.get_ints(line)
    if line.startswith('rect'):
        for y in range(b):
            for x in range(a):
                screen[y][x] = '#'
    elif line.startswith('rotate row'):
        screen[a] = screen[a][-b:] + screen[a][:-b]
    elif line.startswith('rotate col'):
        col = []
        for y in range(6):
            col.append(screen[(y + 6 - b) % 6][a])
        for y in range(6):
            screen[y][a] = col[y]

cnt = 0
for line in screen:
    print(''.join(line))
    cnt += sum([1 for c in line if c == '#'])
Пример #4
0
import sys
import re
import itertools
from collections import defaultdict
from lib.util import get_ints

input = sys.stdin
input = open('year2016/day10.in')
#input = open('year2016/day10.sample.in')

bots = defaultdict(list)  # id -> list
bot_instr = {}
output = {}

for line in input.readlines():
    data = get_ints(line)
    if line.startswith('value'):
        bots[data[1]].append(data[0])
    else:
        c1 = line[line.index('low to') + 7][0]
        c2 = line[line.index('high to') + 8][0]
        bot_instr[data[0]] = ((c1, data[1]), (c2, data[2]))

while True:
    actions = [id for id, contents in bots.items() if len(contents) == 2]
    if not len(actions):
        break
    for id in actions:
        contents = bots[id]
        lo = min(contents[0], contents[1])
        hi = max(contents[0], contents[1])
Пример #5
0
import sys
from collections import defaultdict
from lib import util
from queue import Queue
from aocd import data, submit

lines = data.split('\n')
a = util.get_ints(lines[0])[0]
b = util.get_ints(lines[1])[0]

#a = 65
#b = 8921

af = 16807
bf = 48271
MOD = 2147483647
cnt = 0

for i in range(5000000):
    a = a * af % MOD
    while a % 4 != 0:
        a = a * af % MOD
    b = b * bf % MOD
    while b % 8 != 0:
        b = b * bf % MOD
    if a & 65535 == b & 65535:
        cnt += 1
    if i % 100000 == 0:
        print(i)

print(cnt)
Пример #6
0
# pe/b
# '''

lines = data.strip().split(',')
s = 'abcdefghijklmnop'
perm = list(range(len(s)))

for dances in range(10):
    for line in lines:
        if line[0] == 's':
            x = int(line[1:])
            s = s[len(s) - x:] + s[:len(s) - x]
            if dances == 0:
                perm = perm[len(perm) - x:] + perm[:len(perm) - x]
        elif line[0] == 'x':
            [p, q] = util.get_ints(line)
            if p > q:
                tmp = p
                p = q
                q = tmp
            s = s[0:p] + s[q] + s[p + 1:q] + s[p] + s[q + 1:]
            if dances == 0:
                perm = perm[0:p] + [perm[q]] + perm[p + 1:q] + [
                    perm[p]
                ] + perm[q + 1:]
        elif line[0] == 'p':
            x = line[1]
            y = line[3]
            s = s.replace(x, 'X').replace(y, x).replace('X', y)
    print('dance %d: %s' % (dances + 1, s))
    #if dances == 0:
Пример #7
0
import sys
from queue import Queue
from collections import defaultdict
from lib import util

graph = {}

for line in sys.stdin.readlines():
    ints = util.get_ints(line)
    graph[ints[0]] = ints[1:]

seen = set()
groups = 0
for y in range(len(graph)):
    if y not in seen:
        groups += 1
        q = Queue()
        q.put(y)
        seen.add(y)
        while not q.empty():
            cur = q.get()
            for x in graph[cur]:
                if x not in seen:
                    q.put(x)
                    seen.add(x)

        if groups == 1:
            print(len(seen))

print(groups)
Пример #8
0
import sys
from collections import defaultdict
from lib import util
from queue import Queue
#from aocd import data, submit

# Make sure AOC_SESSION is updated! (Chrome inspector -> Application tab -> session)

code = '1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,5,19,23,1,23,6,27,1,5,27,31,1,31,6,35,1,9,35,39,2,10,39,43,1,43,6,47,2,6,47,51,1,5,51,55,1,55,13,59,1,59,10,63,2,10,63,67,1,9,67,71,2,6,71,75,1,5,75,79,2,79,13,83,1,83,5,87,1,87,9,91,1,5,91,95,1,5,95,99,1,99,13,103,1,10,103,107,1,107,9,111,1,6,111,115,2,115,13,119,1,10,119,123,2,123,6,127,1,5,127,131,1,5,131,135,1,135,6,139,2,139,10,143,2,143,9,147,1,147,6,151,1,151,13,155,2,155,9,159,1,6,159,163,1,5,163,167,1,5,167,171,1,10,171,175,1,13,175,179,1,179,2,183,1,9,183,0,99,2,14,0,0'

for noun in range(100):
    for verb in range(100):
        program = util.get_ints(code)

        program[1] = noun
        program[2] = verb

        pc = 0
        while True:
            if program[pc] == 1:
                program[program[
                    pc +
                    3]] = program[program[pc + 1]] + program[program[pc + 2]]
            elif program[pc] == 2:
                program[program[
                    pc +
                    3]] = program[program[pc + 1]] * program[program[pc + 2]]
            elif program[pc] == 99:
                break
            else:
                assert False