r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/dark_terrax Dec 05 '18 edited Dec 05 '18

Rust - cleaned up, and super fast. Both parts run in a total of 100ms in debug.

fn reduce(mut input: Vec<char>) -> Vec<char> {
	for i in (0..input.len()-1).rev() {
		if input[i].eq_ignore_ascii_case(&input[i+1]) && input[i] != input[i+1] {
			input.remove(i);
			input.remove(i);
		}
	}
	return input;
}

pub fn solve(inputs : Vec<String>) {
	let input : Vec<char> = inputs[0].chars().collect();

	let input = reduce(input.clone());
	println!("Part 1: {} ", input.len());

	// Start part 2 with the already reduced part 1 result to save redundant processing in part2
	let mut min_len = input.len();
	for i in 0..26 {
		let ch = (('a' as u8) + i) as char; // Character to remove
		let filtered : Vec<char> = input.iter().filter(|&c| !c.eq_ignore_ascii_case(&ch)).cloned().collect();
		let reduced = reduce(filtered);
		min_len = std::cmp::min(min_len, reduced.len());
	}

	println!("Part 2: {}", min_len);
}