|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools |
| 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":"""FBFBBFFRLR |
| 38 | +BFFFBBFRRR |
| 39 | +FFFBBBFRRR |
| 40 | +BBFFBBFRLL""", |
| 41 | +"expected": ["357, 567, 119, 820","Unknown"], |
| 42 | +} |
| 43 | + |
| 44 | +test="real" |
| 45 | +input_file=os.path.join( |
| 46 | +os.path.dirname(__file__), |
| 47 | +"Inputs", |
| 48 | +os.path.basename(__file__).replace(".py",".txt"), |
| 49 | +) |
| 50 | +test_data[test]= { |
| 51 | +"input":open(input_file,"r+").read(), |
| 52 | +"expected": ["878","504"], |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +# -------------------------------- Control program execution ------------------------- # |
| 57 | + |
| 58 | +case_to_test="real" |
| 59 | +part_to_test=2 |
| 60 | + |
| 61 | +# -------------------------------- Initialize some variables ------------------------- # |
| 62 | + |
| 63 | +puzzle_input=test_data[case_to_test]["input"] |
| 64 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 65 | +puzzle_actual_result="Unknown" |
| 66 | + |
| 67 | + |
| 68 | +# -------------------------------- Actual code execution ----------------------------- # |
| 69 | + |
| 70 | +ifpart_to_test==2: |
| 71 | +seat_list=list(range(127*8+7+1)) |
| 72 | + |
| 73 | +max_seat_id=0 |
| 74 | +forseatinpuzzle_input.split("\n"): |
| 75 | +row=0 |
| 76 | +column=0 |
| 77 | +row_power=6 |
| 78 | +col_power=2 |
| 79 | +forletterinseat: |
| 80 | +ifletter=="F": |
| 81 | +row_power=row_power-1 |
| 82 | +elifletter=="B": |
| 83 | +row=row+2**row_power |
| 84 | +row_power=row_power-1 |
| 85 | + |
| 86 | +elifletter=="L": |
| 87 | +col_power=col_power-1 |
| 88 | +elifletter=="R": |
| 89 | +column=column+2**col_power |
| 90 | +col_power=col_power-1 |
| 91 | + |
| 92 | +seat_id=row*8+column |
| 93 | +max_seat_id=max(seat_id,max_seat_id) |
| 94 | + |
| 95 | +ifpart_to_test==2: |
| 96 | +seat_list.remove(seat_id) |
| 97 | + |
| 98 | +ifpart_to_test==1: |
| 99 | +puzzle_actual_result=max_seat_id |
| 100 | +else: |
| 101 | +seat_list= [xforxinseat_listifx<=max_seat_id] |
| 102 | + |
| 103 | +puzzle_actual_result=max(seat_list) |
| 104 | + |
| 105 | + |
| 106 | +# -------------------------------- Outputs / results --------------------------------- # |
| 107 | + |
| 108 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 109 | +print("Expected result : "+str(puzzle_expected_result)) |
| 110 | +print("Actual result : "+str(puzzle_actual_result)) |