|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +importos |
| 3 | + |
| 4 | +test_data= {} |
| 5 | + |
| 6 | +test=1 |
| 7 | +test_data[test]= {"input":"""R2, L3""", |
| 8 | +"expected": ['5','Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test+=1 |
| 12 | +test_data[test]= {"input":"""R2, R2, R2""", |
| 13 | +"expected": ['2','Unknown'], |
| 14 | + } |
| 15 | + |
| 16 | +test+=1 |
| 17 | +test_data[test]= {"input":"""R5, L5, R5, R3""", |
| 18 | +"expected": ['12','Unknown'], |
| 19 | + } |
| 20 | + |
| 21 | +test+=1 |
| 22 | +test_data[test]= {"input":"""R8, R4, R4, R8""", |
| 23 | +"expected": ['Unknown','4'], |
| 24 | + } |
| 25 | + |
| 26 | +test='real' |
| 27 | +input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt')) |
| 28 | +test_data[test]= {"input":open(input_file,"r+").read().strip(), |
| 29 | +"expected": ['Unknown','Unknown'], |
| 30 | + } |
| 31 | + |
| 32 | +# -------------------------------- Control program execution -------------------------------- # |
| 33 | + |
| 34 | +case_to_test='real' |
| 35 | +part_to_test=2 |
| 36 | +verbose_level=1 |
| 37 | + |
| 38 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 39 | + |
| 40 | +puzzle_input=test_data[case_to_test]['input'] |
| 41 | +puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
| 42 | +puzzle_actual_result='Unknown' |
| 43 | + |
| 44 | + |
| 45 | +# -------------------------------- Actual code execution -------------------------------- # |
| 46 | + |
| 47 | +x,y= (0,0) |
| 48 | +locations_visited= [(x,y)] |
| 49 | +direction=0 |
| 50 | +ifpart_to_test==1: |
| 51 | +forstringinpuzzle_input.split(', '): |
| 52 | +ifstring[0]=='R': |
| 53 | +direction+=90 |
| 54 | +else: |
| 55 | +direction-=90 |
| 56 | +direction=direction%360 |
| 57 | + |
| 58 | +ifdirection==0: |
| 59 | +y+=int(string[1:]) |
| 60 | +elifdirection==180: |
| 61 | +y-=int(string[1:]) |
| 62 | +elifdirection==90: |
| 63 | +x-=int(string[1:]) |
| 64 | +elifdirection==270: |
| 65 | +x+=int(string[1:]) |
| 66 | +puzzle_actual_result=abs(x)+abs(y) |
| 67 | + |
| 68 | +else: |
| 69 | +forstringinpuzzle_input.split(', '): |
| 70 | +ifstring[0]=='R': |
| 71 | +direction+=90 |
| 72 | +else: |
| 73 | +direction-=90 |
| 74 | +direction=direction%360 |
| 75 | + |
| 76 | + (new_x,new_y)= (x,y) |
| 77 | + |
| 78 | +ifdirection==0: |
| 79 | +new_y+=int(string[1:]) |
| 80 | +elifdirection==180: |
| 81 | +new_y-=int(string[1:]) |
| 82 | +elifdirection==90: |
| 83 | +new_x+=int(string[1:]) |
| 84 | +elifdirection==270: |
| 85 | +new_x-=int(string[1:]) |
| 86 | + |
| 87 | +forx1inrange(min(x,new_x),max(x,new_x)+1): |
| 88 | +fory1inrange (min(y,new_y),max(y,new_y)+1): |
| 89 | +if (x1,y1)== (x,y): |
| 90 | +continue |
| 91 | +if (x1,y1)inlocations_visitedandpuzzle_actual_result=='Unknown': |
| 92 | +print (x1,y1) |
| 93 | +puzzle_actual_result=abs(x1)+abs(y1) |
| 94 | +break |
| 95 | +locations_visited.append((x1,y1)) |
| 96 | + (x,y)= (new_x,new_y) |
| 97 | + |
| 98 | + |
| 99 | +# -------------------------------- Outputs / results -------------------------------- # |
| 100 | + |
| 101 | +ifverbose_level>=3: |
| 102 | +print ('Input : '+puzzle_input) |
| 103 | +print ('Expected result : '+str(puzzle_expected_result)) |
| 104 | +print ('Actual result : '+str(puzzle_actual_result)) |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |