Guest User

Untitled

a guest
Dec 16th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. import java.util.*;
  2. import static java.lang.Integer.parseInt;
  3. /**
  4.  * @author /u/Philboyd_Studge on 12/15/2015.
  5.  */
  6. public class Advent16 {
  7.     Map<String, Integer> MFCSAM = new HashMap<>();
  8.     String[] k = { "children", "cats", "samoyeds", "pomeranians", "akitas", "vizslas",
  9.             "goldfish", "trees", "cars", "perfumes"};
  10.     int[] v = { 3, 7, 2, 3, 0, 0, 5, 3, 2, 1};
  11.  
  12.     boolean part1 = true;
  13.  
  14.     public Advent16() {
  15.         for (int i = 0; i < 10; i++) {
  16.             MFCSAM.put(k[i], v[i]);
  17.         }
  18.     }
  19.  
  20.     public int find(List<Sue> aunts) {
  21.  
  22.         for (Sue each : aunts) {
  23.             int found = 0;
  24.             for (int i = 0; i < 3; i++) {
  25.                 if (part1) {
  26.                     if (MFCSAM.get(each.attributes[i])!=each.values[i]) {
  27.                         break;
  28.                     } else {
  29.                         found++;
  30.                     }
  31.                 } else {
  32.                     switch (each.attributes[i]) {
  33.                         case "cats" : case "trees" :
  34.                             if (MFCSAM.get(each.attributes[i]) >= each.values[i]) {
  35.                                 break;
  36.                             } else {
  37.                                 found++;
  38.                             }
  39.                             break;
  40.                         case "pomeranians" :case "goldfish" :
  41.                             if (MFCSAM.get(each.attributes[i]) <= each.values[i]) {
  42.                                 break;
  43.                             } else {
  44.                                 found++;
  45.                             }
  46.                             break;
  47.                         default :
  48.                             if (MFCSAM.get(each.attributes[i])!=each.values[i]) {
  49.                                 break;
  50.                             } else {
  51.                                 found++;
  52.                             }
  53.                     }
  54.                 }
  55.             }
  56.             if (found==3) return each.number;
  57.         }
  58.         return -1;
  59.     }
  60.  
  61.     static class Sue {
  62.         int number;
  63.         String[] attributes;
  64.         int[] values;
  65.  
  66.         public Sue(int number, String[] attributes, int[] values) {
  67.             this.number = number;
  68.             this.attributes = attributes;
  69.             this.values = values;
  70.         }
  71.  
  72.         @Override
  73.         public String toString() {
  74.             return "Sue{" +
  75.                     "number=" + number +
  76.                     ", attributes=" + Arrays.toString(attributes) +
  77.                     ", values=" + Arrays.toString(values) +
  78.                     '}';
  79.         }
  80.     }
  81.  
  82.  
  83.  
  84.     public static void main(String[] args) {
  85.  
  86.         Advent16 adv = new Advent16();
  87.         List<String[]> input = FileIO.getFileLinesSplit("advent16.txt","[,: ]+");
  88.  
  89.         List<Sue> aunts = new ArrayList<>();
  90.  
  91.         for (String[] each : input) {
  92.             int number = parseInt(each[1]);
  93.             String[] attributes = new String[3];
  94.             int[] values = new int[3];
  95.             for (int i = 2; i < each.length; i++) {
  96.                 if ((i & 1) == 0) {
  97.                     attributes[(i/2)-1] = each[i];
  98.                 } else {
  99.                     values[(i/2)-1] = parseInt(each[i]);
  100.                 }
  101.             }
  102.             aunts.add(new Sue(number, attributes, values));
  103.         }
  104.  
  105.         System.out.println(adv.find(aunts));
  106.  
  107.  
  108.  
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment