[TASK] Solved Day 23 Pt 1

This commit is contained in:
2020-12-29 22:19:21 +01:00
parent d48df9336f
commit e2931282dc
5 changed files with 90 additions and 2 deletions

1
input/day23.txt Normal file
View File

@@ -0,0 +1 @@
186524973

1
input/day23_example.txt Normal file
View File

@@ -0,0 +1 @@
389125467

View File

@@ -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()

84
src/day23.rs Normal file
View File

@@ -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::<Vec<u8>>();
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::<Vec<String>>().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;
}

View File

@@ -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")
}
}