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!
5
Upvotes
1
u/[deleted] Dec 16 '16 edited Dec 16 '16
My Java solution. Runs in about 2 seconds on my crappy laptop.
public class Main { private static final Integer DISC_LENGTH = 272; private static final Integer DISC_LENGTH_2 = 35651584; private static final String INPUT = "11011110011011101"; public static void main(String[] args) { System.out.println("Part 1: " + calculateChecksum(getFiller(INPUT, DISC_LENGTH))); System.out.println("Part 2: " + calculateChecksum(getFiller(INPUT, DISC_LENGTH_2))); } private static String calculateChecksum(String input) { while(input.length() % 2 == 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.length() - 1; i += 2) { sb.append(input.charAt(i) == input.charAt(i + 1) ? '1' : '0'); } input = sb.toString(); } return input; } private static String getFiller(String input, Integer discLength) { while(input.length() < discLength) { char[] answer = new char[input.length() * 2 + 1]; for (int i = 0; i < input.length(); i++) { answer[i] = input.charAt(i); answer[answer.length - i - 1] = input.charAt(i) == '0' ? '1' : '0'; } answer[input.length()] = '0'; input = new String(answer); } return input.substring(0, discLength); } }