|
| 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":"""abc |
| 38 | +
|
| 39 | +a |
| 40 | +b |
| 41 | +c |
| 42 | +
|
| 43 | +ab |
| 44 | +ac |
| 45 | +
|
| 46 | +a |
| 47 | +a |
| 48 | +a |
| 49 | +a |
| 50 | +
|
| 51 | +b""", |
| 52 | +"expected": ["11","6"], |
| 53 | +} |
| 54 | + |
| 55 | +test="real" |
| 56 | +input_file=os.path.join( |
| 57 | +os.path.dirname(__file__), |
| 58 | +"Inputs", |
| 59 | +os.path.basename(__file__).replace(".py",".txt"), |
| 60 | +) |
| 61 | +test_data[test]= { |
| 62 | +"input":open(input_file,"r+").read(), |
| 63 | +"expected": ["6782","3596"], |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +# -------------------------------- Control program execution ------------------------- # |
| 68 | + |
| 69 | +case_to_test=1 |
| 70 | +part_to_test=2 |
| 71 | + |
| 72 | +# -------------------------------- Initialize some variables ------------------------- # |
| 73 | + |
| 74 | +puzzle_input=test_data[case_to_test]["input"] |
| 75 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 76 | +puzzle_actual_result="Unknown" |
| 77 | + |
| 78 | + |
| 79 | +# -------------------------------- Actual code execution ----------------------------- # |
| 80 | + |
| 81 | +ifpart_to_test==1: |
| 82 | +total_score=0 |
| 83 | +forgroupinpuzzle_input.split("\n\n"): |
| 84 | +group_size=len(group.split("\n")) |
| 85 | +answers=Counter(group.replace("\n","")) |
| 86 | +nb_common=len(answers) |
| 87 | +total_score=total_score+nb_common |
| 88 | + |
| 89 | +puzzle_actual_result=total_score |
| 90 | + |
| 91 | + |
| 92 | +else: |
| 93 | +total_score=0 |
| 94 | +forgroupinpuzzle_input.split("\n\n"): |
| 95 | +group_size=len(group.split("\n")) |
| 96 | +answers=Counter(group.replace("\n","")) |
| 97 | +nb_common=len([xforxinanswersifanswers[x]==group_size]) |
| 98 | +total_score=total_score+nb_common |
| 99 | + |
| 100 | +puzzle_actual_result=total_score |
| 101 | + |
| 102 | + |
| 103 | +# -------------------------------- Outputs / results --------------------------------- # |
| 104 | + |
| 105 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 106 | +print("Expected result : "+str(puzzle_expected_result)) |
| 107 | +print("Actual result : "+str(puzzle_actual_result)) |