Ver código fonte

2016/22

master
Roderic Day 4 anos atrás
pai
commit
684c0b2ef0
3 arquivos alterados com 127 adições e 0 exclusões
  1. +13
    -0
      y2016/p20.py
  2. +74
    -0
      y2016/p21.py
  3. +40
    -0
      y2016/p22.py

+ 13
- 0
y2016/p20.py Ver arquivo

@@ -0,0 +1,13 @@
import sys


text = sys.stdin.read()
pairs = [tuple(map(int, line.split('-'))) for line in text.splitlines()]
valid, lo, hi = [], 0, 0
for a, b in sorted(pairs):
if a > hi:
valid.extend(range(hi + 1, a))
lo, hi = a, b
lo, hi = min(lo, a), max(hi, b)
print(valid[0])
print(len(valid))

+ 74
- 0
y2016/p21.py Ver arquivo

@@ -0,0 +1,74 @@
import doctest
import itertools
import sys


def swap(text, a1, a2):
if str(a1).isdigit():
a1 = text[int(a1)]
if str(a2).isdigit():
a2 = text[int(a2)]
return text.translate(str.maketrans(a1 + a2, a2 + a1))


def reverse(text, a1, a2):
a1, a2 = int(a1), int(a2) + 1
return text[:a1] + text[a1:a2][::-1] + text[a2:]


def rotate_left(text, n):
return text[n:] + text[:n]


def rotate_right(text, n):
return rotate_left(text, -n)


def rotate_based(text, c):
n = text.index(c)
if n >= 4:
n += 1
n += 1
n %= len(text)
return text[-n:] + text[:-n]


def move(text, a1, a2):
c = text[a1]
text = text[:a1] + text[a1 + 1:]
return text[:a2] + c + text[a2:]


def scramble(text):
'''
>>> swap('abcde', 4, 0)
'ebcda'
>>> swap(_, 'd', 'b')
'edcba'
>>> reverse(_, 0, 4)
'abcde'
>>> rotate_left(_, 1)
'bcdea'
>>> move(_, 1, 4)
'bdeac'
>>> move(_, 3, 0)
'abdec'
>>> rotate_based(_, 'b')
'ecabd'
>>> rotate_based(_, 'd')
'decab'
'''
for line in instructions:
fn, *args = line.replace('rotate ', 'rotate_').split(' ')
args = [int(c) if c.isdigit() else c for c in args if len(c) == 1]
text = eval(fn)(text, *args)
return text


instructions = sys.stdin.read().splitlines()
doctest.testmod()
print(scramble('abcdefgh'))

for perm in [''.join(seq) for seq in itertools.permutations('abcdefgh')]:
if scramble(perm) == 'fbgdceah':
print(perm)

+ 40
- 0
y2016/p22.py Ver arquivo

@@ -0,0 +1,40 @@
import collections
import itertools
import re
import sys

import toolkit


Node = collections.namedtuple('Node', 'x, y, size, used, avail, pc')
nodes = []
text = sys.stdin.read()
for line in text.splitlines():
found = list(map(int, re.findall(r'\d+', line)))
if found:
nodes.append(Node(*found))

ans1 = 0
for a, b in itertools.permutations(nodes, 2):
if a.used and a.used <= b.avail:
ans1 += 1
print(ans1)


def glyph(node):
if node.used == 0:
return '_'
elif node.x == 0 and node.y == 0:
return '^'
elif node.x == 29 and node.y == 0:
return '@'
elif node.used > 100:
return '#'
elif node.size > 70:
return '.'
else:
return ' '


print(toolkit.render({complex(node.x, node.y): glyph(node) for node in nodes}))
print(5 + 33 + 6 + 5 * 28 + 1)

Carregando…
Cancelar
Salvar