Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2022 Day 1
- Advent of Code 2022 Day 2
- Advent of Code 2022 Day 3
- Advent of Code 2022 Day 4
- Advent of Code 2022 Day 5
- Advent of Code 2022 Day 6
- Advent of Code 2022 Day 7
- Advent of Code 2022 Day 8
- Advent of Code 2022 Day 9
- Advent of Code 2022 Day 10
- Advent of Code 2022 Day 11
- Advent of Code 2022 Day 12
- Advent of Code 2022 Day 13
- Advent of Code 2022 Day 14
- Advent of Code 2022 Day 15
- Advent of Code 2022 Day 16
- Advent of Code 2022 Day 17
- Advent of Code 2022 Day 18
- Advent of Code 2022 Day 19
- Advent of Code 2022 Day 20
- Advent of Code 2022 Day 21
- Advent of Code 2022 Day 22
- Advent of Code 2022 Day 23
- Advent of Code 2022 Day 24
- Advent of Code 2022 Day 25
-
Older post
-
Newer post
Advent of Code 2022 Day 1
Day 1: Calorie Counting
https://adventofcode.com/2022/day/1
We’re on an expedition with the elves. The input file represents the amount of calories each elf carries with them. Each elf has several items. The inventories of each elf is seperated by two line breaks.
Part 1
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
For each elf we want to calculate the sum of the items they are carrying. Then, find the largest of those sums.
I did today with one iterator chain.
Get the input string. Split it on double newlines. Each iterator element is now a string that represents the inventory of a single elf.
For every element: split on a single linebreak. Turn that line into a number. Take the sum of every number.
Now, every element is the sum a single elf is carrying. Find the maximum of those sums.
Part 2
Part 2 is very similar.
Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?
Get the input string. Split it on double newlines. Each iterator element is now a string that represents the inventory of a single elf.
For every element: split on a single linebreak. Turn that line into a number. Take the sum of every number.
Now, every element is the sum a single elf is carrying. Sort those sums. Take the 3 largest sums, and sum those.
To sort the iterator directly, I used the sorted()
method from the itertools
crate.
I could have also called .collect()
on the iterator of all sums, and store that result in a variable with type Vec<u32>
.
Then sort that resulting vector with .sort()
.
This sorts in ascending order, and I want the sum of the 3 last ones. So I reverse the iterator. Take 3 items of that iterator. Sum those.