10.py 585 B

12345678910111213141516171819202122232425262728293031323334
  1. # Read lines
  2. lines = []
  3. with open("10.input") as f:
  4. for line in f.readlines():
  5. lines.append(int(line.strip()))
  6. lines.sort()
  7. current = 0
  8. diffs = [0, 0, 0, 0]
  9. for next in lines:
  10. diffs[next - current] += 1
  11. current = next
  12. diffs[3] += 1 # Device joltage
  13. print("Answer 1: " + str(diffs[1] * diffs[3]))
  14. count_from = {}
  15. count_from[max(lines)] = 1
  16. lines.reverse()
  17. lines.pop(0)
  18. for current in lines + [0]:
  19. count_from[current] = 0
  20. for i in range(1, 4):
  21. count_from[current] += count_from.get(current + i, 0)
  22. print("Answer 2: " + str(count_from[0]))