Day 1 part 2
This commit is contained in:
parent
0c7a00d4fb
commit
b6d882c8a7
1 changed files with 30 additions and 3 deletions
|
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue