diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 20:26:02 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 20:26:02 +0200 |
commit | cfd9b4f2ad1a09bedf7f764f84448a61faab54a3 (patch) | |
tree | bdabffda69f447aa9e6816d14aa6125ac5b767bf /2018/src/bin/day_1.rs | |
parent | 5edb4153cde1171c5aff85de16a9ace526b0b293 (diff) |
Refile for merging repos
Diffstat (limited to '2018/src/bin/day_1.rs')
-rw-r--r-- | 2018/src/bin/day_1.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/2018/src/bin/day_1.rs b/2018/src/bin/day_1.rs new file mode 100644 index 0000000..4693579 --- /dev/null +++ b/2018/src/bin/day_1.rs @@ -0,0 +1,32 @@ +extern crate advent_of_code_2018; +use advent_of_code_2018::*; + +extern crate im_rc; + +use std::error::Error; +use std::path::PathBuf; + +use im_rc::HashSet; + +// cargo watch -cs "cargo run --release --bin day_1" + +fn main() -> Result<(), Box<Error>> { + let input = read_file(&PathBuf::from("inputs/1.txt"))?; + 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 first_repeat = input_ints.iter().cycle().try_fold((0, HashSet::new()), |(acc, seen), &i| { + if seen.contains(&acc) { + Err(acc) + } else { + Ok((acc + i, seen.update(acc))) + } + }).err().unwrap(); + println!("First repeat: {}", first_repeat); + + Ok(()) +} |