Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. public class Day2 extends AdventOfCode {
  2.  
  3.     public Day2(List<String> input) {
  4.         super(input);
  5.         title = "Inventory Management System";
  6.         part1Description = "Checksum: ";
  7.         part2Description = "Common letters: ";
  8.     }
  9.  
  10.     @Override
  11.     public Object part1() {
  12.         int twos = 0;
  13.         int threes = 0;
  14.         for (String each : input) {
  15.             Map<Character, Integer> freq = new HashMap<>();
  16.             boolean found2 = false;
  17.             boolean found3 = false;
  18.             for (int i = 0; i < each.length(); i++) {
  19.                 char c = each.charAt(i);
  20.                 freq.put(c, freq.getOrDefault(c, 0) + 1);
  21.             }
  22.             for (Map.Entry<Character, Integer> dict : freq.entrySet()) {
  23.  
  24.                 if (!found2 && dict.getValue() == 2) {
  25.                     twos++;
  26.                     found2 = true;
  27.                 }
  28.                 if (!found3 && dict.getValue() == 3) {
  29.                     threes++;
  30.                     found3 = true;
  31.                 }
  32.             }
  33.         }
  34.  
  35.  
  36.         return twos * threes;
  37.     }
  38.  
  39.     @Override
  40.     public Object part2() {
  41.         for (int i = 0; i < input.size() - 1; i++) {
  42.             for (int j = 1; j < input.size(); j++) {
  43.                 int dist = EditDistance.calculate(input.get(i), input.get(j));
  44.                 if (dist == 1) {
  45.                     return removeDiff(input.get(i), input.get(j));
  46.                 }
  47.             }
  48.         }
  49.         return "";
  50.     }
  51.  
  52.     private String removeDiff(String a, String b) {
  53.         String result = "";
  54.         for (int i = 0; i < a.length(); i++) {
  55.             if (a.charAt(i) == b.charAt(i)) {
  56.                 result += a.charAt(i);
  57.             }
  58.         }
  59.         return result;
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement