|
@@ -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);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|