[Rust/2020/09] Add solution
This commit is contained in:
parent
fb42493fff
commit
9f6f592510
6 changed files with 96 additions and 2 deletions
|
@ -19,11 +19,11 @@
|
|||
|[**20**](https://adventofcode.com/2021/day/20) [<img height=12 src=".assets/rs.svg">](Rust/2021/20.rs "Rust solution for 2021/20") [<img height=12 src=".assets/py.svg">](Python/2021/20.py "Python solution for 2021/20")|[**21**](https://adventofcode.com/2021/day/21) [<img height=12 src=".assets/rs.svg">](Rust/2021/21.rs "Rust solution for 2021/21") [<img height=12 src=".assets/py.svg">](Python/2021/21.py "Python solution for 2021/21")|[**22**](https://adventofcode.com/2021/day/22) [<img height=12 src=".assets/rs.svg">](Rust/2021/22.rs "Rust solution for 2021/22") [<img height=12 src=".assets/py.svg">](Python/2021/22.py "Python solution for 2021/22")|[**23**](https://adventofcode.com/2021/day/23) [<img height=12 src=".assets/rs.svg">](Rust/2021/23.rs "Rust solution for 2021/23") [<img height=12 src=".assets/py.svg">](Python/2021/23.py "Python solution for 2021/23")|[**24**](https://adventofcode.com/2021/day/24) [<img height=12 src=".assets/rs.svg">](Rust/2021/24.rs "Rust solution for 2021/24") [<img height=12 src=".assets/py.svg">](Python/2021/24.py "Python solution for 2021/24")|[**25**](https://adventofcode.com/2021/day/25) [<img height=12 src=".assets/rs.svg">](Rust/2021/25.rs "Rust solution for 2021/25") [<img height=12 src=".assets/py.svg">](Python/2021/25.py "Python solution for 2021/25")|26|
|
||||
|27|28|29|30|31|||
|
||||
|
||||
## [2020](https://adventofcode.com/2020) ([<img height=18 src=".assets/py.svg"> Python](Python/2020): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2020): 8/25)
|
||||
## [2020](https://adventofcode.com/2020) ([<img height=18 src=".assets/py.svg"> Python](Python/2020): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2020): 9/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
||[**1**](https://adventofcode.com/2020/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2020/01.rs "Rust solution for 2020/01") [<img height=12 src=".assets/py.svg">](Python/2020/01.py "Python solution for 2020/01")|[**2**](https://adventofcode.com/2020/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2020/02.rs "Rust solution for 2020/02") [<img height=12 src=".assets/py.svg">](Python/2020/02.py "Python solution for 2020/02")|[**3**](https://adventofcode.com/2020/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2020/03.rs "Rust solution for 2020/03") [<img height=12 src=".assets/py.svg">](Python/2020/03.py "Python solution for 2020/03")|[**4**](https://adventofcode.com/2020/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2020/04.rs "Rust solution for 2020/04") [<img height=12 src=".assets/py.svg">](Python/2020/04.py "Python solution for 2020/04")|[**5**](https://adventofcode.com/2020/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2020/05.rs "Rust solution for 2020/05") [<img height=12 src=".assets/py.svg">](Python/2020/05.py "Python solution for 2020/05")|[**6**](https://adventofcode.com/2020/day/6) [<img height=12 src=".assets/rs.svg">](Rust/2020/06.rs "Rust solution for 2020/06") [<img height=12 src=".assets/py.svg">](Python/2020/06.py "Python solution for 2020/06")|
|
||||
|[**7**](https://adventofcode.com/2020/day/7) [<img height=12 src=".assets/rs.svg">](Rust/2020/07.rs "Rust solution for 2020/07") [<img height=12 src=".assets/py.svg">](Python/2020/07.py "Python solution for 2020/07")|[**8**](https://adventofcode.com/2020/day/8) [<img height=12 src=".assets/rs.svg">](Rust/2020/08.rs "Rust solution for 2020/08") [<img height=12 src=".assets/py.svg">](Python/2020/08.py "Python solution for 2020/08")|[**9**](https://adventofcode.com/2020/day/9) [<img height=12 src=".assets/py.svg">](Python/2020/09.py "Python solution for 2020/09")|[**10**](https://adventofcode.com/2020/day/10) [<img height=12 src=".assets/py.svg">](Python/2020/10.py "Python solution for 2020/10")|[**11**](https://adventofcode.com/2020/day/11) [<img height=12 src=".assets/py.svg">](Python/2020/11.py "Python solution for 2020/11")|[**12**](https://adventofcode.com/2020/day/12) [<img height=12 src=".assets/py.svg">](Python/2020/12.py "Python solution for 2020/12")|[**13**](https://adventofcode.com/2020/day/13) [<img height=12 src=".assets/py.svg">](Python/2020/13.py "Python solution for 2020/13")|
|
||||
|[**7**](https://adventofcode.com/2020/day/7) [<img height=12 src=".assets/rs.svg">](Rust/2020/07.rs "Rust solution for 2020/07") [<img height=12 src=".assets/py.svg">](Python/2020/07.py "Python solution for 2020/07")|[**8**](https://adventofcode.com/2020/day/8) [<img height=12 src=".assets/rs.svg">](Rust/2020/08.rs "Rust solution for 2020/08") [<img height=12 src=".assets/py.svg">](Python/2020/08.py "Python solution for 2020/08")|[**9**](https://adventofcode.com/2020/day/9) [<img height=12 src=".assets/rs.svg">](Rust/2020/09.rs "Rust solution for 2020/09") [<img height=12 src=".assets/py.svg">](Python/2020/09.py "Python solution for 2020/09")|[**10**](https://adventofcode.com/2020/day/10) [<img height=12 src=".assets/py.svg">](Python/2020/10.py "Python solution for 2020/10")|[**11**](https://adventofcode.com/2020/day/11) [<img height=12 src=".assets/py.svg">](Python/2020/11.py "Python solution for 2020/11")|[**12**](https://adventofcode.com/2020/day/12) [<img height=12 src=".assets/py.svg">](Python/2020/12.py "Python solution for 2020/12")|[**13**](https://adventofcode.com/2020/day/13) [<img height=12 src=".assets/py.svg">](Python/2020/13.py "Python solution for 2020/13")|
|
||||
|[**14**](https://adventofcode.com/2020/day/14) [<img height=12 src=".assets/py.svg">](Python/2020/14.py "Python solution for 2020/14")|[**15**](https://adventofcode.com/2020/day/15) [<img height=12 src=".assets/py.svg">](Python/2020/15.py "Python solution for 2020/15")|[**16**](https://adventofcode.com/2020/day/16) [<img height=12 src=".assets/py.svg">](Python/2020/16.py "Python solution for 2020/16")|[**17**](https://adventofcode.com/2020/day/17) [<img height=12 src=".assets/py.svg">](Python/2020/17.py "Python solution for 2020/17")|[**18**](https://adventofcode.com/2020/day/18) [<img height=12 src=".assets/py.svg">](Python/2020/18.py "Python solution for 2020/18")|[**19**](https://adventofcode.com/2020/day/19) [<img height=12 src=".assets/py.svg">](Python/2020/19.py "Python solution for 2020/19")|[**20**](https://adventofcode.com/2020/day/20) [<img height=12 src=".assets/py.svg">](Python/2020/20.py "Python solution for 2020/20")|
|
||||
|[**21**](https://adventofcode.com/2020/day/21) [<img height=12 src=".assets/py.svg">](Python/2020/21.py "Python solution for 2020/21")|[**22**](https://adventofcode.com/2020/day/22) [<img height=12 src=".assets/py.svg">](Python/2020/22.py "Python solution for 2020/22")|[**23**](https://adventofcode.com/2020/day/23) [<img height=12 src=".assets/py.svg">](Python/2020/23.py "Python solution for 2020/23")|[**24**](https://adventofcode.com/2020/day/24) [<img height=12 src=".assets/py.svg">](Python/2020/24.py "Python solution for 2020/24")|[**25**](https://adventofcode.com/2020/day/25) [<img height=12 src=".assets/py.svg">](Python/2020/25.py "Python solution for 2020/25")|26|27|
|
||||
|28|29|30|31||||
|
||||
|
|
68
Rust/2020/09.rs
Normal file
68
Rust/2020/09.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
#![feature(test)]
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Input {
|
||||
numbers: Vec<u64>,
|
||||
n: usize,
|
||||
}
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
let mut lines = input.lines().peekable();
|
||||
let n = if lines.peek().unwrap().starts_with("# n=") {
|
||||
lines
|
||||
.next()
|
||||
.unwrap()
|
||||
.split('=')
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap()
|
||||
} else {
|
||||
25
|
||||
};
|
||||
let numbers = lines.map(|line| line.parse().unwrap()).collect();
|
||||
Input { numbers, n }
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> u64 {
|
||||
input
|
||||
.numbers
|
||||
.iter()
|
||||
.copied()
|
||||
.enumerate()
|
||||
.skip(input.n)
|
||||
.find(|&(i, x)| {
|
||||
!(i - input.n..i).any(|j| {
|
||||
(i - input.n..i).any(|k| {
|
||||
input.numbers[j] != input.numbers[k] && input.numbers[j] + input.numbers[k] == x
|
||||
})
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.1
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> u64 {
|
||||
let target = part1(input);
|
||||
let mut nums = VecDeque::new();
|
||||
let mut inp = input.numbers.iter().copied();
|
||||
let mut sum = 0;
|
||||
while sum != target {
|
||||
if sum < target {
|
||||
let x = inp.next().unwrap();
|
||||
nums.push_back(x);
|
||||
sum += x;
|
||||
} else {
|
||||
let x = nums.pop_front().unwrap();
|
||||
sum -= x;
|
||||
}
|
||||
}
|
||||
let (min, max) = nums.into_iter().minmax().into_option().unwrap();
|
||||
min + max
|
||||
}
|
||||
|
||||
aoc::main!(2020, 9, ex: 1);
|
|
@ -60,6 +60,9 @@ path = "2020/07.rs"
|
|||
[[bin]]
|
||||
name = "2020_08"
|
||||
path = "2020/08.rs"
|
||||
[[bin]]
|
||||
name = "2020_09"
|
||||
path = "2020/09.rs"
|
||||
|
||||
# 2021
|
||||
[[bin]]
|
||||
|
|
21
examples/2020/9/1
Normal file
21
examples/2020/9/1
Normal file
|
@ -0,0 +1,21 @@
|
|||
# n=5
|
||||
35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576
|
1
examples/2020/9/1.1
Normal file
1
examples/2020/9/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
127
|
1
examples/2020/9/1.2
Normal file
1
examples/2020/9/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
62
|
Loading…
Add table
Add a link
Reference in a new issue