[2018][rust][1] move day 1 into a module
change nysztvxyxvzlzlprsuovosospzvylpmr
commit 24c4040c5949c75e4ea8b6f6e7cae4de7d3c94c4
author Alpha Chen <alpha.chen@gmail.com>
date
parent qpuymvqp
diff --git a/2018/rust/src/day_01.rs b/2018/rust/src/day_01.rs
new file mode 100644
index 0000000..8c6878d
--- /dev/null
+++ b/2018/rust/src/day_01.rs
@@ -0,0 +1,51 @@
+use std::collections::HashMap;
+
+pub fn solve_0(input: &str, delimiter: &str) -> String {
+    let sum: i32 = input
+        .trim()
+        .split(delimiter)
+        .map(|change| change.parse::<i32>().unwrap())
+        .sum();
+    sum.to_string()
+}
+
+#[test]
+fn test_solve_0() {
+    assert_eq!(solve_0("+1, -2, +3, +1", ", "), "3");
+}
+
+pub fn solve_1(input: &str, delimiter: &str) -> String {
+    vec![0 as i32] // Start with 0 frequency
+        .into_iter()
+        .chain(
+            input
+                .trim()
+                .split(delimiter)
+                .map(|change| change.parse::<i32>().unwrap())
+                .cycle(),
+        )
+        .scan(0, |freq, change| {
+            *freq += change;
+            Some(*freq)
+        })
+        .scan(HashMap::new(), |history, freq| {
+            let count = history.entry(freq).or_insert(0);
+            *count += 1;
+
+            Some((freq, *count))
+        })
+        .filter(|(_, count)| *count > 1)
+        .map(|(freq, _)| freq)
+        .next()
+        .unwrap()
+        .to_string()
+}
+
+#[test]
+fn test_solve_1() {
+    assert_eq!(solve_1("+1, -2, +3, +1", ", "), "2");
+    assert_eq!(solve_1("+1, -1", ", "), "0");
+    assert_eq!(solve_1("+3, +3, +4, -2, -4", ", "), "10");
+    assert_eq!(solve_1("-6, +3, +8, +5, -6", ", "), "5");
+    assert_eq!(solve_1("+7, +7, -2, -7, -4", ", "), "14");
+}
diff --git a/2018/rust/src/main.rs b/2018/rust/src/main.rs
index 9c04a65..b407119 100644
--- a/2018/rust/src/main.rs
+++ b/2018/rust/src/main.rs
@@ -1,58 +1,11 @@
-use std::collections::HashMap;
 use std::io::{self, Read};
 
-fn day_1_0(input: &str, delimiter: &str) -> String {
-    let sum: i32 = input
-        .split(delimiter)
-        .flat_map(|change| change.parse::<i32>().ok())
-        .sum();
-    sum.to_string()
-}
-
-#[test]
-fn test_day_1_0() {
-    assert_eq!(day_1_0("+1, -2, +3, +1", ", "), "3");
-}
-
-fn day_1_1(input: &str, delimiter: &str) -> String {
-    vec![0 as i32] // Start with 0 frequency
-        .into_iter()
-        .chain(
-            input
-                .split(delimiter)
-                .map(|change| change.parse::<i32>().unwrap())
-                .cycle(),
-        )
-        .scan(0, |freq, change| {
-            *freq += change;
-            Some(*freq)
-        })
-        .scan(HashMap::new(), |history, freq| {
-            let count = history.entry(freq).or_insert(0);
-            *count += 1;
-
-            Some((freq, *count))
-        })
-        .filter(|(_, count)| *count > 1)
-        .map(|(freq, _)| freq)
-        .next()
-        .unwrap()
-        .to_string()
-}
-
-#[test]
-fn test_day_1_1() {
-    assert_eq!(day_1_1("+1, -2, +3, +1", ", "), "2");
-    assert_eq!(day_1_1("+1, -1", ", "), "0");
-    assert_eq!(day_1_1("+3, +3, +4, -2, -4", ", "), "10");
-    assert_eq!(day_1_1("-6, +3, +8, +5, -6", ", "), "5");
-    assert_eq!(day_1_1("+7, +7, -2, -7, -4", ", "), "14");
-}
+mod day_01;
 
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
-    let output = day_1_1(&input, "\n");
+    let output = day_01::solve_1(&input, "\n");
     println!("{}", output);
 }