|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +importos,collections |
| 3 | + |
| 4 | +test_data= {} |
| 5 | + |
| 6 | +test=1 |
| 7 | +test_data[test]= {"input":"""9 players; last marble is worth 25 points""", |
| 8 | +"expected": ['32','Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test+=1 |
| 12 | +test_data[test]= {"input":"""10 players; last marble is worth 1618 points""", |
| 13 | +"expected": ['8317','Unknown'], |
| 14 | + } |
| 15 | + |
| 16 | +test+=1 |
| 17 | +test_data[test]= {"input":"""13 players; last marble is worth 7999 points""", |
| 18 | +"expected": ['146373','Unknown'], |
| 19 | + } |
| 20 | + |
| 21 | +test+=1 |
| 22 | +test_data[test]= {"input":"""17 players; last marble is worth 1104 points""", |
| 23 | +"expected": ['2764','Unknown'], |
| 24 | + } |
| 25 | + |
| 26 | +test+=1 |
| 27 | +test_data[test]= {"input":"""21 players; last marble is worth 6111 points""", |
| 28 | +"expected": ['54718','Unknown'], |
| 29 | + } |
| 30 | + |
| 31 | +test+=1 |
| 32 | +test_data[test]= {"input":"""30 players; last marble is worth 5807 points""", |
| 33 | +"expected": ['37305','Unknown'], |
| 34 | + } |
| 35 | + |
| 36 | +test='real' |
| 37 | +input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt')) |
| 38 | +test_data[test]= {"input":'404 players; last marble is worth 71852 points', |
| 39 | +"expected": ['434674','3653994575'], |
| 40 | + } |
| 41 | + |
| 42 | +# -------------------------------- Control program execution -------------------------------- # |
| 43 | + |
| 44 | +case_to_test='real' |
| 45 | +part_to_test=2 |
| 46 | + |
| 47 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 48 | + |
| 49 | +puzzle_input=test_data[case_to_test]['input'] |
| 50 | +puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
| 51 | +puzzle_actual_result='Unknown' |
| 52 | + |
| 53 | + |
| 54 | +# -------------------------------- Actual code execution -------------------------------- # |
| 55 | + |
| 56 | +forstringinpuzzle_input.split('\n'): |
| 57 | +nb_players,_,_,_,_,_,points,_=string.split(' ') |
| 58 | +nb_players,points=map(int, (nb_players,points)) |
| 59 | + |
| 60 | + |
| 61 | +ifpart_to_test==2: |
| 62 | +points*=100 |
| 63 | + |
| 64 | +position=0 |
| 65 | +scores= [0]*nb_players |
| 66 | +ifpart_to_test==1: |
| 67 | +marbles= [0,1] |
| 68 | +fornew_marbleinrange(2,points+1): |
| 69 | +ifnew_marble%23==0: |
| 70 | +scores[new_marble%nb_players]+=new_marble |
| 71 | +position= (position-7)%len(marbles) |
| 72 | +scores[new_marble%nb_players]+=marbles[position-1] |
| 73 | +delmarbles[position-1] |
| 74 | +else: |
| 75 | +marbles.insert(position+1,new_marble) |
| 76 | +position= ((position+2)%len(marbles)) |
| 77 | + |
| 78 | +ifnew_marble%10000==0: |
| 79 | +print (new_marble) |
| 80 | + |
| 81 | + |
| 82 | +else: |
| 83 | +marbles=collections.deque([0,1]) |
| 84 | +fornew_marbleinrange(2,points+1): |
| 85 | +ifnew_marble%23==0: |
| 86 | +scores[new_marble%nb_players]+=new_marble |
| 87 | +marbles.rotate(7) |
| 88 | +scores[new_marble%nb_players]+=marbles.pop() |
| 89 | +marbles.rotate(-1) |
| 90 | +else: |
| 91 | +marbles.rotate(-1) |
| 92 | +marbles.append(new_marble) |
| 93 | + |
| 94 | +puzzle_actual_result=max(scores) |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +# -------------------------------- Outputs / results -------------------------------- # |
| 100 | + |
| 101 | +print ('Expected result : '+str(puzzle_expected_result)) |
| 102 | +print ('Actual result : '+str(puzzle_actual_result)) |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |