Solutions to Advent of Code 2017 https://adventofcode.com/2017/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112
  1. with open('data/01.txt', 'r') as file:
  2. captcha = file.readline().strip() * 2
  3. length = len(captcha) // 2
  4. print(f'Part one: {sum(int(captcha[i]) for i in range(length) if captcha[i] == captcha[i+1])}')
  5. print(f'Part two: {sum(int(captcha[i]) for i in range(length) if captcha[i] == captcha[i + length // 2])}')
  6. # Alternatively...
  7. import numpy as np
  8. with open('data/01.txt', 'r') as file:
  9. captcha = list(file.readline().strip())
  10. for part, roll in enumerate([1, len(captcha)//2]):
  11. print(f'Part {part+1}: {sum(int(i) for i, j in zip(captcha, np.roll(captcha, roll)) if i == j)}')