|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools,math |
| 3 | +fromcollectionsimportCounter,deque,defaultdict |
| 4 | + |
| 5 | +fromcompassimport* |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +deflmap(func,*iterables): |
| 10 | +returnlist(map(func,*iterables)) |
| 11 | + |
| 12 | + |
| 13 | +defints(s:str): |
| 14 | +returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +defpositive_ints(s:str): |
| 18 | +returnlmap(int,re.findall(r"\d+",s))# thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +deffloats(s:str): |
| 22 | +returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s)) |
| 23 | + |
| 24 | + |
| 25 | +defpositive_floats(s:str): |
| 26 | +returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s)) |
| 27 | + |
| 28 | + |
| 29 | +defwords(s:str): |
| 30 | +returnre.findall(r"[a-zA-Z]+",s) |
| 31 | + |
| 32 | + |
| 33 | +test_data= {} |
| 34 | + |
| 35 | +test=1 |
| 36 | +test_data[test]= { |
| 37 | +"input":"""939 |
| 38 | +7,13,x,x,59,x,31,19""", |
| 39 | +"expected": ["295","1068781"], |
| 40 | +} |
| 41 | + |
| 42 | +test="real" |
| 43 | +input_file=os.path.join( |
| 44 | +os.path.dirname(__file__), |
| 45 | +"Inputs", |
| 46 | +os.path.basename(__file__).replace(".py",".txt"), |
| 47 | +) |
| 48 | +test_data[test]= { |
| 49 | +"input":open(input_file,"r+").read(), |
| 50 | +"expected": ["2382","906332393333683"], |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +# -------------------------------- Control program execution ------------------------- # |
| 55 | + |
| 56 | +case_to_test="real" |
| 57 | +part_to_test=2 |
| 58 | + |
| 59 | +# -------------------------------- Initialize some variables ------------------------- # |
| 60 | + |
| 61 | +puzzle_input=test_data[case_to_test]["input"] |
| 62 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 63 | +puzzle_actual_result="Unknown" |
| 64 | + |
| 65 | + |
| 66 | +# -------------------------------- Actual code execution ----------------------------- # |
| 67 | + |
| 68 | +ifpart_to_test==1: |
| 69 | +data=puzzle_input.split("\n") |
| 70 | +curr_time=int(data[0]) |
| 71 | +busses=ints(data[1]) |
| 72 | +next_time=curr_time*10 |
| 73 | + |
| 74 | +forbusinbusses: |
| 75 | +next_round=bus-curr_time%bus+curr_time |
| 76 | +print(next_round,bus,curr_time) |
| 77 | +ifnext_round<next_time: |
| 78 | +next_time=next_round |
| 79 | +next_bus=bus |
| 80 | + |
| 81 | +puzzle_actual_result= (next_time-curr_time)*next_bus |
| 82 | + |
| 83 | + |
| 84 | +else: |
| 85 | +data=puzzle_input.split("\n") |
| 86 | +busses=data[1].split(",") |
| 87 | +bus_offsets= {} |
| 88 | + |
| 89 | +i=0 |
| 90 | +forbusinbusses: |
| 91 | +ifbus=="x": |
| 92 | +pass |
| 93 | +else: |
| 94 | +bus_offsets[int(bus)]=i |
| 95 | +i+=1 |
| 96 | + |
| 97 | +timestamp=0 |
| 98 | + |
| 99 | +# I first solved this thanks to a diophantine equation solvers found on Internet |
| 100 | + |
| 101 | +# Then I looked at the solutions megathread to learn more |
| 102 | +# This is the proper algorithm that works in a feasible time |
| 103 | +# It's called the Chinese remainder theorem |
| 104 | +# See https://crypto.stanford.edu/pbc/notes/numbertheory/crt.html |
| 105 | +prod_modulos=math.prod(bus_offsets.keys()) |
| 106 | +forbus,offsetinbus_offsets.items(): |
| 107 | +timestamp+=-offset* (prod_modulos//bus)*pow(prod_modulos//bus,-1,bus) |
| 108 | +timestamp%=prod_modulos |
| 109 | + |
| 110 | +# The below algorithm is the brute-force version: very slow but should work |
| 111 | +# Since timestamp is calculated above, this won't do anything |
| 112 | +# To make it run, uncomment the below line |
| 113 | +# timestamp = 0 |
| 114 | + |
| 115 | +min_bus=min(bus_offsets.keys()) |
| 116 | +whileTrue: |
| 117 | +ifall([(timestamp+bus_offsets[bus])%bus==0forbusinbus_offsets]): |
| 118 | +puzzle_actual_result=timestamp |
| 119 | +break |
| 120 | +else: |
| 121 | +timestamp+=min_bus |
| 122 | + |
| 123 | + |
| 124 | +# -------------------------------- Outputs / results --------------------------------- # |
| 125 | + |
| 126 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 127 | +print("Expected result : "+str(puzzle_expected_result)) |
| 128 | +print("Actual result : "+str(puzzle_actual_result)) |
| 129 | +# Date created: 2020-12-13 06:25:25.641468 |
| 130 | +# Part 1: 2020-12-13 06:31:06 |
| 131 | +# Part 2: 2020-12-13 07:12:10 |