[2018][rust][1.x] refactor

sorbet
Alpha Chen 6 years ago
parent 52cbaef2b1
commit 7eae195b2a

@ -15,20 +15,23 @@ fn test_day_1_0() {
} }
fn day_1_1(input: &str, delimiter: &str) -> String { fn day_1_1(input: &str, delimiter: &str) -> String {
let mut seen = HashMap::new(); vec![0 as i32] // Start with 0 frequency
seen.insert(0, 1); .into_iter()
.chain(
input input
.split(delimiter) .split(delimiter)
.flat_map(|change| change.parse::<i32>().ok()) .map(|change| change.parse::<i32>().unwrap())
.cycle() .cycle(),
.scan((seen, 0), |(seen, freq), change| { )
.scan(0, |freq, change| {
*freq += change; *freq += change;
Some(*freq)
let count = seen.entry(freq.clone()).or_insert(0); })
.scan(HashMap::new(), |history, freq| {
let count = history.entry(freq).or_insert(0);
*count += 1; *count += 1;
Some((*freq, *count)) Some((freq, *count))
}) })
.filter(|(_, count)| *count > 1) .filter(|(_, count)| *count > 1)
.map(|(freq, _)| freq) .map(|(freq, _)| freq)

Loading…
Cancel
Save