diff options
author | Federico Igne <git@federicoigne.com> | 2021-12-02 12:02:58 +0000 |
---|---|---|
committer | Federico Igne <git@federicoigne.com> | 2021-12-02 15:11:34 +0000 |
commit | 0da5993fbf69e7fc515888afd3cf02b01baf386c (patch) | |
tree | 23a5080e169e7c92d96287a41bfdbbde1cf3499f /day1 | |
parent | 2933a91f9a1d7030fbc67a2fd497c6e26d46d862 (diff) | |
download | aoc-0da5993fbf69e7fc515888afd3cf02b01baf386c.tar.gz aoc-0da5993fbf69e7fc515888afd3cf02b01baf386c.zip |
Day 1 (improvements)
Diffstat (limited to 'day1')
-rw-r--r-- | day1/src/main.rs | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/day1/src/main.rs b/day1/src/main.rs index dd64a15..4db292f 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs | |||
@@ -1,5 +1,15 @@ | |||
1 | use std::fs; | 1 | use std::fs; |
2 | 2 | ||
3 | fn ex1(sweep: &[u32]) { | ||
4 | let incs = sweep.windows(2).filter(|w| w[0] < w[1]).count(); | ||
5 | println!("Ex1: depth increases {} times.", incs); | ||
6 | } | ||
7 | |||
8 | fn ex2(sweep: &[u32]) { | ||
9 | let incs = sweep.windows(4).filter(|w| w[0] < w[3]).count(); | ||
10 | println!("Ex2: depth increases {} times.", incs); | ||
11 | } | ||
12 | |||
3 | /* AOC21 Day 1: https://adventofcode.com/2021/day/1 */ | 13 | /* AOC21 Day 1: https://adventofcode.com/2021/day/1 */ |
4 | fn main() { | 14 | fn main() { |
5 | let input = "resources/input.txt"; | 15 | let input = "resources/input.txt"; |
@@ -8,14 +18,3 @@ fn main() { | |||
8 | ex1(&sweep); | 18 | ex1(&sweep); |
9 | ex2(&sweep); | 19 | ex2(&sweep); |
10 | } | 20 | } |
11 | |||
12 | fn ex1(sweep: &[u32]) { | ||
13 | let incs = sweep.windows(2).fold(0, |a, w| if w[0] < w[1] { a+1 } else { a }); | ||
14 | println!("Ex1: depth increases {} times.", incs); | ||
15 | } | ||
16 | |||
17 | fn ex2(sweep: &[u32]) { | ||
18 | let avg3: Vec<u32> = sweep.windows(3).map(|w| w[0] + w[1] + w[2]).collect(); | ||
19 | let incs = avg3.windows(2).fold(0, |a, w| if w[0] < w[1] { a+1 } else { a }); | ||
20 | println!("Ex2: depth increases {} times.", incs); | ||
21 | } | ||