feat: part 2 of day 3 complete
This commit is contained in:
35
day3/main.py
35
day3/main.py
@@ -1,3 +1,6 @@
|
||||
from functools import reduce
|
||||
import numpy
|
||||
|
||||
rucksacks = []
|
||||
|
||||
input_file = open("input.txt", "r")
|
||||
@@ -15,23 +18,39 @@ for line in lines:
|
||||
rucksacks[-1].append(first_compartment)
|
||||
rucksacks[-1].append(second_compartment)
|
||||
|
||||
|
||||
# part 1
|
||||
|
||||
|
||||
def get_item_in_common(first_compartment, second_compartment):
|
||||
item = [value for value in first_compartment if value in second_compartment][0]
|
||||
return item
|
||||
def get_items_in_common(items_one, items_two):
|
||||
items = [value for value in items_one if value in items_two]
|
||||
return items
|
||||
|
||||
|
||||
letters = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
total_priority = 0
|
||||
total_priority_part_one = 0
|
||||
for rucksack in rucksacks:
|
||||
first_compartment = rucksack[0]
|
||||
second_compartment = rucksack[1]
|
||||
|
||||
in_common = get_item_in_common(first_compartment, second_compartment)
|
||||
total_priority += letters.index(in_common) + 1
|
||||
in_common = get_items_in_common(first_compartment, second_compartment)[0]
|
||||
total_priority_part_one += letters.index(in_common) + 1
|
||||
|
||||
print("Part 1: " + str(total_priority))
|
||||
print("Part 1: " + str(total_priority_part_one))
|
||||
|
||||
# part 2
|
||||
|
||||
rucksack_groups = [[y[0]+y[1] for y in rucksacks][x:x+3]
|
||||
for x in range(0, len(rucksacks), 3)]
|
||||
|
||||
total_priority_part_two = 0
|
||||
for rucksack_group in rucksack_groups:
|
||||
rucksack_one = rucksack_group[0]
|
||||
rucksack_two = rucksack_group[1]
|
||||
rucksack_three = rucksack_group[2]
|
||||
|
||||
in_common = reduce(numpy.intersect1d,
|
||||
(rucksack_one, rucksack_two, rucksack_three))[0]
|
||||
total_priority_part_two += letters.index(in_common) + 1
|
||||
|
||||
print("Part 2: " + str(total_priority_part_two))
|
||||
|
||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
autopep8==2.0.0
|
||||
numpy==1.23.5
|
||||
pycodestyle==2.10.0
|
||||
tomli==2.0.1
|
||||
Reference in New Issue
Block a user