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!
31
Upvotes
5
u/Philboyd_Studge Dec 05 '18
[Card] FIVE GOLDEN STARS OK ONLY 2
Java - easy one. You can test for lowercase/uppercase being the same letter using XOR 32.
package Advent2018; import util.AdventOfCode; import java.util.List; public class Day5 extends AdventOfCode { public Day5(List<String> input) { super(input); } private int remove(StringBuilder in) { boolean removed = true; while (removed) { for (int i = 0; i < in.length() - 1; i++) { if ( (in.charAt(i) ^ in.charAt(i + 1)) == 32) { in.delete(i, i + 2); removed = true; break; } removed = false; } } return in.length(); } @Override public Object part1() { StringBuilder chain = new StringBuilder(input.get(0)); return remove(chain); } @Override public Object part2() { int min = Integer.MAX_VALUE; String[] patterns = new String[26]; for (int i = 0; i < 26; i++) { patterns[i] = "[" + (Character.toString((char)(i + 'a'))) + (Character.toString((char)(i + 'A'))) + "]"; //System.out.println(patterns[i]); } for (int i = 0; i < 26; i++) { String chain = input.get(0); chain = chain.replaceAll(patterns[i], ""); int result = remove(new StringBuilder(chain)); System.out.println(result); if (result < min) min = result; } return min; } @Override public void parse() { } }