Skip to content

Commit

Permalink
[2022][day 13] adding solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IAjimi committed Dec 13, 2022
1 parent b9b9d76 commit 08be37a
Show file tree
Hide file tree
Showing 3 changed files with 546 additions and 0 deletions.
96 changes: 96 additions & 0 deletions 2022/AOC13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from typing import List, Tuple, Set

from _utils import read_input, timer, Solution, product


def process_input(filename: str):
_input = read_input(filename)
_input = [eval(l) for l in _input if l != ""]
return _input


def compare_pairs(pair1, pair2) -> bool:
if type(pair1) == int and type(pair2) == int:
if pair1 < pair2:
return True
elif pair1 > pair2:
return False
else:
return None

i = 0
while i < len(pair1) and i < len(pair2):
p1, p2 = pair1[i], pair2[i]
if type(p1) == int and type(p2) == int:
if p1 < p2:
return True
elif p1 > p2:
return False
else:
comp = None
elif type(p1) == list and type(p2) == int:
comp = compare_pairs(p1, [p2])
elif type(p1) == int and type(p2) == list:
comp = compare_pairs([p1], p2)
else:
comp = compare_pairs(p1, p2)

if comp is not None:
return comp
else:
i += 1

if len(pair1) < len(pair2):
return True
elif len(pair1) > len(pair2):
return False
else:
return None


def part1(_input) -> int:
_input = [compare_pairs(_input[i], _input[i + 1]) for i in range(0, len(_input), 2)]
return sum([1 + i if pair else 0 for i, pair in enumerate(_input)])


class InsertionSort:
def insert(self, A: List[int], i: int) -> List[int]:
j = i - 1

while j >= 0 and compare_pairs(A[i], A[j]):
A = self.swap(A, i, j)
i -= 1
j -= 1

return A

def swap(self, A: List[int], i: int, j: int) -> List[int]:
A[i], A[j] = A[j], A[i]
return A

def sort(self, A: List[int]) -> List[int]:
for i in range(len(A)):
A = self.insert(A, i)

return A


def part2(_input) -> int:
_input.append([[2]])
_input.append([[6]])
_input = InsertionSort().sort(_input)
return product([i + 1 for i, v in enumerate(_input) if v in ([[2]], [[6]])])


@timer
def main(filename: str) -> Solution:
_input = process_input(filename)
part_1_solution = part1(_input)
part_2_solution = part2(_input)
return part_1_solution, part_2_solution


if __name__ == "__main__":
part_1_solution, part_2_solution = main("aoc13.txt")
print(f"PART 1: {part_1_solution}") # 5185
print(f"PART 2: {part_2_solution}") # 23751
1 change: 1 addition & 0 deletions 2022/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ This repository contains Python solutions to the 2022 edition of [Advent of Code
| [10](https://adventofcode.com/2022/day/10) | **Cathode-Ray Tube** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC10.py) |
| [11](https://adventofcode.com/2022/day/11) | **Monkey in the Middle** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC11.py) |
| [12](https://adventofcode.com/2022/day/12) | **Hill Climbing Algorithm** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC12.py) |
| [13](https://adventofcode.com/2022/day/13) | **Distress Signal** | [:star::star:](https://github.com/IAjimi/AdventOfCode/blob/master/2022/AOC13.py) |
Loading

0 comments on commit 08be37a

Please sign in to comment.