|
1 |
| -// use std::cmp; |
2 | 1 | use std::collections::HashSet;
|
3 | 2 |
|
4 | 3 | fn main() {
|
5 | 4 | let input = "5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6";
|
| 5 | + const BANK_QTY: usize = 16; |
6 | 6 |
|
7 | 7 | // setup memory banks
|
8 |
| - let mut banks: [u32; 0x10] = [0; 0x10]; |
| 8 | + let mut banks: [u32; BANK_QTY] = [0; BANK_QTY]; |
9 | 9 |
|
10 | 10 | // set slots to initial values
|
11 | 11 | for (index, element) in input.split("\t").enumerate() {
|
12 | 12 | banks[index] = element.parse::<u32>().unwrap();
|
13 | 13 | }
|
14 | 14 |
|
15 | 15 | let index = get_max_index(&banks);
|
16 |
| - // let mut set = HashSet::new(); |
17 |
| - // set.insert(banks.clone()); |
18 |
| - let high_slot = banks[index]; |
19 |
| - banks[index] = 0; |
20 | 16 |
|
21 |
| - println!("{:#?}", (index, banks, high_slot)); |
22 |
| -} |
23 |
| - |
24 |
| -fn get_max_index(banks: &[u32]) -> usize { |
25 |
| - let max = banks.iter().max().unwrap(); |
26 |
| - banks.iter().position(|&p| p == *max).unwrap() |
27 |
| -} |
28 |
| - |
29 |
| -// fn main() { |
30 |
| -// let test = vec!["one", "two", "three"]; |
31 |
| -// let index = test.iter().position(|&r| r == "two").unwrap(); |
32 |
| -// println!("{}", index); |
33 |
| -// } |
34 |
| -// for indexing |
35 |
| -// can't you just take the height of all the stacks |
36 |
| -// push it on a HashSet |
37 |
| -// if the size doesn't change, it's a repeat |
38 |
| -// so the indicies are unique |
39 |
| -// just put a vector in the set |
40 |
| -/* |
41 |
| ---- Day 6: Memory Reallocation --- |
| 17 | + let mut set = HashSet::new(); |
| 18 | + let mut iteration = 0; |
42 | 19 |
|
43 |
| -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. |
44 | 20 |
|
45 |
| -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. |
| 21 | + let result = loop { |
| 22 | + // println!("{:?}", (banks)); |
46 | 23 |
|
47 |
| -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. |
| 24 | + // add array to hashset and check lengths |
| 25 | + set.insert(banks.clone()); |
| 26 | + iteration += 1; |
48 | 27 |
|
49 |
| -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. |
| 28 | + if iteration != set.len() { |
| 29 | + break set.len(); |
| 30 | + } |
50 | 31 |
|
51 |
| -For example, imagine a scenario with only four memory banks: |
| 32 | + // iterate |
| 33 | + let index = get_max_index(&banks); |
| 34 | + let high_slot = banks[index]; |
| 35 | + banks[index] = 0; |
52 | 36 |
|
53 |
| - The banks start with 0, 2, 7, and 0 blocks. The third bank has the most blocks, so it is chosen for redistribution. |
54 |
| - 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. |
55 |
| - 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. |
56 |
| - 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. |
57 |
| - The fourth bank is chosen, and its four blocks are distributed such that each of the four banks receives one: 1 3 4 1. |
58 |
| - The third bank is chosen, and the same thing happens: 2 4 1 2. |
| 37 | + for i in 0..high_slot { |
| 38 | + banks[(1 + index + i as usize) % BANK_QTY] += 1; |
| 39 | + } |
| 40 | + }; |
59 | 41 |
|
60 |
| -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. |
61 |
| -
|
62 |
| -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? |
| 42 | + println!("{:?}", result); |
| 43 | +} |
63 | 44 |
|
64 |
| -*/ |
| 45 | +fn get_max_index(banks: &[u32]) -> usize { |
| 46 | + let max = banks.iter().max().unwrap(); |
| 47 | + banks.iter().position(|&p| p == *max).unwrap() |
| 48 | +} |
0 commit comments