aboutsummaryrefslogtreecommitdiff
path: root/day02.rs
diff options
context:
space:
mode:
Diffstat (limited to 'day02.rs')
-rw-r--r--day02.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/day02.rs b/day02.rs
new file mode 100644
index 0000000..3c5b9b5
--- /dev/null
+++ b/day02.rs
@@ -0,0 +1,33 @@
+use std::fs::File;
+use std::io::{BufRead, BufReader};
+
+fn appears(s: &str, n: usize) -> bool {
+ s.chars().any(|c| s.matches(c).count() == n)
+}
+
+fn same_but_one(s: &str, t: &str) -> Option<String> {
+ let mut x = s.chars().zip(t.chars()).enumerate().filter(|(_, (a,b))| a != b);
+ match (x.next(), x.next()) {
+ (Some(m), None) => Some(format!("{}{}", &s[..m.0], &s[m.0+1..])),
+ _ => None
+ }
+}
+
+fn main() {
+ let f = File::open("day02").unwrap();
+
+ let v: Vec<_> = BufReader::new(&f).lines().map(|l| l.unwrap()).collect();
+
+ println!("{}",
+ v.iter().filter(|s| appears(s, 2)).count() *
+ v.iter().filter(|s| appears(s, 3)).count());
+
+ 'outer: for x in &v {
+ for y in &v {
+ if let Some(s) = same_but_one(&x, &y) {
+ println!("{}", s);
+ break 'outer;
+ }
+ }
+ }
+}