summaryrefslogtreecommitdiff
path: root/day1/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'day1/src/main.rs')
-rw-r--r--day1/src/main.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/day1/src/main.rs b/day1/src/main.rs
new file mode 100644
index 0000000..dd64a15
--- /dev/null
+++ b/day1/src/main.rs
@@ -0,0 +1,21 @@
1use std::fs;
2
3/* AOC21 Day 1: https://adventofcode.com/2021/day/1 */
4fn main() {
5 let input = "resources/input.txt";
6 let content = fs::read_to_string(input).expect("Unable to read input file.");
7 let sweep: Vec<u32> = content.lines().map(|l| l.parse::<u32>().expect("Malformed input")).collect();
8 ex1(&sweep);
9 ex2(&sweep);
10}
11
12fn 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
17fn 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}