Working out solutions for Advent of Code
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

02b.py 809B

5 år sedan
123456789101112131415161718192021222324252627282930
  1. # Python 3.7
  2. import copy
  3. def main():
  4. instrs = ""
  5. with open("02in.txt","r") as file:
  6. instrs = file.readline()
  7. instrs.strip('\n')
  8. ints = [int(x) for x in instrs.split(',')]
  9. default_ints = copy.deepcopy(ints)
  10. for j in range(100):
  11. for k in range(100):
  12. ints = copy.deepcopy(default_ints)
  13. ints[1] = j
  14. ints[2] = k
  15. i = 0
  16. while ints[i] != 99:
  17. if ints[i] == 1:
  18. v = ints[ints[i+1]] + ints[ints[i+2]]
  19. if ints[i] == 2:
  20. v = ints[ints[i+1]] * ints[ints[i+2]]
  21. ints[ints[i+3]] = v
  22. i += 4
  23. if ints[0] == 19690720:
  24. print((100*j) + k)
  25. return
  26. if __name__ == "__main__":
  27. main()