Compare commits

..

No commits in common. "3185eef1300cd02ca0f36de0951c29c3404ff839" and "d3ded19a61d0e3905a457f9a71579d3a1bf8e819" have entirely different histories.

4 changed files with 41 additions and 1236 deletions

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -1,186 +0,0 @@
//! --- 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,7 +6,6 @@ pub mod day5;
pub mod day6;
pub mod day7;
pub mod day8;
pub mod day9;
use aoc_runner_derive::aoc_lib;