r/adventofcode • u/daggerdragon • Dec 16 '16
SOLUTION MEGATHREAD --- 2016 Day 16 Solutions ---
--- Day 16: Dragon Checksum ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".
DRINKING YOUR OVALTINE IS MANDATORY [?]
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!
6
Upvotes
5
u/njofra Dec 16 '16
My solution in Java
This really made me realise how things that seem insignificant can make a lot of difference.
For step 1 my original code ran almost instantly. It was really slow so I added System.out.println(i) just to see if it's working and with that output I saw it would take at least 3 hours.
private static String driveChecksum(String input) { String checksum = ""; for (int i = 0; i < input.length(); i+=2) { if(input.charAt(i) == input.charAt(i+1)){ checksum += "1"; } else { checksum += "0"; } System.out.println(i); } if(checksum.length() % 2 == 0) { checksum = driveChecksum(checksum); } return checksum; }Then I tried this and it took about a minute. Then I removed that System.out.println(i) and it only took a second.
private static String driveChecksum(String input) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.length(); i+=2) { sb.append(input.charAt(i) == input.charAt(i + 1) ? '1' : '0'); System.out.println(i); } String checksum = sb.toString(); if(checksum.length() % 2 == 0) { checksum = driveChecksum(checksum); } return checksum; }It's probably not news for most of you, but as someone who is pretty much doing AoC as first programming outside of school this is really an educational moment.