|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +importos |
| 3 | + |
| 4 | +test_data= {} |
| 5 | + |
| 6 | +test=1 |
| 7 | +test_data[test]= {"input":"""p=<3,0,0>, v=<2,0,0>, a=<-1,0,0> |
| 8 | +p=<4,0,0>, v=<0,0,0>, a=<-2,0,0>""", |
| 9 | +"expected": ['Unknown','Unknown'], |
| 10 | + } |
| 11 | + |
| 12 | +test+=1 |
| 13 | +test_data[test]= {"input":"""p=<-6,0,0>, v=<3,0,0>, a=<0,0,0> |
| 14 | +p=<-4,0,0>, v=<2,0,0>, a=<0,0,0> |
| 15 | +p=<-2,0,0>, v=<1,0,0>, a=<0,0,0> |
| 16 | +p=<3,0,0>, v=<-1,0,0>, a=<0,0,0>""", |
| 17 | +"expected": ['Unknown','Unknown'], |
| 18 | + } |
| 19 | + |
| 20 | +test='real' |
| 21 | +input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt')) |
| 22 | +test_data[test]= {"input":open(input_file,"r+").read().strip(), |
| 23 | +"expected": ['Unknown','Unknown'], |
| 24 | + } |
| 25 | + |
| 26 | +# -------------------------------- Control program execution -------------------------------- # |
| 27 | + |
| 28 | +case_to_test='real' |
| 29 | +part_to_test=2 |
| 30 | +verbose_level=1 |
| 31 | + |
| 32 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 33 | + |
| 34 | +puzzle_input=test_data[case_to_test]['input'] |
| 35 | +puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
| 36 | +puzzle_actual_result='Unknown' |
| 37 | + |
| 38 | + |
| 39 | +# -------------------------------- Actual code execution -------------------------------- # |
| 40 | + |
| 41 | +max_accel=10**6 |
| 42 | + |
| 43 | +ifpart_to_test==1: |
| 44 | +part_nr=0 |
| 45 | +forstringinpuzzle_input.split('\n'): |
| 46 | +_,_,acceleration=string.split(' ') |
| 47 | +acceleration=list(map(int,acceleration[3:-1].split(','))) |
| 48 | + |
| 49 | +ifmax_accel>sum(map(abs,acceleration)): |
| 50 | +max_accel=sum(map(abs,acceleration)) |
| 51 | +closest_part=part_nr |
| 52 | + |
| 53 | +part_nr+=1 |
| 54 | + |
| 55 | +puzzle_actual_result=closest_part |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +else: |
| 60 | +particles= {} |
| 61 | +collisions= [] |
| 62 | +part_nr=0 |
| 63 | +saved_len=0 |
| 64 | +forstringinpuzzle_input.split('\n'): |
| 65 | +position,speed,acceleration=string.split(' ') |
| 66 | +position=list(map(int,position[3:-2].split(','))) |
| 67 | +speed=list(map(int,speed[3:-2].split(','))) |
| 68 | +acceleration=list(map(int,acceleration[3:-1].split(','))) |
| 69 | + |
| 70 | +particles[part_nr]= [position,speed,acceleration] |
| 71 | + |
| 72 | +part_nr+=1 |
| 73 | + |
| 74 | +foriinrange(10**4): |
| 75 | +collisions= [] |
| 76 | +forpart_nrinparticles: |
| 77 | +position,speed,acceleration=particles[part_nr] |
| 78 | +speed= [speed[x]+acceleration[x]forxinrange (3)] |
| 79 | +position= [position[x]+speed[x]forxinrange (3)] |
| 80 | +particles[part_nr]= [position,speed,acceleration] |
| 81 | +collisions.append(position) |
| 82 | + |
| 83 | +coordinates= [','.join(map(str,collision))forcollisionincollisions] |
| 84 | + |
| 85 | +list_particles=list(particles.keys()) |
| 86 | +forpart_nrinlist_particles: |
| 87 | +ifcollisions.count(particles[part_nr][0])>1: |
| 88 | +delparticles[part_nr] |
| 89 | + |
| 90 | +ifi%10==0andlen(particles)==saved_len: |
| 91 | +break |
| 92 | +elifi%10==0: |
| 93 | +saved_len=len(particles) |
| 94 | + |
| 95 | +print(i,len(particles)) |
| 96 | + |
| 97 | +puzzle_actual_result=len(particles) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +# -------------------------------- Outputs / results -------------------------------- # |
| 103 | + |
| 104 | +ifverbose_level>=3: |
| 105 | +print ('Input : '+puzzle_input) |
| 106 | +print ('Expected result : '+str(puzzle_expected_result)) |
| 107 | +print ('Actual result : '+str(puzzle_actual_result)) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |