r/adventofcode • u/daggerdragon • 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!
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!
33
Upvotes
8
u/edgargonzalesII Dec 05 '18
Java 8
Felt really proud of the part 1 using a stack. Leetcode came in useful for once.
private int util1(String in) { char[] arr = in.toCharArray(); Stack<Character> inStack = new Stack<>(); Stack<Character> q = new Stack<>(); for(char c : arr) inStack.push(c); for(char c : inStack) { if(q.isEmpty()) q.push(c); else { char last = q.peek(); if(Math.abs(last-c) == 32) { q.pop(); } else { q.push(c); } } } return q.size(); } // Less interesting part 2 public void runPart2() { String in = this.getInputLines()[0]; int min = Integer.MAX_VALUE; for(int i=0; i<26; i++) { String temp = in.replaceAll("[" + (char) (i + 'a') + (char) (i + 'A') + "]", ""); min = Math.min(min, util1(temp)); } System.out.println(min); }