[2018][rust][2.0]
change lzsqkksskkypqsontvnvrxmurymxsmvu
commit 89f6de2bb5a735d1bd4d125aa817bc499d8b3110
author Alpha Chen <alpha.chen@gmail.com>
date
parent ulpmpzym
diff --git a/2018/ruby/day_03.rb b/2018/ruby/day_03.rb
index 5f89a5e..32686de 100644
--- a/2018/ruby/day_03.rb
+++ b/2018/ruby/day_03.rb
@@ -18,7 +18,7 @@ claims, fabric = ARGF
         fabric[[y+dy, x+dx]] << claim
       }
     }
-}
+  }
 
 p claims.reject {|claim|
   fabric.any? {|_,v|
diff --git a/2018/rust/src/day_02.rs b/2018/rust/src/day_02.rs
new file mode 100644
index 0000000..d24771d
--- /dev/null
+++ b/2018/rust/src/day_02.rs
@@ -0,0 +1,52 @@
+pub fn solve(input: &str) -> String {
+    let box_ids: Vec<_> = input.lines().map(BoxId::new).collect();
+
+    let two_count = box_ids.iter().filter(|&x| x.is_two()).count();
+    let three_count = box_ids.iter().filter(|&x| x.is_three()).count();
+
+    (two_count * three_count).to_string()
+}
+
+struct BoxId(String);
+
+impl BoxId {
+    fn new(s: &str) -> Self {
+        BoxId(s.into())
+    }
+
+    fn is_two(&self) -> bool {
+        self.is_n(2)
+    }
+
+    fn is_three(&self) -> bool {
+        self.is_n(3)
+    }
+
+    fn is_n(&self, n: usize) -> bool {
+        self.0
+            .chars()
+            .any(|x| self.0.chars().filter(|&y| x == y).count() == n)
+    }
+}
+
+#[test]
+fn test_is_two() {
+    assert_eq!(BoxId::new("abcdef").is_two(), false);
+    assert_eq!(BoxId::new("bababc").is_two(), true);
+    assert_eq!(BoxId::new("abbcde").is_two(), true);
+    assert_eq!(BoxId::new("abcccd").is_two(), false);
+    assert_eq!(BoxId::new("aabcdd").is_two(), true);
+    assert_eq!(BoxId::new("abcdee").is_two(), true);
+    assert_eq!(BoxId::new("ababab").is_two(), false);
+}
+
+#[test]
+fn test_is_three() {
+    assert_eq!(BoxId::new("abcdef").is_three(), false);
+    assert_eq!(BoxId::new("bababc").is_three(), true);
+    assert_eq!(BoxId::new("abbcde").is_three(), false);
+    assert_eq!(BoxId::new("abcccd").is_three(), true);
+    assert_eq!(BoxId::new("aabcdd").is_three(), false);
+    assert_eq!(BoxId::new("abcdee").is_three(), false);
+    assert_eq!(BoxId::new("ababab").is_three(), true);
+}
diff --git a/2018/rust/src/main.rs b/2018/rust/src/main.rs
index b407119..0135cf4 100644
--- a/2018/rust/src/main.rs
+++ b/2018/rust/src/main.rs
@@ -1,11 +1,12 @@
 use std::io::{self, Read};
 
 mod day_01;
+mod day_02;
 
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
-    let output = day_01::solve_1(&input, "\n");
+    let output = day_02::solve(&input);
     println!("{}", output);
 }