Compare commits

...

3 Commits

Author SHA1 Message Date
3185eef130 Updating README.md 2020-12-09 17:43:32 -08:00
32d13861d9 Day 9 part 2 solution 2020-12-09 17:41:11 -08:00
99c79f84fe Day 9 part 1 solution 2020-12-09 16:41:40 -08:00
4 changed files with 1236 additions and 41 deletions

View File

@ -1,87 +1,95 @@
``` ```
Advent of code 2020 Advent of code 2020
Day 1 - Part 1 - binary: 1006875 Day 1 - Part 1 - binary: 1006875
generator: 24.616µs, generator: 14.64µs,
runner: 551ns runner: 469ns
Day 1 - Part 1 - linear: 1006875 Day 1 - Part 1 - linear: 1006875
generator: 8.305µs, generator: 4.359µs,
runner: 11.516µs runner: 6.927µs
Day 1 - Part 1 - set: 1006875 Day 1 - Part 1 - set: 1006875
generator: 30.951µs, generator: 18.656µs,
runner: 1.226µs runner: 1.821µs
Day 1 - Part 2: 165026160 Day 1 - Part 2: 165026160
generator: 7.717µs, generator: 3.857µs,
runner: 1.537908ms runner: 1.097067ms
Day 2 - Part 1: 640 Day 2 - Part 1: 640
generator: 1.542315ms, generator: 1.63711ms,
runner: 105.423µs runner: 98.201µs
Day 2 - Part 1 - handrolled: 640 Day 2 - Part 1 - handrolled: 640
generator: 164.895µs, generator: 139.956µs,
runner: 99.099µs runner: 89.144µs
Day 2 - Part 2: 472 Day 2 - Part 2: 472
generator: 1.407729ms, generator: 1.293951ms,
runner: 9.654µs runner: 11.228µs
Day 3 - Part 1: 148 Day 3 - Part 1: 148
generator: 35.003µs, generator: 31.71µs,
runner: 1.025µs runner: 920ns
Day 3 - Part 2: 727923200 Day 3 - Part 2: 727923200
generator: 33.53µs, generator: 31.224µs,
runner: 4.357µs runner: 4.013µs
Day 4 - Part 1: 239 Day 4 - Part 1: 239
generator: 350.065µs, generator: 354.778µs,
runner: 1.603µs runner: 1.521µs
Day 4 - Part 2: 188 Day 4 - Part 2: 188
generator: 329.906µs, generator: 295.614µs,
runner: 48.36µs runner: 39.631µs
Day 5 - Part 1 - glenng: 989 Day 5 - Part 1 - glenng: 989
generator: 162ns, generator: 148ns,
runner: 79.3µs runner: 70.247µs
Day 5 - Part 1 - wathiede: 989 Day 5 - Part 1 - wathiede: 989
generator: 85.622µs, generator: 74.196µs,
runner: 594ns runner: 635ns
Day 5 - Part 2 - wathiede: 548 Day 5 - Part 2 - wathiede: 548
generator: 80.347µs, generator: 69.849µs,
runner: 28.44µs runner: 25.683µs
Day 6 - Part 1: 6930 Day 6 - Part 1: 6930
generator: 105ns, generator: 107ns,
runner: 518.654µs runner: 509.283µs
Day 6 - Part 2: 3585 Day 6 - Part 2: 3585
generator: 109ns, generator: 112ns,
runner: 1.780838ms runner: 1.752429ms
Day 6 - Part 2 - faster: 3585 Day 6 - Part 2 - faster: 3585
generator: 135ns, generator: 135ns,
runner: 843.403µs runner: 834.711µs
Day 7 - Part 1: 222 Day 7 - Part 1: 222
generator: 910.453µs, generator: 915.839µs,
runner: 171.213µs runner: 213.861µs
Day 7 - Part 2: 13264 Day 7 - Part 2: 13264
generator: 860.829µs, generator: 871.477µs,
runner: 3.662µs runner: 3.553µs
Day 8 - Part 1: 1744 Day 8 - Part 1: 1744
generator: 207ns, generator: 114ns,
runner: 36.361µs runner: 35.998µs
Day 8 - Part 2: 1174 Day 8 - Part 2: 1174
generator: 99ns, generator: 114ns,
runner: 131.713µs runner: 130.717µs
Day 9 - Part 1: 1309761972
generator: 26.764µs,
runner: 32.942µs
Day 9 - Part 2: 177989832
generator: 24.726µs,
runner: 158.532µs
``` ```

1000
2020/input/2020/day9.txt Normal file

File diff suppressed because it is too large Load Diff

186
2020/src/day9.rs Normal file
View File

@ -0,0 +1,186 @@
//! --- Day 9: Encoding Error ---
//! 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?
//!
//! --- 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?
use std::collections::HashSet;
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day9)]
fn parse(input: &str) -> Vec<usize> {
input
.split('\n')
.map(|s| s.parse::<usize>().unwrap())
.collect::<Vec<_>>()
}
fn solution1_impl(nums: &[usize], win_size: usize) -> usize {
nums.windows(win_size + 1)
// TODO(wathiede): try soring and/or hashmap for speed.
.skip_while(|chunk| {
let past = &chunk[..win_size];
let cur = chunk[win_size];
for p in past {
let diff = if cur > *p { cur - p } else { p - cur };
if past.contains(&diff) {
return true;
}
}
false
})
// TODO(wathiede): try find_map()
.map(|chunk| chunk[win_size])
.nth(0)
.unwrap()
}
#[aoc(day9, part1)]
fn solution1(nums: &[usize]) -> usize {
solution1_impl(nums, 25)
}
fn sum_min_max(low: usize, hi: usize, nums: &[usize]) -> usize {
// TODO(wathiede): rewrite with fold to do it in one pass.
nums[low..hi].iter().min().unwrap() + nums[low..hi].iter().max().unwrap()
}
// If contiguous numbers adding up to `sum` are found, the hi index (inclusive) is returned.
fn find_sum_at(low: usize, nums: &[usize], sum: usize) -> Option<usize> {
let mut p_sum = nums[low];
for hi in low + 1..nums.len() {
let n = nums[hi];
p_sum += n;
if p_sum == sum {
return Some(hi + 1);
}
if p_sum > sum {
return None;
}
}
unreachable!();
}
fn solution2_impl(nums: &[usize], win_size: usize) -> usize {
let sum = solution1_impl(nums, win_size);
dbg!(sum);
for low in 0..nums.len() - 1 {
if let Some(hi) = find_sum_at(low, nums, sum) {
return sum_min_max(low, hi, nums);
}
}
unreachable!();
}
#[aoc(day9, part2)]
fn solution2(nums: &[usize]) -> usize {
solution2_impl(nums, 25)
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &'static str = r#"35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576"#;
#[test]
fn part1() {
assert_eq!(solution1_impl(&parse(&INPUT), 5), 127);
}
#[test]
fn part2() {
assert_eq!(solution2_impl(&parse(&INPUT), 5), 62);
}
}

View File

@ -6,6 +6,7 @@ pub mod day5;
pub mod day6; pub mod day6;
pub mod day7; pub mod day7;
pub mod day8; pub mod day8;
pub mod day9;
use aoc_runner_derive::aoc_lib; use aoc_runner_derive::aoc_lib;