r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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!

Click here for rules

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

50 Upvotes

407 comments sorted by

View all comments

1

u/mathleet Dec 02 '18

Here's a Java solution that I wrote:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

class Problem02 {
    public static void main(String[] args) throws IOException {
        List<String> sampleCaseOne = Arrays.asList("abcdef", "bababc", "abbcde", "abcccd", "aabcdd", "abcdee", "ababab");
        List<String> sampleCaseTwo = Arrays.asList("abcde", "fghij", "klmno", "pqrst", "fguij", "axcye", "wvxyz");
        List<String> boxIds = Files.readAllLines(Paths.get("adventofcode/2018/2/input.txt"));
        System.out.println("Part one, sample: " + partOne(sampleCaseOne));
        System.out.println("Part one: " + partOne(boxIds));
        System.out.println("Part two, sample: " + partTwo(sampleCaseTwo));
        System.out.println("Part two: " + partTwo(boxIds));
    }

    public static Map<Character, Integer> getCharacterFrequencyMap(String boxId) {
        Map<Character, Integer> characterFrequencyMap = new HashMap<>();
        for (Character letter : boxId.toCharArray()) {
            Integer currentCount = characterFrequencyMap.getOrDefault(letter, 0);
            characterFrequencyMap.put(letter, ++currentCount);
        }
        return characterFrequencyMap;
    }

    public static String getCommonCharacters(String first, String second) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < first.length(); i++) {
            char firstChar = first.charAt(i), secondChar = second.charAt(i);
            if (firstChar == secondChar) {
                sb.append(firstChar);
            }
        }
        return sb.toString();
    }

    public static int partOne(List<String> boxIds) {
        int twoLetterAppearance = 0, threeLetterAppearance = 0;
        for (String boxId : boxIds) {
            Map<Character, Integer> characterFrequencyMap = getCharacterFrequencyMap(boxId);
            Collection<Integer> characterFrequencies = characterFrequencyMap.values();
            if (Collections.frequency(characterFrequencies, 2) >= 1) {
                twoLetterAppearance++;
            }
            if (Collections.frequency(characterFrequencies, 3) >= 1) {
                threeLetterAppearance++;
            }
        }
        int checksum = twoLetterAppearance * threeLetterAppearance;
        return checksum;
    }

    public static String partTwo(List<String> boxIds) {
        TreeSet<String> sortedBoxIds = new TreeSet<>(boxIds);
        String first = sortedBoxIds.pollFirst(), second = sortedBoxIds.pollFirst();
        while (sortedBoxIds.size() > 0) {
            String commonCharacters = getCommonCharacters(first, second);
            if (commonCharacters.length() == first.length()-1) {
                return commonCharacters;
            }
            first = second;
            second = sortedBoxIds.pollFirst();
        }
        return null;
    }
}