Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day20-infinite_elves_and_infinite_houses"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,48 @@
\--- Day 20: Infinite Elves and Infinite Houses ---
----------
To keep the Elves busy, Santa has them deliver some presents by hand, door-to-door. He sends them down a street with infinite houses numbered sequentially: `1`, `2`, `3`, `4`, `5`, and so on.
Each Elf is assigned a number, too, and delivers presents to houses based on that number:
* The first Elf (number `1`) delivers presents to every house: `1`, `2`, `3`, `4`, `5`, ....
* The second Elf (number `2`) delivers presents to every second house: `2`, `4`, `6`, `8`, `10`, ....
* Elf number `3` delivers presents to every third house: `3`, `6`, `9`, `12`, `15`, ....
There are infinitely many Elves, numbered starting with `1`. Each Elf delivers presents equal to *ten times* his or her number at each house.
So, the first nine houses on the street end up like this:
```
House 1 got 10 presents.
House 2 got 30 presents.
House 3 got 40 presents.
House 4 got 70 presents.
House 5 got 60 presents.
House 6 got 120 presents.
House 7 got 80 presents.
House 8 got 150 presents.
House 9 got 130 presents.
```
The first house gets `10` presents: it is visited only by Elf `1`, which delivers `1 * 10 = 10` presents. The fourth house gets `70` presents, because it is visited by Elves `1`, `2`, and `4`, for a total of `10 + 20 + 40 = 70` presents.
What is the *lowest house number* of the house to get at least as many presents as the number in your puzzle input?
Your puzzle answer was `665280`.
\--- Part Two ---
----------
The Elves decide they don't want to visit an infinite number of houses. Instead, each Elf will stop after delivering presents to `50` houses. To make up for it, they decide to deliver presents equal to *eleven times* their number at each house.
With these changes, what is the new *lowest house number* of the house to get at least as many presents as the number in your puzzle input?
Your puzzle answer was `705600`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to [admire your Advent calendar](/2015).
Your puzzle input was `29000000`.

View file

@ -0,0 +1,48 @@
pub fn run(input: usize) -> (usize, usize) {
let first = presents_count_for_house(input, 10, None);
let second = presents_count_for_house(input, 11, Some(50));
(first, second)
}
fn presents_count_for_house(number: usize, multiplier: usize, max: Option<usize> ) -> usize {
let max = max.unwrap_or(number/multiplier);
let size = number/multiplier+1;
let mut counts = vec![0; size];
(1..=size).for_each(|n| {
(n..=size.min(max*n)).step_by(n).for_each(|i| {
counts[i-1] += multiplier*n;
});
});
counts.iter().position(|i| *i >= number).unwrap_or(0) + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sample() {
let exptected = [
(9, (1, 1)),
(10, (1, 1)),
(11, (2, 1)),
(30, (2, 2)),
(40, (3, 3)),
(70, (4, 4)),
(60, (4, 4)),
(120, (6, 6)),
(80, (6, 6)),
(150, (8, 8)),
(130, (8, 6)),
];
for (input, output) in exptected {
assert_eq!(run(input), output);
}
}
#[test]
fn test_challenge() {
let challenge_input = 29_000_000;
assert_eq!(run(challenge_input), (665280, 705600));
}
}