Added Solution for 2020 day 09
This commit is contained in:
parent
b0f42876e7
commit
efaa7c5ef6
5 changed files with 1187 additions and 0 deletions
8
2020/day09_encoding_error/Cargo.toml
Normal file
8
2020/day09_encoding_error/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day09_encoding_error"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
97
2020/day09_encoding_error/challenge.txt
Normal file
97
2020/day09_encoding_error/challenge.txt
Normal file
|
@ -0,0 +1,97 @@
|
|||
With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you.
|
||||
|
||||
Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input).
|
||||
|
||||
The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness.
|
||||
|
||||
XMAS starts by transmitting a *preamble* of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair.
|
||||
|
||||
For example, suppose your preamble consists of the numbers `1` through `25` in a random order. To be valid, the next number must be the sum of two of those numbers:
|
||||
|
||||
* `26` would be a *valid* next number, as it could be `1` plus `25` (or many other pairs, like `2` and `24`).
|
||||
* `49` would be a *valid* next number, as it is the sum of `24` and `25`.
|
||||
* `100` would *not* be valid; no two of the previous 25 numbers sum to `100`.
|
||||
* `50` would also *not* be valid; although `25` appears in the previous 25 numbers, the two numbers in the pair must be different.
|
||||
|
||||
Suppose the 26th number is `45`, and the first number (no longer an option, as it is more than 25 numbers ago) was `20`. Now, for the next number to be valid, there needs to be some pair of numbers among `1`-`19`, `21`-`25`, or `45` that add up to it:
|
||||
|
||||
* `26` would still be a *valid* next number, as `1` and `25` are still within the previous 25 numbers.
|
||||
* `65` would *not* be valid, as no two of the available numbers sum to it.
|
||||
* `64` and `66` would both be *valid*, as they are the result of `19+45` and `21+45` respectively.
|
||||
|
||||
Here is a larger example which only considers the previous *5* numbers (and has a preamble of length 5):
|
||||
|
||||
```
|
||||
35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576
|
||||
|
||||
```
|
||||
|
||||
In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is *`127`*.
|
||||
|
||||
The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is *not* the sum of two of the 25 numbers before it. *What is the first number that does not have this property?*
|
||||
|
||||
Your puzzle answer was `1504371145`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
The final step in breaking the XMAS encryption relies on the invalid number you just found: you must *find a contiguous set of at least two numbers* in your list which sum to the invalid number from step 1.
|
||||
|
||||
Again consider the above example:
|
||||
|
||||
```
|
||||
35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576
|
||||
|
||||
```
|
||||
|
||||
In this list, adding up all of the numbers from `15` through `40` produces the invalid number from step 1, `127`. (Of course, the contiguous set of numbers in your actual list might be much longer.)
|
||||
|
||||
To find the *encryption weakness*, add together the *smallest* and *largest* number in this contiguous range; in this example, these are `15` and `47`, producing *`62`*.
|
||||
|
||||
*What is the encryption weakness in your XMAS-encrypted list of numbers?*
|
||||
|
||||
Your puzzle answer was `183278487`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2020) and try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](9/input).
|
62
2020/day09_encoding_error/src/lib.rs
Normal file
62
2020/day09_encoding_error/src/lib.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use std::{num::ParseIntError, collections::VecDeque};
|
||||
|
||||
pub fn run(input: &str, preamble_length: usize) -> Result<(usize, usize), ParseIntError> {
|
||||
let numbers: Vec<_> = input.lines().map(|n| n.parse::<usize>()).collect::<Result<Vec<_>, _>>()?;
|
||||
let mut current = VecDeque::from(numbers[..preamble_length].to_vec());
|
||||
let mut first = 0;
|
||||
for n in numbers.iter().skip(preamble_length) {
|
||||
if contains_pair(¤t, *n) {
|
||||
current.pop_front();
|
||||
current.push_back(*n);
|
||||
} else {
|
||||
first = *n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let second = get_min_max_sum_of_contiguous(&numbers, first);
|
||||
Ok((first, second))
|
||||
}
|
||||
|
||||
fn contains_pair(list: &VecDeque<usize>, target: usize) -> bool {
|
||||
list.iter().enumerate().any(|(idx, x)| list.iter().skip(idx+1).any(|y| x+y == target))
|
||||
}
|
||||
|
||||
fn get_min_max_sum_of_contiguous(list: &[usize], target: usize) -> usize {
|
||||
for first_idx in 0..list.len() {
|
||||
let mut current_sum = 0;
|
||||
let mut current_summands = Vec::new();
|
||||
let mut last_idx = first_idx;
|
||||
while current_sum < target && last_idx < list.len() {
|
||||
let summand = list[last_idx];
|
||||
current_sum += summand;
|
||||
current_summands.push(summand);
|
||||
last_idx += 1;
|
||||
}
|
||||
if current_sum == target {
|
||||
return current_summands.iter().min().unwrap() + current_summands.iter().max().unwrap();
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input, 5), Ok((127, 62)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input, 25), Ok((1504371145, 183278487)));
|
||||
}
|
||||
}
|
1000
2020/day09_encoding_error/tests/challenge_input
Normal file
1000
2020/day09_encoding_error/tests/challenge_input
Normal file
File diff suppressed because it is too large
Load diff
20
2020/day09_encoding_error/tests/sample_input
Normal file
20
2020/day09_encoding_error/tests/sample_input
Normal file
|
@ -0,0 +1,20 @@
|
|||
35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576
|
Loading…
Add table
Add a link
Reference in a new issue