Skip to content

Instantly share code, notes, and snippets.

@qubyte
Last active December 2, 2018 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qubyte/b99a47b75c91044f2b5c9767200389d4 to your computer and use it in GitHub Desktop.
Save qubyte/b99a47b75c91044f2b5c9767200389d4 to your computer and use it in GitHub Desktop.
Advent of Code 2018 day 01 task 2.
use std::io::{stdin, BufRead};
use std::collections::BTreeSet;
fn main() {
let shifts: Vec<i32> = stdin().lock()
.lines()
.filter_map(|line| line.unwrap().parse().ok())
.collect();
let mut frequency = 0;
let mut seen_frequencies = BTreeSet::new();
for shift in shifts.iter().cycle() {
frequency += shift;
if !seen_frequencies.insert(frequency) {
break;
}
}
println!("Frequency is {}", frequency);
}
@qubyte
Copy link
Author

qubyte commented Dec 2, 2018

To run (not optimised but fast enough for this task):

cargo run < input.txt

where input.txt is a file containing the input data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment