# HG changeset patch # User Scoopta # Date 1607247900 28800 # Sun Dec 06 01:45:00 2020 -0800 # Node ID 8a6d7cc0391045340331a8f8c4659e611811957e # Parent b77010af0557e6a4614970a5bcadc394d6beb7cf Day 6 diff --git a/src/ninja/scoopta/software/advent_of_code_12020/Day6.java b/src/ninja/scoopta/software/advent_of_code_12020/Day6.java new file mode 100644 --- /dev/null +++ b/src/ninja/scoopta/software/advent_of_code_12020/Day6.java @@ -0,0 +1,70 @@ +package ninja.scoopta.software.advent_of_code_12020; + +import java.util.*; +import java.nio.file.*; +import java.io.IOException; + +final class Day6 { + public static final void main(String[] args) { + try { + List lines = Files.readAllLines(Paths.get("day6.txt")); + System.out.println("Part 1"); + pt1(lines); + System.out.println("Part 2"); + pt2(lines); + } catch(IOException e) { + e.printStackTrace(); + } + } + + private static final void pt1(List lines) { + TreeSet answers = new TreeSet<>(); + int yeses = 0; + for(String line : lines) { + if(line.equals("")) { + yeses += answers.size(); + answers.clear(); + continue; + } + for(char chr : line.toCharArray()) { + answers.add(chr); + } + } + yeses += answers.size(); + System.out.println(yeses); + } + + private static final void pt2(List lines) { + TreeMap answers = new TreeMap<>(); + + int people = 0; + int yeses = 0; + for(String line : lines) { + if(line.equals("")) { + for(int count : answers.values()) { + if(count == people) { + ++yeses; + } + } + people = 0; + answers.clear(); + continue; + } + ++people; + for(char chr : line.toCharArray()) { + Integer count = answers.get(chr); + if(count != null) { + answers.put(chr, ++count); + } else { + answers.put(chr, 1); + } + } + } + for(int count : answers.values()) { + if(count == people) { + ++yeses; + } + } + System.out.println(yeses); + } +}