Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1.     public static void problem1(List<String> input) {
  2.         long sum = 0;
  3.  
  4.         for (String line : input) {
  5.             String[] values = line.replaceAll("-|\\[|\\]", " ").split(" ");
  6.             int sector = Integer.valueOf(values[values.length - 2]);
  7.             String checksum = values[values.length - 1];
  8.  
  9.             Map<Character, Chars> totals = Maps.newLinkedHashMap();
  10.             for (int i = 0; i < values.length - 2; i++) {
  11.                 String value = values[i];
  12.                 for (char ch : value.toCharArray()) {
  13.                     Chars chars = totals.get(ch);
  14.                     if (chars == null) {
  15.                         chars = new Chars(ch, 0);
  16.                         totals.put(ch, chars);
  17.                     }
  18.                     chars.times++;
  19.                 }
  20.             }
  21.  
  22.             List<Chars> chars = Lists.newArrayList(totals.values());
  23.             chars.sort(null);
  24.  
  25.             boolean valid = true;
  26.             for (int i = 0; i < checksum.length(); i++) {
  27.                 Chars ch = chars.get(i);
  28.                 if (!checksum.contains(String.valueOf(ch.ch))) {
  29.                     valid = false;
  30.                     break;
  31.                 }
  32.             }
  33.  
  34.             if (valid) sum += sector;
  35.  
  36.         }
  37.  
  38.         System.out.println(sum);
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement