-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
38 lines (33 loc) · 862 Bytes
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::collections::HashSet;
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::io::prelude::*;
fn read_file(name: &str) -> Result<Vec<String>, io::Error>
{
let file = File::open(name)?;
let buf_reader = BufReader::new(file);
return buf_reader.lines().collect();
}
fn main() -> Result<(), io::Error>
{
let data = read_file("../data.txt")?
.iter()
.filter_map(|l| l.parse::<i32>().ok())
.collect::<Vec<i32>>();
let part1 = data.iter().sum::<i32>();
let mut i = 0;
let mut cache :HashSet<i32> = HashSet::new();
let mut part2 = 0;
loop {
part2 += data[i % data.len()];
if cache.contains(&part2) {
break;
}
cache.insert(part2);
i += 1;
}
println!("Part 1: {}", part1);
println!("Part 2: {}", part2);
return Ok(());
}