summary refs log tree commit diff
path: root/01/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2021-11-30 21:36:12 -0800
committerIrene Knapp <ireneista@gmail.com>2021-11-30 21:36:12 -0800
commitaed6777886ca340e392a039150a3465e6436e0f8 (patch)
treef2301b5e9c75a6d5a987dcbd112677da432853c0 /01/src
parent0fc36ff992d4ea1a7fe5240da8c5c63f7c602a6e (diff)
01 :)
Diffstat (limited to '01/src')
-rw-r--r--01/src/main.rs85
1 files changed, 42 insertions, 43 deletions
diff --git a/01/src/main.rs b/01/src/main.rs
index b7cdbbf..5c4348b 100644
--- a/01/src/main.rs
+++ b/01/src/main.rs
@@ -1,7 +1,5 @@
 use advent_lib::prelude::*;
 
-use std::collections::BTreeSet;
-
 
 fn main() -> Result<()> {
   let mut args = std::env::args();
@@ -11,56 +9,57 @@ fn main() -> Result<()> {
   let _ = args.next();
   let filename = args.next().unwrap();
 
-  let mut input = advent_lib::read_int_file(&filename)?;
-
-  input.sort();
-
-  let mut input_set = BTreeSet::new();
-  for item in &input {
-    input_set.insert(item);
-  }
+  let input = advent_lib::read_int_file(&filename)?;
+
+  {
+    let mut last_item = None;
+    let mut n_increases = 0;
+    for item in &input {
+      match last_item {
+        Some(value) => {
+          if *item > value {
+            n_increases += 1;
+          }
+        }
+        None => { }
+      }
 
-  for i in 0 .. input.len() {
-    let a = input[i];
-    if a > 2020 {
-      break;
+      last_item = Some(*item);
     }
 
-    let b = 2020 - a;
-    if input_set.contains(&b) {
-      let product = a * b;
-      println!("a: {:?}, b: {:?}, a*b: {:?}", a, b, product);
-      break;
-    }
+    println!("{}", n_increases);
   }
 
-  let mut done = false;
-  for i in 0 .. input.len() {
-    if done {
-      break;
-    }
-
-    let a = input[i];
-    if a > 2020 {
-      break;
-    }
-
-    for j in i+1 .. input.len() {
-      let b = input[j];
-
-      if a + b > 2020 {
-        break;
+  {
+    let mut window = Vec::new();
+    let mut last_sum = None;
+    let mut n_increases = 0;
+    for item in &input {
+      window.push(item);
+      if window.len() > 3 {
+        let _ = window.remove(0);
       }
 
-      let c = 2020 - a - b;
-      if input_set.contains(&c) {
-        let product = a * b * c;
-        println!("a: {:?}, b: {:?}, c: {:?}, a*b*c: {:?}", a, b, c, product);
-
-        done = true;
-        break;
+      if window.len() == 3 {
+        let mut sum = 0;
+        for a in &window {
+          sum += *a;
+        }
+
+        match last_sum {
+          None => { }
+          Some(value) => {
+            if sum > value {
+              n_increases += 1;
+            }
+          }
+        }
+
+        last_sum = Some(sum);
       }
     }
+
+    println!("{}", n_increases);
   }
 
   Ok(())