Files
advent-of-code/2018/01/part2.rs
Nick Thomas cb0c5fa868 Add '2018/' from commit '4f35fa85150c24c3c606851be3a8cd5efd6f5500'
git-subtree-dir: 2018
git-subtree-mainline: 5ccd921b23
git-subtree-split: 4f35fa8515
2022-01-09 17:08:32 +00:00

46 lines
1.1 KiB
Rust

use std::collections::HashSet;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::SeekFrom;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut file = match File::open("input") {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open input: {}", Error::description(&why)),
Ok(file) => file,
};
let mut repeat = true;
let mut total: i128 = 0;
let mut seen = HashSet::<i128>::new();
seen.insert(0);
while repeat {
file.seek(SeekFrom::Start(0));
let reader = BufReader::new(&file);
let lines = reader.lines();
for result in lines {
let l = result.unwrap();
let offset:i128 = i128::from_str_radix(&l, 10).unwrap();
total = total + offset;
println!("{} => {}", offset, total);
if seen.contains(&total) {
println!("seen {} before", total);
repeat = false;
break;
}
seen.insert(total);
}
}
println!("{}", total);
Ok(())
}