Advertisement
tterrag1098

Untitled

Dec 4th, 2016
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package com.tterrag.advent2016;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import java.util.stream.Collectors;
  13.  
  14. import lombok.Getter;
  15. import lombok.ToString;
  16.  
  17. public class Day04 {
  18.  
  19.     @ToString
  20.     private static class Key {
  21.        
  22.         private static final Pattern PATTERN = Pattern.compile("((?:[a-z]+-)+)([0-9]+)\\[([a-z]+)\\]");
  23.  
  24.         private String input;
  25.         private char[] checksum;
  26.         private @Getter int key;
  27.         private Map<Character, Integer> counts = new HashMap<>();
  28.        
  29.         public Key(String newInput, Key old) {
  30.             this.input = newInput;
  31.             this.checksum = old.checksum;
  32.             this.key = old.key;
  33.             this.counts = old.counts;
  34.         }
  35.        
  36.         public Key(String in) {            
  37.             Matcher matcher = PATTERN.matcher(in);
  38.             matcher.find();
  39.             this.input = matcher.group(1).replaceAll("-$", "");
  40.             input.replace("-", "").chars().forEach(c->counts.compute((char)c, (k, v) -> v == null ? 1 : v + 1));
  41.             key = Integer.parseInt(matcher.group(2));
  42.             checksum = matcher.group(3).toCharArray();
  43.         }
  44.        
  45.         public boolean isValid() {
  46.             List<Entry<Character, Integer>> sortedEntries = counts.entrySet().stream().sorted((e1, e2) -> e1.getValue().equals(e2.getValue()) ? Character.compare(e1.getKey(), e2.getKey()) : -Integer.compare(e1.getValue(), e2.getValue())).collect(Collectors.toList());
  47.             for (int i = 0; i < checksum.length; i++) {
  48.                 if (sortedEntries.get(i).getKey() != checksum[i]) return false;
  49.             }
  50.             return true;
  51.         }
  52.     }
  53.  
  54.     public static void main(String[] args) throws IOException {
  55.         List<String> lines = Files.readAllLines(Paths.get("day4.txt"));
  56.         List<Key> validKeys = lines.stream().map(Key::new).filter(Key::isValid).collect(Collectors.toList());
  57.         System.out.println("Part 1 Sum: " + validKeys.stream().mapToInt(Key::getKey).sum());
  58.         System.out.println("Part 2 ID: "  + validKeys.stream().map(k -> new Key(k.input.chars().map(c -> c == '-' ? ' ' : (((c - 'a') + k.key) % 26) + 'a').mapToObj(i -> "" + Character.valueOf((char) i)).collect(Collectors.joining()), k)).filter(key -> key.input.equals("northpole object storage")).findFirst().get().key);
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement