diff --git a/2015/1/d1.md b/2015/1/d1.md new file mode 100644 index 0000000..0a0b5c2 --- /dev/null +++ b/2015/1/d1.md @@ -0,0 +1,28 @@ +# Day1: Not Quite Lisp +## Part 1 +Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time. + +An opening parenthesis, `(`, means he should go up one floor, and a closing parenthesis, `)`, means he should go down one floor. + +The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors. + +For example: + +- `(())` and `()()` both result in floor 0. +- `(((` and `(()(()(` both result in floor 3. +- `))(((((` also results in floor 3. +- `())`and `))(` both result in floor -1 (the first basement level). +- `)))` and `)())())` both result in floor -3. + +To what floor do the instructions take Santa? + +## Part 2 + +Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on. + +For example: + +- `)` causes him to enter the basement at character position 1. +- `()())` causes him to enter the basement at character position 5. + +What is the position of the character that causes Santa to first enter the basement? diff --git a/2015/1/d1.py b/2015/1/d1.py index c467939..afcf32e 100644 --- a/2015/1/d1.py +++ b/2015/1/d1.py @@ -1,19 +1,5 @@ """ -Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time. - -An opening parenthesis, `(`, means he should go up one floor, and a closing parenthesis, `)`, means he should go down one floor. - -The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors. - -For example: - - `(())` and `()()` both result in floor 0. - `(((` and `(()(()(` both result in floor 3. - `))(((((` also results in floor 3. - `())`and `))(` both result in floor -1 (the first basement level). - `)))` and `)())())` both result in floor -3. - -To what floor do the instructions take Santa? +sum up coded instruction """ with open('directions.txt') as filein: @@ -25,18 +11,11 @@ translation = { } print(sum([translation.get(c,0) for c in text])) - + """ - Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on. - -For example: - - `)` causes him to enter the basement at character position 1. - `()())` causes him to enter the basement at character position 5. - -What is the position of the character that causes Santa to first enter the basement? +stop at given value """ -s = 0 +s = 0 for i,c in enumerate(text, start=1): s += translation[c] if s<0: diff --git a/2015/2/day_2.md b/2015/2/day_2.md new file mode 100644 index 0000000..a7dec61 --- /dev/null +++ b/2015/2/day_2.md @@ -0,0 +1,25 @@ +# Day 2: I Was Told There Would Be No Math + +The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length `l`, width `w`, and height `h`) of each present, and only want to order exactly as much as they need. + +Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is `2*l*w + 2*w*h + 2*h*l`. The elves also need a little extra paper for each present: the area of the smallest side. + +For example: + +- A present with dimensions `2x3x4` requires `2*6 + 2*12 + 2*8 = 52` square feet of wrapping paper plus `6` square feet of slack, for a total of *`58`* square feet. +- A present with dimensions `1x1x10` requires `2*1 + 2*10 + 2*10 = 42`square feet of wrapping paper plus `1` square foot of slack, for a total of *`43`* square feet. + +All numbers in the elves' list are in feet. How many `total square feet` of wrapping paper should they order? + +## Part 2: + +The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact. + +The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don't ask how they tie the bow, though; they'll never tell. + +For example: + +- A present with dimensions `2x3x4` requires `2+2+3+3 = 10` feet of ribbon to wrap the present plus `2*3*4 = 24` feet of ribbon for the bow, for a total of *`34`* feet. +- A present with dimensions `1x1x10` requires `1+1+1+1 = 4` feet of ribbon to wrap the present plus `1*1*10 = 10` feet of ribbon for the bow, for a total of *`14`* feet. + +How many total `feet of ribbon` should they order? \ No newline at end of file diff --git a/2015/2/day_2.py b/2015/2/day_2.py index 424dac4..e8fefc4 100644 --- a/2015/2/day_2.py +++ b/2015/2/day_2.py @@ -1,16 +1,5 @@ """ -# Day 2: I Was Told There Would Be No Math - -The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need. - -Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side. - -For example: - -- A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet. -- A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet. - -All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order? +stringcomprehension: get cuboid dimension and sum all surface aera plus again the smallest sides """ def surface(dimensions:str) -> int: @@ -25,22 +14,13 @@ with open('dimensions.txt') as filein: print(sum([surface(line) for line in lines])) """ -The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact. - -The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don't ask how they tie the bow, though; they'll never tell. - -For example: - -- A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of ribbon to wrap the present plus 2*3*4 = 24 feet of ribbon for the bow, for a total of 34 feet. -- A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of ribbon to wrap the present plus 1*1*10 = 10 feet of ribbon for the bow, for a total of 14 feet. - -How many total feet of ribbon should they order? +now get the smallest circumference and add the volume """ import numpy as np def ribbon(dimensions:str): l,w,h = [int(i) for i in dimensions.strip().split('x')] perimiter = min(np.array([l+w,w+h,h+l])*2) - + return perimiter + l*w*h print(sum([ribbon(line) for line in lines])) diff --git a/2022/1/d1-1.py b/2022/1/d1-1.py index 3a34479..ff720d9 100644 --- a/2022/1/d1-1.py +++ b/2022/1/d1-1.py @@ -1,39 +1,5 @@ -"""# Day 1: Calorie Counting - -The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of **Calories** each Elf is carrying (your puzzle input). - -The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line. - -For example, suppose the Elves finish writing their items' Calories and end up with the following list: - -``` -1000 -2000 -3000 - -4000 - -5000 -6000 - -7000 -8000 -9000 - -10000 -``` - -This list represents the Calories of the food carried by five Elves: - -- The first Elf is carrying food with `1000`, `2000`, and `3000` Calories, a total of `6000` Calories. -- The second Elf is carrying one food item with `4000` Calories. -- The third Elf is carrying food with `5000` and `6000` Calories, a total of `11000` Calories. -- The fourth Elf is carrying food with `7000`, `8000`, and `9000` Calories, a total of `24000` Calories. -- The fifth Elf is carrying one food item with `10000` Calories. - -In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories are being carried by the Elf carrying the *most* Calories. In the example above, this is `24000` (carried by the fourth Elf). - -Find the Elf carrying the most Calories. **How many total Calories is that Elf carrying?** +""" +sum list items seperated bu empty lines and find the maximum """ import numpy as np @@ -46,13 +12,7 @@ cals = np.array([sum([int(c) for c in elf.split()]) for elf in elves]) print(np.max(cals)) """ -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. - -To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups. - -In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 Calories), then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000. - -Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total? +now sort and fint the top 3 """ top3 = (np.sort(cals)[::-1][:3]) diff --git a/2022/1/d1.md b/2022/1/d1.md new file mode 100644 index 0000000..1d2c2a4 --- /dev/null +++ b/2022/1/d1.md @@ -0,0 +1,46 @@ +# Day 1: Calorie Counting +## Part 1 +The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of **Calories** each Elf is carrying (your puzzle input). + +The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line. + +For example, suppose the Elves finish writing their items' Calories and end up with the following list: + +``` +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 +``` + +This list represents the Calories of the food carried by five Elves: + +- The first Elf is carrying food with `1000`, `2000`, and `3000` Calories, a total of `6000` Calories. +- The second Elf is carrying one food item with `4000` Calories. +- The third Elf is carrying food with `5000` and `6000` Calories, a total of `11000` Calories. +- The fourth Elf is carrying food with `7000`, `8000`, and `9000` Calories, a total of `24000` Calories. +- The fifth Elf is carrying one food item with `10000` Calories. + +In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories are being carried by the Elf carrying the *most* Calories. In the example above, this is `24000` (carried by the fourth Elf). + +Find the Elf carrying the most Calories. **How many total Calories is that Elf carrying?** + +## Part 2 + +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. + +To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups. + +In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 Calories), then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000. + +Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total? \ No newline at end of file diff --git a/2022/2/d2.md b/2022/2/d2.md new file mode 100644 index 0000000..4e747f2 --- /dev/null +++ b/2022/2/d2.md @@ -0,0 +1,49 @@ +# Day 2: Rock Paper Scissors +## Part 1 +The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant **Rock Paper Scissors** tournament is already in progress. + +Rock Paper Scissors is a game between two players. Each game contains many rounds; in each round, the players each simultaneously choose one of Rock, Paper, or Scissors using a hand shape. Then, a winner for that round is selected: Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock. If both players choose the same shape, the round instead ends in a draw. + +Appreciative of your help yesterday, one Elf gives you an **encrypted strategy guide* (your puzzle input) that they say will be sure to help you win. "The first column is what your opponent is going to play: `A` for Rock, `B` for Paper, and `C` for Scissors. The second column--" Suddenly, the Elf is called away to help with someone's tent. + +The second column, you reason, must be what you should play in response: `X` for Rock, `Y` for Paper, and `Z` for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen. + +The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round. The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won). + +Since you can't be sure if the Elf is trying to help you or trick you, you should calculate the score you would get if you were to follow the strategy guide. + +For example, suppose you were given the following strategy guide: + +``` +A Y +B X +C Z +``` + +This strategy guide predicts and recommends the following: + +- In the first round, your opponent will choose Rock (`A`), and you should choose Paper (`Y`). This ends in a win for you with a score of *8* (2 because you chose Paper + 6 because you won). +- In the second round, your opponent will choose Paper (`B`), and you should choose Rock (`X`). This ends in a loss for you with a score of *1* (1 + 0). +- The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = *6*. + +In this example, if you were to follow the strategy guide, you would get a total score of *15* (8 + 1 + 6). + +What would your total score be if everything goes exactly according to your strategy guide? + +----- +x : Rock -> 1 +Y : Paper -> 2 +Z : Scissors -> 3 + + ## Part 2 + The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: `X` means you need to lose, `Y` means you need to end the round in a draw, and `Z` means you need to win. Good luck!" + +The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this: + +- In the first round, your opponent will choose Rock (`A`), and you need the round to end in a draw (`Y`), so you also choose Rock. This gives you a score of 1 + 3 = *4*. +- In the second round, your opponent will choose Paper (`B`), and you choose Rock so you lose (`X`) with a score of 1 + 0 = *1*. +- In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = *7*. + +Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of *12*. + +Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide? diff --git a/2022/2/d2.py b/2022/2/d2.py index 4a56473..19b6210 100644 --- a/2022/2/d2.py +++ b/2022/2/d2.py @@ -1,35 +1,9 @@ """ -The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant **Rock Paper Scissors** tournament is already in progress. - -Rock Paper Scissors is a game between two players. Each game contains many rounds; in each round, the players each simultaneously choose one of Rock, Paper, or Scissors using a hand shape. Then, a winner for that round is selected: Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock. If both players choose the same shape, the round instead ends in a draw. - -Appreciative of your help yesterday, one Elf gives you an **encrypted strategy guide* (your puzzle input) that they say will be sure to help you win. "The first column is what your opponent is going to play: `A` for Rock, `B` for Paper, and `C` for Scissors. The second column--" Suddenly, the Elf is called away to help with someone's tent. - -The second column, you reason, must be what you should play in response: `X` for Rock, `Y` for Paper, and `Z` for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen. - -The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round. The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won). - -Since you can't be sure if the Elf is trying to help you or trick you, you should calculate the score you would get if you were to follow the strategy guide. - -For example, suppose you were given the following strategy guide: - -``` -A Y -B X -C Z -``` - -This strategy guide predicts and recommends the following: - - In the first round, your opponent will choose Rock (`A`), and you should choose Paper (`Y`). This ends in a win for you with a score of *8* (2 because you chose Paper + 6 because you won). - In the second round, your opponent will choose Paper (`B`), and you should choose Rock (`X`). This ends in a loss for you with a score of *1* (1 + 0). - The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = *6*. - -In this example, if you were to follow the strategy guide, you would get a total score of *15* (8 + 1 + 6). - -What would your total score be if everything goes exactly according to your strategy guide? - +calculate the sum of all games for a coded game plan of rock-paper-scissors ----- +A : Rock -> 1 +B : Paper -> 2 +C : Scissors -> 3 x : Rock -> 1 Y : Paper -> 2 Z : Scissors -> 3 @@ -58,17 +32,10 @@ score = sum([game_points(*game) for game in games]) print(score) """ -The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: `X` means you need to lose, `Y` means you need to end the round in a draw, and `Z` means you need to win. Good luck!" - -The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this: - - In the first round, your opponent will choose Rock (`A`), and you need the round to end in a draw (`Y`), so you also choose Rock. This gives you a score of 1 + 3 = *4*. - In the second round, your opponent will choose Paper (`B`), and you choose Rock so you lose (`X`) with a score of 1 + 0 = *1*. - In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = *7*. - -Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of *12*. - -Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide? +new cypher: +x : lose +Y : draw +Z : win """ shape_points = { diff --git a/2022/3/3.md b/2022/3/3.md new file mode 100644 index 0000000..04c52a6 --- /dev/null +++ b/2022/3/3.md @@ -0,0 +1,67 @@ +# Day 3 +## Part 1 +One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged. + +Each rucksack has two large *compartments*. All items of a given type are meant to go into exactly one of the two 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 of 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 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 number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment. + +For example, suppose you have the following list of contents from six rucksacks: + +``` +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw +``` +- The first rucksack contains the items `vJrwpWtwJgWrhcsFMMfFFhFp`, which means its first compartment contains the items `vJrwpWtwJgWr`, while the second compartment contains the items `hcsFMMfFFhFp`. The only item type that appears in both compartments is lowercase *`p`*. +- The second rucksack's compartments contain jqHRNqRjqzjGD`LGL and `rsFMfFZSrLrFZsSL`. The only item type that appears in both compartments is uppercase *`L`*. +- The third rucksack's compartments contain `PmmdzqPrV` and `vPwwTWBwg`; the only common item type is uppercase *`P`*. +- The fourth rucksack's compartments only share item type *`v`*. +- The fifth rucksack's compartments only share item type *`t`*. +- The sixth rucksack's compartments only share item type *`s`*. + +To help prioritize item rearrangement, every item type can be converted to a *priority*: + + Lowercase item types `a` through `z` have priorities 1 through 26. + Uppercase item types `A` through `Z` have priorities 53 through 52. + +In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (`p`), 38 (`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 2 + +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 of 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? diff --git a/2022/3/3.py b/2022/3/3.py index a8b0b91..248b144 100644 --- a/2022/3/3.py +++ b/2022/3/3.py @@ -1,37 +1,5 @@ """ -One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged. - -Each rucksack has two large *compartments*. All items of a given type are meant to go into exactly one of the two 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 of 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 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 number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment. - -For example, suppose you have the following list of contents from six rucksacks: - -``` -vJrwpWtwJgWrhcsFMMfFFhFp -jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL -PmmdzqPrVvPwwTWBwg -wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn -ttgJtRGJQctTZtZT -CrZsJsPPZsGzwwsLwLmpwMDw -``` -- The first rucksack contains the items `vJrwpWtwJgWrhcsFMMfFFhFp`, which means its first compartment contains the items `vJrwpWtwJgWr`, while the second compartment contains the items `hcsFMMfFFhFp`. The only item type that appears in both compartments is lowercase *`p`*. -- The second rucksack's compartments contain jqHRNqRjqzjGD`LGL and `rsFMfFZSrLrFZsSL`. The only item type that appears in both compartments is uppercase *`L`*. -- The third rucksack's compartments contain `PmmdzqPrV` and `vPwwTWBwg`; the only common item type is uppercase *`P`*. -- The fourth rucksack's compartments only share item type *`v`*. -- The fifth rucksack's compartments only share item type *`t`*. -- The sixth rucksack's compartments only share item type *`s`*. - -To help prioritize item rearrangement, every item type can be converted to a *priority*: - - Lowercase item types `a` through `z` have priorities 1 through 26. - Uppercase item types `A` through `Z` have priorities 53 through 52. - -In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (`p`), 38 (`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*? +find an prioritize common items in two halves of strings """ import numpy as np from string import ascii_letters @@ -64,35 +32,7 @@ rucksacks = [compartments(line) for line in lines] print(sum([priority[find_common(*rucksack)] for rucksack in rucksacks])) """ -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 of 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? +find an prioritize common items in three stringlines """ groups = np.array(lines).reshape(-1,3) badges = [priority[find_common(*group)] for group in groups]