Skip to content

Commit 3820880

Browse files
author
thom
committed
day6 part1
1 parent e76aeab commit 3820880

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

2017/day6/src/main.rs

+26-42
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,48 @@
1-
// use std::cmp;
21
use std::collections::HashSet;
32

43
fn main() {
54
let input = "5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6";
5+
const BANK_QTY: usize = 16;
66

77
// setup memory banks
8-
let mut banks: [u32; 0x10] = [0; 0x10];
8+
let mut banks: [u32; BANK_QTY] = [0; BANK_QTY];
99

1010
// set slots to initial values
1111
for (index, element) in input.split("\t").enumerate() {
1212
banks[index] = element.parse::<u32>().unwrap();
1313
}
1414

1515
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;
2016

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;
4219

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.
4420

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));
4623

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;
4827

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+
}
5031

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;
5236

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+
};
5941

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+
}
6344

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

Comments
 (0)