1
0
Fork 0
mirror of https://github.com/wezm/advent-of-code.git synced 2025-01-23 02:50:47 +00:00
advent-of-code/2018/src/bin/day1.rs

37 lines
829 B
Rust
Raw Permalink Normal View History

use std::io::{BufRead, BufReader};
2018-12-01 22:53:05 +00:00
use std::fs::{self, File};
use std::collections::HashSet;
const INPUT: &str = "input/day1.txt";
2018-12-01 22:53:05 +00:00
fn main() {
part1();
part2();
}
fn part1() {
let input = File::open(INPUT).expect("input");
let input = BufReader::new(input);
let frequency = input.lines().map(|line| {
line.unwrap().parse::<i32>().expect("unable to parse line")
}).sum::<i32>();
println!("{}", frequency);
}
fn part2() {
let input = fs::read_to_string(INPUT).expect("input");
let mut frequency = 0;
let mut seen = HashSet::new();
for line in input.lines().cycle() {
let drift = line.parse::<i32>().expect("unable to parse line");
frequency += drift;
if !seen.insert(frequency) {
break;
}
}
println!("{}", frequency);
}