[rust] Don't use a Day trait
change
commit 7afc3ba7143a6e5f069c249a3a968514826a4519
author Alpha Chen <alpha.chen@gmail.com>
date
parent e4027329
diff --git a/rust/src/day_01.rs b/rust/src/day_01.rs
index cdd5863..8a58271 100644
--- a/rust/src/day_01.rs
+++ b/rust/src/day_01.rs
@@ -1,20 +1,8 @@
-use std::io;
 use std::str::Chars;
 
-use day::Day;
-
-pub struct Day01 {
-    elevator: Elevator,
-}
-
-impl Day for Day01 {
-    fn new(input: String) -> Day01 {
-        Day01 { elevator: Elevator::new(input) }
-    }
-
-    fn solve(&self) -> io::Result<i32> {
-        Ok(1 + self.elevator.run().position(|f| f == -1).unwrap_or(0) as i32)
-    }
+pub fn solve(input: &str) -> i32 {
+    let elevator = Elevator::new(input.to_string());
+    1 + elevator.run().position(|f| f == -1).unwrap_or(0) as i32
 }
 
 struct Elevator {
diff --git a/rust/src/day_02.rs b/rust/src/day_02.rs
index 106f87d..bba4e38 100644
--- a/rust/src/day_02.rs
+++ b/rust/src/day_02.rs
@@ -1,20 +1,6 @@
-use std::io;
-
-use day::Day;
-
-pub struct Day02 {
-    presents: Vec<Present>,
-}
-
-impl Day for Day02 {
-    fn new(input: String) -> Day02 {
-        let presents = input.split("\n").map(|line| Present::new(line));
-        Day02 { presents: presents.collect::<Vec<Present>>() }
-    }
-
-    fn solve(&self) -> io::Result<i32> {
-        Ok(self.presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32)
-    }
+pub fn solve(input: &str) -> i32 {
+    let presents = input.split("\n").map(|line| Present::new(line)).collect::<Vec<Present>>();
+    presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32
 }
 
 struct Present {
diff --git a/rust/src/day_03.rs b/rust/src/day_03.rs
index c64e2ec..8c720f6 100644
--- a/rust/src/day_03.rs
+++ b/rust/src/day_03.rs
@@ -1,42 +1,29 @@
 use std::cmp::Eq;
 use std::collections::HashMap;
 use std::fmt;
-use std::io;
 use std::ops::Add;
 use std::str::Chars;
 
-use day::Day;
+pub fn solve(input: &str) -> i32 {
+    let mut santa = String::new();
+    let mut robo_santa = String::new();
+    let mut iter = input.chars();
 
-pub struct Day03 {
-    directions: String,
-}
-
-impl Day for Day03 {
-    fn new(input: String) -> Day03 {
-        Day03 { directions: input }
-    }
-
-    fn solve(&self) -> io::Result<i32> {
-        let mut santa = String::new();
-        let mut robo_santa = String::new();
-        let mut iter = self.directions.chars();
-
-        loop {
-            match iter.next() {
-                Some(c) => { santa.push(c) },
-                None => break,
-            }
-            match iter.next() {
-                Some(c) => { robo_santa.push(c) },
-                None => break,
-            }
+    loop {
+        match iter.next() {
+            Some(c) => { santa.push(c) },
+            None => break,
+        }
+        match iter.next() {
+            Some(c) => { robo_santa.push(c) },
+            None => break,
         }
-
-        let mut houses: HashMap<Point, u32> = HashMap::new();
-        Santa::houses_visited(&santa, &mut houses);
-        Santa::houses_visited(&robo_santa, &mut houses);
-        Ok(houses.len() as i32)
     }
+
+    let mut houses: HashMap<Point, u32> = HashMap::new();
+    Santa::houses_visited(&santa, &mut houses);
+    Santa::houses_visited(&robo_santa, &mut houses);
+    houses.len() as i32
 }
 
 struct Santa<'a> {
diff --git a/rust/src/day_04.rs b/rust/src/day_04.rs
index daa28db..8289193 100644
--- a/rust/src/day_04.rs
+++ b/rust/src/day_04.rs
@@ -1,42 +1,27 @@
-use std::io;
-
 use crypto::md5::Md5;
 use crypto::digest::Digest;
 
-use day::Day;
-
-pub struct Day04 {
-    secret: String,
-}
-
-impl Day for Day04 {
-    fn new(input: String) -> Day04 {
-        Day04 { secret: input }
-    }
-
-    fn solve(&self) -> io::Result<i32> {
-        let mut md5 = Md5::new();
-        let key = self.secret.as_bytes();
-        let mut i = 0;
-        let mut out = [0; 16];
-        loop {
-            i += 1;
+pub fn solve(input: &str) -> i32 {
+    let mut md5 = Md5::new();
+    let key = input.as_bytes();
+    let mut i = 0;
+    let mut out = [0; 16];
+    loop {
+        i += 1;
 
-            md5.input(key);
-            md5.input(i.to_string().as_bytes());
-            md5.result(&mut out);
-            if out[0] == 0 && out[1] == 0 && out[2] == 0 {
-                break;
-            }
-            md5.reset();
+        md5.input(key);
+        md5.input(i.to_string().as_bytes());
+        md5.result(&mut out);
+        if out[0] == 0 && out[1] == 0 && out[2] == 0 {
+            break;
         }
-        Ok(i)
+        md5.reset();
     }
+    i
 }
 
 #[test]
 #[ignore]
 fn test_day04() {
-    let day = Day04::new("abcdef".to_string());
-    assert_eq!(609043, day.solve().unwrap());
+    assert_eq!(609043, solve("abcdef"));
 }
diff --git a/rust/src/day_05.rs b/rust/src/day_05.rs
index edec83f..760842e 100644
--- a/rust/src/day_05.rs
+++ b/rust/src/day_05.rs
@@ -1,59 +1,44 @@
-use std::io;
+#![allow(dead_code)]
 
 use regex::Regex;
 
-use day::Day;
-
-pub struct Day05 {
-    input: String,
+pub fn solve(input: &str) -> i32 {
+    input.lines().filter(|&s| is_nice(&s.to_string())).count() as i32
 }
 
-#[allow(dead_code)]
-impl Day05 {
-    fn is_nice(string: &String) -> bool {
-        Day05::has_pair_of_two_letters(string) &&
-            Day05::has_letter_sandwich(string)
-    }
-
-    fn has_three_vowels(string: &String) -> bool {
-        let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
-        three_vowels.is_match(string)
-    }
-
-    fn has_double_letters(string: &String) -> bool {
-        string.as_bytes().windows(2).any(|win| win[0] == win[1])
-    }
+fn is_nice(string: &String) -> bool {
+    has_pair_of_two_letters(string) &&
+        has_letter_sandwich(string)
+}
 
-    fn has_no_substrings(string: &String) -> bool {
-        !vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
-    }
+fn has_three_vowels(string: &String) -> bool {
+    let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
+    three_vowels.is_match(string)
+}
 
-    fn has_pair_of_two_letters(string: &String) -> bool {
-        string.as_bytes().windows(2).any(|win| {
-            let s = String::from_utf8(win.to_vec()).unwrap();
-            string.split(&s).count() > 2
-        })
-    }
+fn has_double_letters(string: &String) -> bool {
+    string.as_bytes().windows(2).any(|win| win[0] == win[1])
+}
 
-    fn has_letter_sandwich(string: &String) -> bool {
-        string.as_bytes().windows(3).any(|win| win[0] == win[2])
-    }
+fn has_no_substrings(string: &String) -> bool {
+    !vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
 }
 
-impl Day for Day05 {
-    fn new(input: String) -> Day05 {
-        Day05 { input: input }
-    }
+fn has_pair_of_two_letters(string: &String) -> bool {
+    string.as_bytes().windows(2).any(|win| {
+        let s = String::from_utf8(win.to_vec()).unwrap();
+        string.split(&s).count() > 2
+    })
+}
 
-    fn solve(&self) -> io::Result<i32> {
-        Ok(self.input.lines().filter(|&s| Day05::is_nice(&s.to_string())).count() as i32)
-    }
+fn has_letter_sandwich(string: &String) -> bool {
+    string.as_bytes().windows(3).any(|win| win[0] == win[2])
 }
 
 #[test]
 fn test_nice() {
-    assert!( Day05::is_nice(&"qjhvhtzxzqqjkmpb".to_string()));
-    assert!( Day05::is_nice(&"xxyxx ".to_string()));
-    assert!(!Day05::is_nice(&"uurcxstgmygtbstg ".to_string()));
-    assert!(!Day05::is_nice(&"ieodomkazucvgmuy ".to_string()));
+    assert!( is_nice(&"qjhvhtzxzqqjkmpb".to_string()));
+    assert!( is_nice(&"xxyxx ".to_string()));
+    assert!(!is_nice(&"uurcxstgmygtbstg ".to_string()));
+    assert!(!is_nice(&"ieodomkazucvgmuy ".to_string()));
 }
diff --git a/rust/src/day_06.rs b/rust/src/day_06.rs
index 51009d5..f0879ea 100644
--- a/rust/src/day_06.rs
+++ b/rust/src/day_06.rs
@@ -1,23 +1,6 @@
-use std::io;
 use regex::Regex;
 
-use day::Day;
-
-pub struct Day06 {
-    input: String,
-}
-
-impl Day for Day06 {
-    fn new(input: String) -> Day06 {
-        Day06 { input: input }
-    }
-
-    fn solve(&self) -> io::Result<i32> {
-        Ok(solve(&self.input))
-    }
-}
-
-fn solve(input: &str) -> i32 {
+pub fn solve(input: &str) -> i32 {
     let mut lg = LightGrid::new();
 
     for line in input.lines() {
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 9051fb6..249280e 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -1,17 +1,15 @@
 extern crate crypto;
 extern crate regex;
 
-macro_rules! import_day {
+macro_rules! pub_mod {
     ( $( $d:ident ),* ) => {
         $(
-            pub use $d::*;
-            mod $d;
+            pub mod $d;
         )*
     }
 }
 
-import_day! {
-    day,
+pub_mod! {
     day_01,
     day_02,
     day_03,
diff --git a/rust/src/main.rs b/rust/src/main.rs
index 5403464..776c8c8 100644
--- a/rust/src/main.rs
+++ b/rust/src/main.rs
@@ -7,6 +7,5 @@ use advent_of_code::*;
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).ok();
-    let day = Day06::new(input);
-    println!("{}", day.solve().unwrap());
+    println!("{}", day_06::solve(&input));
 }