From 1cdb2f668f7875b46419f4c1a4793b13cd7ec980 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Tue, 8 Dec 2020 22:03:07 -0800 Subject: [PATCH] day 9 --- src/main.rs | 2 +- src/problem9/lib.rs | 65 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index c112533..25a2180 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,6 @@ pub trait Problem { fn main() { let problem9 = Problem9::new(&util::get_problem(9)); - problem9.run_part1(); + //problem9.run_part1(); problem9.run_part2(); } diff --git a/src/problem9/lib.rs b/src/problem9/lib.rs index 513816b..c8d3aad 100644 --- a/src/problem9/lib.rs +++ b/src/problem9/lib.rs @@ -2,7 +2,7 @@ use crate::Problem; pub struct Problem9 { - number_list: Vec, + number_list: Vec, } impl Problem9 {} @@ -15,7 +15,7 @@ impl Problem for Problem9 { .filter_map(|s| { let s = s.trim(); if !s.is_empty() { - Some(s.parse::().unwrap()) + Some(s.parse::().unwrap()) } else { None } @@ -25,10 +25,71 @@ impl Problem for Problem9 { fn run_part1(&self) { + + let mut top = 0; + let mut bottom = 25; + + let mut rolling_array = &self.number_list[top..bottom]; + + println!("{:?}", self.number_list); + + while bottom < self.number_list.len() { + //let sum = rolling_array.iter().fold(0, |a, &b| a + b); + + println!("{:?}", rolling_array); + let mut found = false; + for i in rolling_array { + for q in rolling_array { + let sum = i + q; + if sum == self.number_list[bottom] { + found = true; + println!("the two numbers are {} {}", i, q); + } + } + } + if found == false { + println!("bad stuff found at {},supposed to be {}", bottom + 1, self.number_list[bottom]); + } + top += 1; + bottom += 1; + rolling_array = &self.number_list[top..bottom]; + } + + } + // invalid number was 26134589 fn run_part2(&self) { + let mut top = 0; + let mut bottom = top + 1; + + let mut rolling_array = &self.number_list[top..bottom]; + + println!("{:?}", self.number_list); + + while top + 1 < self.number_list.len() { + + while 26134589 >= rolling_array.iter().fold(0, |a, &b| a + b) { + bottom += 1; + rolling_array = &self.number_list[top..bottom]; + if rolling_array.iter().fold(0, |a, &b| a + b) == 26134589 { + let mut v = vec![0; 17]; + v.copy_from_slice(&rolling_array); + v.sort(); + println!("{:?}", v); + println!("found top {:?} bottom {}", top, bottom); + } + } + + let sum = rolling_array.iter().fold(0, |a, &b| a + b); + println!("top {}, bottom {}, sum {}", top, bottom, sum); + + + top += 1; + bottom = top + 1; + rolling_array = &self.number_list[top..bottom]; + } } }