Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.stream.Collectors;
  8.  
  9. import org.apache.commons.lang3.StringUtils;
  10.  
  11. import com.google.common.collect.Lists;
  12. import com.google.common.collect.Maps;
  13.  
  14. public class Day4 {
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         Path path = Paths.get("src/main/java/input4.txt");
  18.         List<String> input = Files.readAllLines(path).stream()
  19.                 .map(StringUtils::stripToEmpty)
  20.                 .collect(Collectors.toList());
  21.  
  22.         problem1(input);
  23.         problem2(input);
  24.     }
  25.  
  26.     public static class Chars implements Comparable<Chars> {
  27.         char ch = 0;
  28.         long times = 0;
  29.  
  30.         Chars(char ch, long times) {
  31.             this.ch = ch;
  32.             this.times = times;
  33.         }
  34.  
  35.         @Override
  36.         public int compareTo(Chars o) {
  37.             if (this.times == o.times) {
  38.                 return Character.compare(ch, o.ch);
  39.             }
  40.  
  41.             return -Long.compare(this.times, o.times);
  42.         }
  43.     }
  44.  
  45.     public static void problem1(List<String> input) {
  46.         long sum = 0;
  47.  
  48.         for (String line : input) {
  49.             String[] values = line.replaceAll("-|\\[|\\]", " ").split(" ");
  50.             int sector = Integer.valueOf(values[values.length - 2]);
  51.             String checksum = values[values.length - 1];
  52.  
  53.             Map<Character, Chars> totals = Maps.newLinkedHashMap();
  54.             for (int i = 0; i < values.length - 2; i++) {
  55.                 String value = values[i];
  56.                 for (char ch : value.toCharArray()) {
  57.                     Chars chars = totals.get(ch);
  58.                     if (chars == null) {
  59.                         chars = new Chars(ch, 0);
  60.                         totals.put(ch, chars);
  61.                     }
  62.                     chars.times++;
  63.                 }
  64.             }
  65.  
  66.             List<Chars> chars = Lists.newArrayList(totals.values());
  67.             chars.sort(null);
  68.  
  69.             boolean valid = true;
  70.             for (int i = 0; i < checksum.length(); i++) {
  71.                 Chars ch = chars.get(i);
  72.                 if (!checksum.contains(String.valueOf(ch.ch))) {
  73.                     valid = false;
  74.                     break;
  75.                 }
  76.             }
  77.  
  78.             if (valid) sum += sector;
  79.  
  80.         }
  81.  
  82.         System.out.println(sum);
  83.     }
  84.  
  85.     public static void problem2(List<String> input) {
  86.  
  87.         for (String line : input) {
  88.             String[] values = line.replaceAll("-|\\[|\\]", " ").split(" ");
  89.             int sector = Integer.valueOf(values[values.length - 2]);
  90.  
  91.             String translatedMessage = "";
  92.             for (int i = 0; i < values.length - 2; i++) {
  93.                 String value = values[i];
  94.                 for (char ch : value.toCharArray()) {
  95.                     ch = (char) ((ch - 'a' + sector) % 26 + 'a');
  96.                     translatedMessage += ch;
  97.                 }
  98.                 translatedMessage += " ";
  99.             }
  100.             if (translatedMessage.contains("object"))
  101.                 System.out.println(translatedMessage + " - " + sector);
  102.         }
  103.  
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement