summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-12-06 08:01:52 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-12-06 08:01:52 +0200
commitfc9a217119293918e94a0d0d983cdb291803f88f (patch)
tree29374c21c2c03100e0aa79b1f138268cd55865d4
parent895f9bdc60c476861757c083c98a516b8238c84a (diff)
Day 6: memory rebalancing
-rw-r--r--inputs/day_6.txt1
-rw-r--r--src/bin/day_6.rs61
-rw-r--r--src/lib.rs3
3 files changed, 65 insertions, 0 deletions
diff --git a/inputs/day_6.txt b/inputs/day_6.txt
new file mode 100644
index 0000000..b16e11b
--- /dev/null
+++ b/inputs/day_6.txt
@@ -0,0 +1 @@
+4 10 4 1 8 4 9 14 5 1 14 15 0 15 3 5
diff --git a/src/bin/day_6.rs b/src/bin/day_6.rs
new file mode 100644
index 0000000..be9a515
--- /dev/null
+++ b/src/bin/day_6.rs
@@ -0,0 +1,61 @@
+extern crate advent_of_code_2017;
+use advent_of_code_2017::*;
+
+fn main() {
+ let args = AdventArgs::init();
+
+ let init_layout = parse_space_separated_ints(&args.input[0]).unwrap();
+
+ let mut layouts = vec!(init_layout);
+ let mut balances = 0;
+ let mut cycle_found = false;
+ let mut cycle_size = 0;
+
+ while !cycle_found {
+ balances += 1;
+ let new_layout = find_next_layout(&layouts);
+
+ if let Some(index) = layouts.iter().position(|x| *x == new_layout) {
+ cycle_found = true;
+ cycle_size = layouts.len() - index;
+ };
+
+ layouts.push(new_layout);
+ }
+
+ if args.part == 1 {
+ println!("Did {} rebalances", balances);
+ } else {
+ println!("Cycle was {} long", cycle_size);
+ }
+}
+
+fn find_next_layout(layouts: &Vec<Vec<i32>>) -> Vec<i32> {
+ let previous_layout = layouts.last().unwrap();
+ rebalance(&previous_layout)
+}
+
+fn rebalance(layout: &Vec<i32>) -> Vec<i32> {
+ let biggest_container = layout.iter()
+ .enumerate()
+ .max_by(|&(ai, &asize), &(bi, &bsize)| {
+ asize.cmp(&bsize).then(bi.cmp(&ai))
+ })
+ .map(|(i, _)| i)
+ .unwrap();
+
+
+ let mut new_layout = layout.clone();
+ let mut to_redistribute = new_layout[biggest_container];
+ new_layout[biggest_container] = 0;
+ let mut target = (biggest_container + 1) % layout.len();
+
+ while to_redistribute > 0 {
+ new_layout[target] += 1;
+ to_redistribute -= 1;
+ target = (target + 1) % layout.len();
+ }
+
+ new_layout
+}
+
diff --git a/src/lib.rs b/src/lib.rs
index caa8881..2a7b73a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -62,6 +62,9 @@ impl AdventArgs {
pub fn one_number_input(&self) -> Result<i32, std::num::ParseIntError> {
self.input[0].parse()
}
+ pub fn number_per_line_input(&self) -> Result<Vec<i32>, std::num::ParseIntError> {
+ self.input.iter().map(|line| line.parse()).collect()
+ }
}
pub fn parse_space_separated_ints(line: &String) -> Result<Vec<i32>, std::num::ParseIntError> {