|
| 1 | + |
| 2 | + |
| 3 | +def solve_day6_part1(input_): |
| 4 | + res = 0 |
| 5 | + |
| 6 | + section = len(input_[0].split()) |
| 7 | + questions = [[] for _ in range(section)] |
| 8 | + |
| 9 | + for line in input_: |
| 10 | + for i, q in enumerate(line.split()): |
| 11 | + questions[i].append(q) |
| 12 | + |
| 13 | + for q in questions: |
| 14 | + match q[-1]: |
| 15 | + case '*': |
| 16 | + temp_res = 1 |
| 17 | + for num in q[:-1]: |
| 18 | + temp_res *= int(num) |
| 19 | + case '+': |
| 20 | + temp_res = 0 |
| 21 | + for num in q[:-1]: |
| 22 | + temp_res += int(num) |
| 23 | + |
| 24 | + res += temp_res |
| 25 | + |
| 26 | + return res |
| 27 | + |
| 28 | + |
| 29 | +def solve_day6_part2(input_): |
| 30 | + res = 0 |
| 31 | + |
| 32 | + section = len(input_[0].split()) |
| 33 | + questions = [[] for _ in range(section)] |
| 34 | + |
| 35 | + for line in input_: |
| 36 | + for i, q in enumerate(line.split()): |
| 37 | + questions[i].append(q) |
| 38 | + |
| 39 | + aligned_questions = [[] for _ in range(section)] |
| 40 | + start_index = 0 |
| 41 | + for i in range(section): |
| 42 | + max_length = len(sorted(questions[i], key=lambda x: len(x))[-1]) |
| 43 | + aligned_questions[i] = [ |
| 44 | + line[start_index:start_index+max_length] for line in input_] |
| 45 | + start_index += (max_length+1) |
| 46 | + |
| 47 | + for q in aligned_questions: |
| 48 | + match q[-1].strip(): |
| 49 | + case '*': |
| 50 | + start_index = 0 |
| 51 | + temp_res = 1 |
| 52 | + while True: |
| 53 | + is_last_digit_reached = False |
| 54 | + curr_num = "" |
| 55 | + for num in q[:-1]: |
| 56 | + if start_index <= len(num)-1: |
| 57 | + is_last_digit_reached = True |
| 58 | + curr_num += num[start_index] |
| 59 | + |
| 60 | + if is_last_digit_reached: |
| 61 | + temp_res *= int(curr_num) |
| 62 | + else: |
| 63 | + break |
| 64 | + |
| 65 | + start_index += 1 |
| 66 | + case '+': |
| 67 | + start_index = 0 |
| 68 | + temp_res = 0 |
| 69 | + while True: |
| 70 | + is_last_digit_reached = False |
| 71 | + curr_num = "" |
| 72 | + for num in q[:-1]: |
| 73 | + if start_index <= len(num)-1: |
| 74 | + is_last_digit_reached = True |
| 75 | + curr_num += num[start_index] |
| 76 | + |
| 77 | + if is_last_digit_reached: |
| 78 | + temp_res += int(curr_num) |
| 79 | + else: |
| 80 | + break |
| 81 | + |
| 82 | + start_index += 1 |
| 83 | + res += temp_res |
| 84 | + |
| 85 | + return res |
0 commit comments