コード例 #1
0
import loader

if __name__ == '__main__':
    data_raw = loader.loadInLines('day2.in')

    count = 0
    count2 = 0
    for i in data_raw:
        i = i.strip()
        i = i.split(' ')

        policy_mm = i[0].split('-')
        lower = int(policy_mm[0])
        upper = int(policy_mm[1])
        letter = i[1][0]
        pw = i[2]

        c = pw.count(letter)

        # PART1
        if c >= lower and c <= upper:
            count += 1

        # PART2
        t = 0
        if pw[lower - 1] == letter:
            t += 1
        if pw[upper - 1] == letter:
            t += 1
        if t == 1:
            count2 += 1
コード例 #2
0
ファイル: day8.py プロジェクト: natfaulk/Advent_of_code_2020
    instr=_program[pc]
    instrs_run.add(pc)
    if instr[0]=='jmp':
      pc+=instr[1]
      continue
      
    if instr[0]=='acc':
      acc+=instr[1]

    pc+=1

  return acc,cleanExit

if __name__ == '__main__':
  data = loader.loadInLines('day8.in')

  program=[decodeLine(i) for i in data]
  
  # PART 1
  acc,cleanExit=checkProgram(program)
  print(acc)

  # PART 2
  i=0
  while i < len(program):
    if program[i][0] != 'acc':
      program_copy=copy.deepcopy(program)
      if program_copy[i][0]=='nop':
        program_copy[i][0]='jmp'
      else:
コード例 #3
0
import loader

if __name__ == '__main__':
    data = loader.loadInLines('day10.in')

    jolts = [0] + [int(i) for i in data]
    jolts += [max(jolts) + 3]
    jolts.sort()

    diffs = [0 for i in range(4)]
    for i in range(1, len(jolts)):
        diffs[jolts[i] - jolts[i - 1]] += 1

    # PART 1
    print(diffs[1] * diffs[3])

    # PART 2
    # number of combinations can be worked out from the length of a sequence of ones
    # 1: 1 combination
    # 2: 2
    # 3: 4
    # 4: 7
    # 5: 13
    # 6: 24
    # 7: 44

    # The pattern is n = (n-1)+(n-2)+(n-3)

    # cache this pattern
    n_combs = [0, 1, 2, 4, 7, 13]