From e2931282dca4bbd06e0902c444c6428f98f9607a Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Tue, 29 Dec 2020 22:19:21 +0100 Subject: [PATCH] [TASK] Solved Day 23 Pt 1 --- input/day23.txt | 1 + input/day23_example.txt | 1 + src/day21.rs | 2 +- src/day23.rs | 84 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 +- 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 input/day23.txt create mode 100644 input/day23_example.txt create mode 100644 src/day23.rs diff --git a/input/day23.txt b/input/day23.txt new file mode 100644 index 0000000..0f6ac81 --- /dev/null +++ b/input/day23.txt @@ -0,0 +1 @@ +186524973 \ No newline at end of file diff --git a/input/day23_example.txt b/input/day23_example.txt new file mode 100644 index 0000000..7a64a0a --- /dev/null +++ b/input/day23_example.txt @@ -0,0 +1 @@ +389125467 \ No newline at end of file diff --git a/src/day21.rs b/src/day21.rs index a469a28..48da2cf 100644 --- a/src/day21.rs +++ b/src/day21.rs @@ -31,7 +31,7 @@ pub fn solve() { possible_ingredients_per_allergen.insert(allergen, matching_ingredients); } - println!("{:?}", possible_ingredients_per_allergen); + // println!("{:?}", possible_ingredients_per_allergen); // Now all the ingredients that are not "possible ingredients per allergen" can not possibly contain an allergen, so we count those instances: let allergy_ingredients: HashSet<&String> = HashSet::from_iter(possible_ingredients_per_allergen.iter() diff --git a/src/day23.rs b/src/day23.rs new file mode 100644 index 0000000..6cc97ee --- /dev/null +++ b/src/day23.rs @@ -0,0 +1,84 @@ +use super::util; +use std::collections::LinkedList; +use std::iter::FromIterator; +use std::convert::TryFrom; +use std::intrinsics::copy; + +pub fn solve() { + let lines = util::read_file("input/day23.txt"); + let input = lines.first().unwrap().chars().map(|c| (c as u8 - '0' as u8)).collect::>(); + + let result = play_game(<&[u8; 9]>::try_from(&input[0..]).unwrap(), 100); + let part1 = reorder_starting_from(&result, 1).iter().take(8).map(|s| s.to_string()).collect::>().join(""); + println!("Day 23 Part 1: {}", part1); + + + let part2 = 0; + println!("Day 23 Part 2: {}", part2); +} + +fn play_game(input: &[u8; 9], rounds: u64) -> [u8; 9] { + + let max_cup = input.iter().max().unwrap().clone(); + let min_cup = input.iter().min().unwrap().clone(); + let len = input.len(); + let mut cur_order = input.clone(); + let mut next_order = input.clone(); + let mut cur_idx = 0usize; + for _ in 0..rounds { + + let cur_label = &cur_order[cur_idx]; + let mut dest_label = if cur_label == &min_cup { max_cup } else { cur_label - 1 }; + let mut dest_idx = index_of(&cur_order, &dest_label); + let mut looped_dest_idx = if dest_idx < cur_idx { dest_idx + input.len() } else { dest_idx }; + + while looped_dest_idx > cur_idx && looped_dest_idx <= cur_idx + 3 { + dest_label = if dest_label == min_cup { max_cup } else { dest_label - 1 }; + dest_idx = index_of(&cur_order, &dest_label); + looped_dest_idx = if dest_idx < cur_idx { dest_idx + input.len() } else { dest_idx }; + } + + + // First copy the part from the end of the "picked up" cups to the destination + for i in cur_idx + 4..looped_dest_idx + 1 { + next_order[(i - 3) % len] = cur_order[i % len]; + } + + // Then copy the picked up cups to the destination + for i in 0..3 { + next_order[(looped_dest_idx - 2 + i) % len] = cur_order[(cur_idx + 1 + i) % len]; + } + + // println!("{:?}", next_order); + for c in input { + assert!(next_order.contains(c), format!("Lost {} while playing with index {} and destination label {}, dest index {}", c, cur_idx, dest_label, dest_idx)); + } + + // The rest should already be in the correct order + cur_order = next_order.clone(); + + cur_idx = (cur_idx + 1) % input.len(); + } + + return cur_order; +} + +fn index_of(cups: &[u8;9], cup: &u8) -> usize { + for i in 0..cups.len() { + if &cups[i] == cup { + return i; + } + } + panic!(format!("Couldn't find {}", cup)); +} + +fn reorder_starting_from(cups: &[u8; 9], n:u8) -> [u8; 9] { + + let n_idx = index_of(cups, &n); + let mut res = [0u8; 9]; + for i in 0..9 { + res[i] = cups[(n_idx + 1 + i) % cups.len()]; + } + return res; + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fee3838..a549154 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,9 @@ mod day19; mod day20; mod day21; mod day22; +mod day23; -const MAX_DAY: u8 = 22; +const MAX_DAY: u8 = 23; const BENCHMARK_AMOUNT: u32 = 100; fn solve(day: u8) { @@ -52,6 +53,7 @@ fn solve(day: u8) { 20 => day20::solve(), 21 => day21::solve(), 22 => day22::solve(), + 23 => day23::solve(), _ => println!("This day is not yet implemented") } }