Skip to content

Commit

Permalink
2022_13: python solution(part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 29, 2022
1 parent e1dd2f6 commit d169d27
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
target
__pycache__
20 changes: 0 additions & 20 deletions 2022/day_13/python/main.py → 2022/day_13/python/compare_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,3 @@ def compare_pair(first: list, second: list) -> int:
return -1

return 0


pairs: list[tuple[list, list]] = []
with open("./2022/day_13/input.txt", "r") as file:
while True:
first = eval(next(file).strip())
second = eval(next(file).strip())
pairs.append((first, second))

last = next(file, None)
if last == None:
break

indicies = []
for i, pair in enumerate(pairs):
first, second = pair
if compare_pair(first, second) == 1:
indicies.append(i + 1)

print(sum(indicies))
20 changes: 20 additions & 0 deletions 2022/day_13/python/part_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from compare_pair import compare_pair

pairs: list[tuple[list, list]] = []
with open("./2022/day_13/input.txt", "r") as file:
while True:
first = eval(next(file).strip())
second = eval(next(file).strip())
pairs.append((first, second))

last = next(file, None)
if last == None:
break

indicies = []
for i, pair in enumerate(pairs):
first, second = pair
if compare_pair(first, second) == 1:
indicies.append(i + 1)

print(sum(indicies))
24 changes: 24 additions & 0 deletions 2022/day_13/python/part_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from compare_pair import compare_pair
from copy import deepcopy

data: list[list] = []
with open("./2022/day_13/input.txt", "r") as file:
for line in file:
line = line.strip()
if line != "":
data.append(eval(line))

divider_packet_1 = [[2]]
divider_packet_2 = [[6]]

smaller_of_1 = 1
smaller_of_2 = 2

for line in data:
if compare_pair(deepcopy(line), deepcopy(divider_packet_1)) == 1:
smaller_of_1 += 1
if compare_pair(deepcopy(line), deepcopy(divider_packet_2)) == 1:
smaller_of_2 += 1

print(smaller_of_1, smaller_of_2)
print(smaller_of_1 * smaller_of_2)

0 comments on commit d169d27

Please sign in to comment.