about summary refs log tree commit diff
path: root/day01.rs
diff options
context:
space:
mode:
Diffstat (limited to 'day01.rs')
-rw-r--r--day01.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/day01.rs b/day01.rs
new file mode 100644
index 0000000..a864d5d
--- /dev/null
+++ b/day01.rs
@@ -0,0 +1,22 @@
+use std::fs::File;
+use std::collections::HashSet;
+use std::io::{BufRead,BufReader};
+
+fn main() {
+    let f = File::open("day01").unwrap();
+
+    let v: Vec<i32> = BufReader::new(&f).lines()
+        .map(|l| l.unwrap().parse().unwrap())
+        .collect();
+    println!("{}", v.iter().sum::<i32>());  // 553
+
+    let mut seen = HashSet::new();
+    let s = v.iter()
+        .cycle()
+        .scan(0, |a,e| {
+            *a += e;
+            Some(*a)
+        })
+        .find(|n| !seen.insert(*n));
+    println!("{}", s.unwrap_or(-1));  // 78724
+}