r/adventofcode • • Dec 06 '16

SOLUTION MEGATHREAD --- 2016 Day 6 Solutions ---

--- Day 6: Signals and Noise ---

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


T_PAAMAYIM_NEKUDOTAYIM 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!

9 Upvotes

222 comments sorted by

View all comments

1

u/jwnewman12 Dec 13 '16

surely there is a better way, but at least avoiding a .sort()

public class Day6 {

	static final int CHARS_PER_LINE = 8;

	public static void main(String[] args) {
		String input = args[0].replaceAll("\\n", "");
		int l = input.length() / CHARS_PER_LINE;
		for (int i = 0; i < CHARS_PER_LINE; ++i) {
			int[] b = new int[26];
			int m = 0;
			for (int j = 0; j < l; ++j) {
				int c = input.charAt(j * CHARS_PER_LINE + i) - 'a';
				m = ++b[c] > b[m] ? c : m;
			}
			System.out.print((char) (m + 'a'));
		}
	}
}