Watch
1
0
Fork
You've already forked advent-of-code
0

Day 1 part 2

This commit is contained in:
Jeremy Kaplan 2018-12-01 12:09:39 -08:00
commit b6d882c8a7

View file

@ -1,13 +1,40 @@
use std::collections;
use std::str; use std::str;
fn main() { fn day1part1() -> i64 {
let input = include_str!("day1-input.txt"); let input = include_str!("day1-input.txt");
let mut freq = 0; let mut freq = 0;
for line in str::lines(input) { for line in str::lines(input) {
match line.parse::<i32>() { match line.parse::<i64>() {
Ok(d) => freq += d, Ok(d) => freq += d,
Err(s) => println!("{}", s), Err(s) => println!("{}", s),
} }
} }
println!("{}", freq); freq
}
fn day1part2() -> i64 {
let input = include_str!("day1-input.txt");
let mut freqs = collections::HashSet::new();
let mut freq = 0;
freqs.insert(freq);
loop {
for line in str::lines(input) {
match line.parse::<i64>() {
Ok(d) => {
freq += d;
if freqs.contains(&freq) {
return freq;
}
freqs.insert(freq);
}
Err(s) => println!("{}", s),
}
}
}
}
fn main() {
println!("{}", day1part1());
println!("{}", day1part2());
} }