solution.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/python3
  2. import sys
  3. import re
  4. def less(a, b):
  5. return a < b
  6. def greater(a, b):
  7. return a > b
  8. def part1():
  9. print('part 1')
  10. flow = {}
  11. pos = {'x': 0, 'm': 1, 'a': 2, 's': 3}
  12. for line in map(str.strip, sys.stdin):
  13. if len(line) == 0:
  14. break
  15. workflow, rulestring = re.findall(r'([a-z]+)\{(.*)\}', line)[0]
  16. rules = rulestring.split(',')
  17. flow[workflow] = []
  18. for rule in rules:
  19. extracted = re.findall(r'([ARa-z]+)?([><])?([0-9]+)?:?([ARa-z]+)?', rule)[0]
  20. if extracted[1] == '':
  21. flow[workflow].append((extracted[0],))
  22. else:
  23. flow[workflow].append((pos[extracted[0]], less if extracted[1] == '<' else greater, int(extracted[2]), extracted[3]))
  24. ratings = 0
  25. for line in map(str.strip, sys.stdin):
  26. stats = [int(n) for n in re.findall(r'\{x=(\d+),m=(\d+),a=(\d+),s=(\d+)\}', line)[0]]
  27. workflow = 'in'
  28. while workflow not in 'RA':
  29. rules = flow[workflow]
  30. for rule in rules:
  31. if len(rule) == 1:
  32. workflow = rule[0]
  33. break
  34. else:
  35. i, fun, value, target = rule
  36. if fun(stats[i], value):
  37. workflow = target
  38. break
  39. if workflow == 'A':
  40. ratings += sum(stats)
  41. print(ratings)
  42. def part2():
  43. print('part 2')
  44. flow = {}
  45. pos = {'x': 0, 'm': 1, 'a': 2, 's': 3}
  46. for line in map(str.strip, sys.stdin):
  47. if len(line) == 0:
  48. break
  49. workflow, rulestring = re.findall(r'([a-z]+)\{(.*)\}', line)[0]
  50. rules = rulestring.split(',')
  51. flow[workflow] = []
  52. for rule in rules:
  53. extracted = re.findall(r'([ARa-z]+)?([><])?([0-9]+)?:?([ARa-z]+)?', rule)[0]
  54. if extracted[1] == '':
  55. flow[workflow].append((extracted[0],))
  56. else:
  57. flow[workflow].append((pos[extracted[0]], less if extracted[1] == '<' else greater, int(extracted[2]), extracted[3]))
  58. ratings = 0
  59. workflow = 'in'
  60. ranges = [(1, 4000), (1, 4000), (1, 4000), (1, 4000)]
  61. while workflow not in 'RA':
  62. ranges, workflow = nodes.pop()
  63. rules = flow[workflow]
  64. for rule in rules:
  65. if len(rule) == 1:
  66. workflow = rule[0]
  67. break
  68. else:
  69. i, fun, value, target = rule
  70. if fun(stats[i], value):
  71. workflow = target
  72. break
  73. if workflow == 'A':
  74. ratings += sum(stats)
  75. print(ratings)
  76. if sys.argv[1] in '1':
  77. part1()
  78. else:
  79. part2()