fork(1) download
  1. from __future__ import print_function
  2. from collections import defaultdict
  3.  
  4. cups = defaultdict(set)
  5.  
  6. def add():
  7. for y in sorted(cups, reverse=True):
  8. for x in sorted(cups[y]):
  9. if x+1 in cups[y] and (y+1 not in cups or x not in cups[y+1]):
  10. cups[y+1].add(x)
  11. return
  12.  
  13. x = 0
  14. while True:
  15. if x not in cups[0]:
  16. cups[0].add(x)
  17. return
  18.  
  19. x += 1
  20.  
  21. def remove():
  22. for y in sorted(cups, reverse=True):
  23. cups[y].remove(min(cups[y]))
  24.  
  25. if not cups[y]:
  26. del cups[y]
  27.  
  28. return
  29.  
  30. def print_cups():
  31. for y, row in sorted(cups.items(), reverse=True):
  32. print(" "*y + " ".join(" A"[x in row] for x in range(max(row)+1)))
  33.  
  34. instructions = ""
  35.  
  36. try:
  37. input = raw_input
  38. except NameError:
  39. pass
  40.  
  41. while True:
  42. try:
  43. line = input()
  44. if not line: break
  45. instructions += line
  46.  
  47. except EOFError:
  48. break
  49.  
  50. a = None
  51. r = None
  52.  
  53. for c in [x for x in instructions if ' ' <= x <= '~']:
  54. if a is None or c == a:
  55. add()
  56. a = c
  57.  
  58. elif r is None or c == r:
  59. remove()
  60. r = c
  61.  
  62. print_cups()
Success #stdin #stdout 0.03s 9128KB
stdin
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrraaaaaaaaaaaaaaaaaaarrrrrrrrrrrrrrraaaaaaaaaaaaaarrrrrrrrrrraaaaaaaa
stdout
    A
   A A                   A
  A A A     A   A       A A
 A A A A   A A A A     A A A A
A A A A A A A A A A   A A A A A