From a8e05159002f0b0fed0bec84869039ff039dad6b Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Thu, 7 Dec 2017 15:11:38 +1000 Subject: [PATCH] Add day 6 solution --- 2017/day/6/Cargo.lock | 4 ++++ 2017/day/6/Cargo.toml | 6 +++++ 2017/day/6/input | 1 + 2017/day/6/problem.txt | 23 +++++++++++++++++++ 2017/day/6/src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 2017/day/6/Cargo.lock create mode 100644 2017/day/6/Cargo.toml create mode 100644 2017/day/6/input create mode 100644 2017/day/6/problem.txt create mode 100644 2017/day/6/src/main.rs diff --git a/2017/day/6/Cargo.lock b/2017/day/6/Cargo.lock new file mode 100644 index 0000000..079ab9a --- /dev/null +++ b/2017/day/6/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "day6" +version = "0.1.0" + diff --git a/2017/day/6/Cargo.toml b/2017/day/6/Cargo.toml new file mode 100644 index 0000000..be94685 --- /dev/null +++ b/2017/day/6/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day6" +version = "0.1.0" +authors = ["Wesley Moore "] + +[dependencies] diff --git a/2017/day/6/input b/2017/day/6/input new file mode 100644 index 0000000..d369702 --- /dev/null +++ b/2017/day/6/input @@ -0,0 +1 @@ +2 8 8 5 4 2 3 1 5 5 1 2 15 13 5 14 diff --git a/2017/day/6/problem.txt b/2017/day/6/problem.txt new file mode 100644 index 0000000..934cce0 --- /dev/null +++ b/2017/day/6/problem.txt @@ -0,0 +1,23 @@ +--- Day 6: Memory Reallocation --- + +A debugger program here is having an issue: it is trying to repair a memory reallocation routine, but it keeps getting stuck in an infinite loop. + +In this area, there are sixteen memory banks; each memory bank can hold any number of blocks. The goal of the reallocation routine is to balance the blocks between the memory banks. + +The reallocation routine operates in cycles. In each cycle, it finds the memory bank with the most blocks (ties won by the lowest-numbered memory bank) and redistributes those blocks among the banks. To do this, it removes all of the blocks from the selected bank, then moves to the next (by index) memory bank and inserts one of the blocks. It continues doing this until it runs out of blocks; if it reaches the last memory bank, it wraps around to the first one. + +The debugger would like to know how many redistributions can be done before a blocks-in-banks configuration is produced that has been seen before. + +For example, imagine a scenario with only four memory banks: + + The banks start with 0, 2, 7, and 0 blocks. The third bank has the most blocks, so it is chosen for redistribution. + Starting with the next bank (the fourth bank) and then continuing to the first bank, the second bank, and so on, the 7 blocks are spread out over the memory banks. The fourth, first, and second banks get two blocks each, and the third bank gets one back. The final result looks like this: 2 4 1 2. + Next, the second bank is chosen because it contains the most blocks (four). Because there are four memory banks, each gets one block. The result is: 3 1 2 3. + Now, there is a tie between the first and fourth memory banks, both of which have three blocks. The first bank wins the tie, and its three blocks are distributed evenly over the other three banks, leaving it with none: 0 2 3 4. + The fourth bank is chosen, and its four blocks are distributed such that each of the four banks receives one: 1 3 4 1. + The third bank is chosen, and the same thing happens: 2 4 1 2. + +At this point, we've reached a state we've seen before: 2 4 1 2 was already seen. The infinite loop is detected after the fifth block redistribution cycle, and so the answer in this example is 5. + +Given the initial block counts in your puzzle input, how many redistribution cycles must be completed before a configuration is produced that has been seen before? + diff --git a/2017/day/6/src/main.rs b/2017/day/6/src/main.rs new file mode 100644 index 0000000..956ce4b --- /dev/null +++ b/2017/day/6/src/main.rs @@ -0,0 +1,51 @@ +use std::fs::File; +use std::io::{Read}; +use std::str::FromStr; +use std::collections::HashSet; + +fn main() { + let mut buffer = String::new(); + let mut file = File::open("input").expect("unable to open input file"); + file.read_to_string(&mut buffer).expect("error reading input"); + + let mut banks = buffer.as_str() + .split_whitespace() + .map(|digit| i32::from_str(&digit).unwrap()) + .collect::>(); + + println!("{}", redistribution_cycles(&mut banks)); +} + +fn redistribute(banks: &mut Vec) { + let max = *banks.iter().max().unwrap(); + let bank_to_redistribute = banks.iter().position(|blocks| *blocks == max).unwrap(); + let mut blocks = banks[bank_to_redistribute]; + let mut current_bank = bank_to_redistribute; + banks[bank_to_redistribute] = 0; + + while blocks > 0 { + current_bank = (current_bank + 1) % banks.len(); + banks[current_bank] += 1; + blocks -= 1; + } +} + +fn redistribution_cycles(banks: &mut Vec) -> usize { + let mut seen_states = HashSet::new(); + seen_states.insert(banks.clone()); + + loop { + redistribute(banks); + if seen_states.insert(banks.clone()) == false { + break; + } + } + + seen_states.len() +} + +#[test] +fn test_example() { + let mut banks = vec![0, 2, 7, 0]; + assert_eq!(redistribution_cycles(&mut banks), 5); +}