Johann Woelper 5 years ago
parent
commit
5d43810c25
2 changed files with 62 additions and 1 deletions
  1. 59 0
      src/day_4.rs
  2. 3 1
      src/main.rs

+ 59 - 0
src/day_4.rs

@@ -0,0 +1,59 @@
+fn part_1(c: u32) -> Option<u32> {
+    let mut prev = 0;
+    let mut has_double = false;
+    for i in c.to_string().chars() {
+        let ic: u32 = i.to_digit(10).unwrap();
+        if ic < prev {
+            return None
+        }
+        if ic == prev {
+            has_double = true;
+        }
+        prev = ic;
+    }
+    if !has_double {
+        return None;
+    }
+    Some(c)
+}
+
+
+// This is in fact just checking if there is two consecutive digits at all.
+fn part_2(c: u32) -> Option<u32> {
+    for d in 0..10 {
+        if c
+            .to_string()
+            .chars()
+            .filter(|c| c == &std::char::from_digit(d, 10).unwrap())
+            .count() == 2 {
+                return Some(c);
+        }
+    }
+    None
+}
+
+
+pub fn main() {
+
+    let mut sum = 0;
+    for c in 124075..580769+1 {
+        if let Some(valid_c) = part_1(c) {
+            // println!("{}", valid_c);
+            sum +=1;
+        }
+    }
+    println!("part one sum {}", sum);
+
+
+    let mut sum = 0;
+    for c in 124075..580769+1 {
+        if let Some(valid_c) = part_1(c) {
+            if let Some(second_c) = part_2(c) {
+            sum +=1;
+        }
+        }
+    }
+    println!("part two sum {}", sum);
+
+
+}

+ 3 - 1
src/main.rs

@@ -1,5 +1,6 @@
 
 mod day_3;
+mod day_4;
 
 
 
@@ -7,5 +8,6 @@ mod day_3;
 
 
 fn main() {
-    day_3::main();
+    // day_3::main();
+    day_4::main();
 }