From cc0f0211c8ac8533935d5696470a38f0527a5a9a Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Wed, 6 Dec 2017 00:28:02 -0500 Subject: Day 6, part 2 --- 2017/src/bin/day06.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/2017/src/bin/day06.rs b/2017/src/bin/day06.rs index dbc8d07..509e576 100644 --- a/2017/src/bin/day06.rs +++ b/2017/src/bin/day06.rs @@ -1,16 +1,17 @@ -use std::collections::HashSet; +use std::collections::hash_map::{Entry, HashMap}; use std::io::{self, Read}; -fn solve1(input: &str) -> u32 { +fn solve(input: &str) -> (u32, u32) { let mut banks: Vec = input.split_whitespace() .map(str::parse) .map(Result::unwrap) .collect(); - let mut states = HashSet::new(); + let mut states = HashMap::new(); for cycle in 0.. { - if !states.insert(banks.clone()) { - return cycle; + match states.entry(banks.clone()) { + Entry::Occupied(e) => return (cycle, cycle - e.get()), + Entry::Vacant(e) => { e.insert(cycle); }, } let (index, mut blocks) = banks.iter() @@ -32,14 +33,28 @@ fn solve1(input: &str) -> u32 { unreachable!() } +fn solve1(input: &str) -> u32 { + solve(input).0 +} + +fn solve2(input: &str) -> u32 { + solve(input).1 +} + fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); println!("Part 1: {}", solve1(input.trim())); + println!("Part 2: {}", solve2(input.trim())); } #[test] fn part1() { assert_eq!(5, solve1("0 2 7 0")); } + +#[test] +fn part2() { + assert_eq!(4, solve2("0 2 7 0")); +} -- cgit 1.4.1 6a9b42a3604d30911c96d5ce4ae3c9be4de97'/>
Commit message (Collapse)Author
2019-07-27Add c11.pdfJune McEnroe
2019-07-27Move pdf script to MakefileJune McEnroe
2019-07-27Update macOS neovim to 0.3.8June McEnroe
Which somehow still doesn't include my man fix.
2019-07-27Rewrite port makefiles consistentlyJune McEnroe
2019-07-27Rewrite Makefile once more with more organizationJune McEnroe
2019-07-27Reference png.h in bin.7June McEnroe
2019-07-27Revert "Use scheme to style HTMLs"June McEnroe
This reverts commit 4b7d5161a30645b0a23691687ef1aafdda62d3bb. I don't know why but I don't like it.
2019-07-27Remove scheme text linkJune McEnroe
No standard base64 encoder so no way to maintain it.
2019-07-26Update Dark.terminalJune McEnroe
Why are the blobs so much bigger now?
2019-07-26Lighten dark whiteJune McEnroe
2019-07-25Add png.3 man pageJune McEnroe
2019-07-25Use scheme to style HTMLsJune McEnroe
2019-07-25Don't match comments in strings in makeJune McEnroe
2019-07-25Generate index.html with links from bin.7June McEnroe
2019-07-24Warn when date has changedJune McEnroe
2019-07-24Add whenJune McEnroe
When? Now.
2019-07-24Remove histedit from bitJune McEnroe