mirror of
https://github.com/rjNemo/advent_of_code_2022
synced 2026-06-06 02:26:41 +00:00
day3 part 2
This commit is contained in:
parent
b8d574b907
commit
62a1897a35
3 changed files with 71 additions and 6 deletions
|
|
@ -52,7 +52,7 @@ fourth Elf).
|
|||
|
||||
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
|
||||
|
||||
## --- Part Two ---
|
||||
## Part Two
|
||||
|
||||
By the time you calculate the answer to the Elves' question, they've already realized that the Elf carrying the most
|
||||
Calories of food might eventually run out of snacks.
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ Each rucksack has two large **compartments**. All items of a given type are mean
|
|||
compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.
|
||||
|
||||
The Elves have made a list of all the items currently in each rucksack (your puzzle input), but they need your help
|
||||
finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, `a` and `A` refer to
|
||||
finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, `a` and `A` refer
|
||||
to
|
||||
different types of items).
|
||||
|
||||
The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same
|
||||
|
|
@ -46,3 +47,48 @@ L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is `157`.
|
|||
|
||||
Find the item type that appears in both compartments of each rucksack. **What is the sum of the priorities of those item
|
||||
types?**
|
||||
|
||||
## Part Two
|
||||
|
||||
As you finish identifying the misplaced items, the Elves come to you with another issue.
|
||||
|
||||
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For
|
||||
efficiency, within each group of three Elves, the badge is the **only item type carried by all three Elves**. That is,
|
||||
if a
|
||||
group's badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of
|
||||
the Elves will be carrying any other item type.
|
||||
|
||||
The problem is that someone forgot to put this year's updated authenticity sticker on the badges. All the badges need
|
||||
to be pulled out of the rucksacks so the new authenticity stickers can be attached.
|
||||
|
||||
Additionally, nobody wrote down which item type corresponds to each group's badges. The only way to tell which item type
|
||||
is the right one is by finding the one item type that is **common between all three Elves** in each group.
|
||||
|
||||
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item
|
||||
type. So, in the above example, the first group's rucksacks are the first three lines:
|
||||
|
||||
```
|
||||
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||
PmmdzqPrVvPwwTWBwg
|
||||
```
|
||||
|
||||
And the second group's rucksacks are the next three lines:
|
||||
|
||||
```
|
||||
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||
ttgJtRGJQctTZtZT
|
||||
CrZsJsPPZsGzwwsLwLmpwMDw
|
||||
```
|
||||
|
||||
In the first group, the only item type that appears in all three rucksacks is lowercase `r`; this must be their badges.
|
||||
In
|
||||
the second group, their badge item type must be `Z`.
|
||||
|
||||
Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (`r`) for
|
||||
the
|
||||
first group and 52 (`Z`) for the second group. The sum of these is **70**.
|
||||
|
||||
Find the item type that corresponds to the badges of each three-Elf group.\
|
||||
**What is the sum of the priorities of those
|
||||
item types?**
|
||||
19
day3/main.py
19
day3/main.py
|
|
@ -29,7 +29,26 @@ def read_data() -> list[str]:
|
|||
return [line.strip() for line in f.readlines()]
|
||||
|
||||
|
||||
def priority_shared_items_sum_2(data: list[str]) -> int:
|
||||
badges = []
|
||||
i = 0
|
||||
while i < len(data):
|
||||
group = [set(line) for line in data[i: i + 3]]
|
||||
badge = find_intersection(group)
|
||||
badges.append(ascii_letters.index(badge) + 1)
|
||||
i += 3
|
||||
return sum(badges)
|
||||
|
||||
|
||||
def find_intersection(group: list[set[str]]) -> str:
|
||||
return group[0].intersection(group[1]).intersection(group[2]).pop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
dataset = read_data()
|
||||
|
||||
res = priority_shared_items_sum_1(dataset)
|
||||
assert res == 157, f"{res} is not the right value"
|
||||
|
||||
res = priority_shared_items_sum_2(dataset)
|
||||
assert res == 70, f"{res} is not the right value"
|
||||
|
|
|
|||
Loading…
Reference in a new issue