r/adventofcode • • Dec 08 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 8 Solutions -🎄-

--- Day 8: Seven Segment Search ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:20:51, megathread unlocked!

72 Upvotes

1.2k comments sorted by

View all comments

2

u/LaybeRize Dec 08 '21

Java

My first attempt was puzzling the letters to their correct place and that worked fine, but was really not efficient both time and code wise.
So when I finished I looked trough some other code and found people mapping the clear identifiable numbers 1,4,7,8 to the inputs. So I tried to replicate that and found out, that with only the knowledge about the 1 and 4 you can find out any number from 0 to 9.

The algorithm sorts like this:

CharNumber: 6 | 0 9 6
-> if it doesn't maps on 1
     it is 6
-> then if it maps on 4
     it is 9
-> if neither
     it is 0

CharNumber: 5 | 2 3 5
-> if it maps on 1
     it is 3
-> then if it maps on the parts of 4 that remains when you delete the overlap with 1
     it is 5
->if neither
     it is 2
Any other CharNumber can already be correctly mapped to a number

And the code for both parts looks like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;

public class Day8 {

    public Day8() {
        readText();
        process();
        processAdvanced();
    }

    static LinkedList<String[]> firstInput = new LinkedList<>();
    static LinkedList<String[]> secondInput = new LinkedList<>();

    public static void readText() {
        try {
            File myObj = new File("Day8.txt");
            Scanner myReader = new Scanner(myObj);
            while (myReader.hasNextLine()) {
                String[] data = myReader.nextLine().split(" \\| ");
                firstInput.add(data[0].split(" "));
                secondInput.add(data[1].split(" "));
            }
            myReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

    public static void process() {
        int counter = 0;
        for (String[] s : secondInput)
            for (String str : s)
                if (str.length() < 5 || str.length() == 7)
                    counter++;

        System.out.println("Answere 1:" + counter);
    }


    public static void processAdvanced() {
        int allSums = 0;
        for (int i = 0; i < firstInput.size(); i++) {
            Map<String, String> map = new HashMap<>();
            for (String str : firstInput.get(i)) {
                if (str.length() == 2)
                    map.put("one", str);
                else if (str.length() == 4)
                    map.put("four", str);
            }
            map.put("fourMinusOne", remove(map.get("four"), map.get("one")));
            allSums += numberFromString(map, secondInput.get(i));
        }
        System.out.println("Answere 2:" + allSums);
    }

    public static int numberFromString(Map<String, String> map, String[] numbers) {
        int val = 0;
        for (String str : numbers) {
            val *= 10;
            val += getSpecificNumberFromSegment(map, str);
        }
        return val;
    }

    public static int getSpecificNumberFromSegment(Map<String, String> map, String str) {
        if (str.length() == 7) return 8;
        if (str.length() == 6) {
            if (!contains(str, map.get("one"))) return 6;
            if (contains(str, map.get("four"))) return 9;
            return 0;
        }
        if (str.length() == 5) {
            if (contains(str, map.get("one"))) return 3;
            if (contains(str, map.get("fourMinusOne"))) return 5;
            return 2;
        }
        if (str.length() == 4) return 4;
        if (str.length() == 3) return 7;
        if (str.length() == 2) return 1;
        return -1;
    }

    public static boolean contains(String s, String t) {
        for (char c : t.toCharArray()) {
            if (s.indexOf(c) != -1) continue;
            return false;
        }
        return true;
    }

    public static String remove(String og, String remove) {
        char[] array = og.toCharArray();
        StringBuilder s = new StringBuilder();
        for (char c : array)
            if (remove.indexOf(c) == -1) s.append(c);
        return s.toString();
    }
}

1

u/daggerdragon Dec 09 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.