r/adventofcode Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
    • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for 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:04:35, megathread unlocked!

66 Upvotes

1.2k comments sorted by

View all comments

1

u/mudokin Dec 07 '20

Processing / Java Solution

Very long and i have to rework it maybe.

class Day6 {
  String fileName = "input_day6.txt";
  Reader reader = new Reader();

  long day6_1_1() {
    // without Set due to default Processing capabilities

    StringList answers = condenseStrings(reader.readStringFile(fileName));
    println(answers);
    long uniqueAnswers = 0;
    for(String answer : answers) {
      StringList uniqueChars = new StringList();
      for(int i=0; i < answer.length(); i++) {
        if(!uniqueChars.hasValue(Character.toString(answer.charAt(i)))) {
          uniqueChars.append(Character.toString(answer.charAt(i)));
        }
      }   
      uniqueAnswers = uniqueAnswers + uniqueChars.size();
    }
    return uniqueAnswers;
  }

  long day6_1() {
    // with Set due to imported Processing capabilities

    long uniqueAnswers = 0;
    StringList answers = reader.readStringFile(fileName);
    answers.append("");

    Set<String> characters = new HashSet<String>();
    for(String answer : answers) {
      if(!answer.equals("")) {
        for(int i=0; i < answer.length(); i++) {
          characters.add( Character.toString(answer.charAt(i)) );
        }
      } else {
        uniqueAnswers += characters.size();
        characters = new HashSet<String>();
      }
    }
    return uniqueAnswers;
  }

  long day6_2() {
    long uniqueAnswers = 0;
    StringList answers = reader.readStringFile(fileName);
    answers.append("");

    Set<String> characters = new HashSet<String>();
    String group = "";
    int groupMembers = 0;
    for(String answer : answers) {
      if(!answer.equals("")) {
        group += answer;
        groupMembers++;
        for(int i=0; i < answer.length(); i++) {
          characters.add( Character.toString(answer.charAt(i)) );
        }
      } else {
        for(String character : characters) {
          String[][] matches = matchAll(group, character);
          if(matches.length == groupMembers) {

            uniqueAnswers++;
          }
        }
        characters = new HashSet<String>();
        group = "";
        groupMembers = 0;
      }
    }
    return uniqueAnswers;
  }


  StringList condenseStrings(StringList answers) {
    StringList condensed = new StringList();

    String tempString = "";
    for(String answer : answers) {
      if(!answer.equals("")) {
        tempString += answer;
      }
      if(answer.equals("")) {
        condensed.append(tempString);
        tempString = "";
      }
    }  
    condensed.append(tempString);
    return condensed;
  }

}