diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-12-01 07:13:56 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-12-01 07:13:56 +0200 |
commit | 2ae69db72b55374864bdbe3f88b5859e0ace9fe2 (patch) | |
tree | 68a1bf20a6c10423d3c54e3361ab5f093cb9789f /src/bin | |
parent | 819a06ab7310ff8606206be83bbba077992596c1 (diff) |
Day 1
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/day_1.rs | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs index 7c9cdbf..9f54c70 100644 --- a/src/bin/day_1.rs +++ b/src/bin/day_1.rs @@ -4,15 +4,34 @@ use advent_of_code_2018::*; use std::error::Error; use std::path::PathBuf; +use std::collections::HashSet; + // cargo watch -cs "cargo run --bin day_1" fn main() -> Result<(), Box<Error>> { let input = read_file(&PathBuf::from("inputs/1.txt"))?; - - println!("Input: {:?}", input); - - - + let input_ints: Vec<i32> = input.iter().map(|str| str.parse::<i32>().unwrap()).collect(); + + println!("Input: {:?}", input_ints); + + let sum: i32 = input_ints.iter().sum(); + println!("Sum: {}", sum); + + let mut seen = HashSet::new(); + let mut acc = 0; + let mut repeat_found = false; + while !repeat_found { + for i in &input_ints { + if seen.contains(&acc) { + repeat_found = true; + break; + } else { + seen.insert(acc); + } + acc += i; + } + } + println!("First repeat: {}", acc); Ok(()) } |