Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import static java.lang.Integer.parseInt;
- /**
- * @author /u/Philboyd_Studge on 12/15/2015.
- */
- public class Advent16 {
- Map<String, Integer> MFCSAM = new HashMap<>();
- String[] k = { "children", "cats", "samoyeds", "pomeranians", "akitas", "vizslas",
- "goldfish", "trees", "cars", "perfumes"};
- int[] v = { 3, 7, 2, 3, 0, 0, 5, 3, 2, 1};
- boolean part1 = true;
- public Advent16() {
- for (int i = 0; i < 10; i++) {
- MFCSAM.put(k[i], v[i]);
- }
- }
- public int find(List<Sue> aunts) {
- for (Sue each : aunts) {
- int found = 0;
- for (int i = 0; i < 3; i++) {
- if (part1) {
- if (MFCSAM.get(each.attributes[i])!=each.values[i]) {
- break;
- } else {
- found++;
- }
- } else {
- switch (each.attributes[i]) {
- case "cats" : case "trees" :
- if (MFCSAM.get(each.attributes[i]) >= each.values[i]) {
- break;
- } else {
- found++;
- }
- break;
- case "pomeranians" :case "goldfish" :
- if (MFCSAM.get(each.attributes[i]) <= each.values[i]) {
- break;
- } else {
- found++;
- }
- break;
- default :
- if (MFCSAM.get(each.attributes[i])!=each.values[i]) {
- break;
- } else {
- found++;
- }
- }
- }
- }
- if (found==3) return each.number;
- }
- return -1;
- }
- static class Sue {
- int number;
- String[] attributes;
- int[] values;
- public Sue(int number, String[] attributes, int[] values) {
- this.number = number;
- this.attributes = attributes;
- this.values = values;
- }
- @Override
- public String toString() {
- return "Sue{" +
- "number=" + number +
- ", attributes=" + Arrays.toString(attributes) +
- ", values=" + Arrays.toString(values) +
- '}';
- }
- }
- public static void main(String[] args) {
- Advent16 adv = new Advent16();
- List<String[]> input = FileIO.getFileLinesSplit("advent16.txt","[,: ]+");
- List<Sue> aunts = new ArrayList<>();
- for (String[] each : input) {
- int number = parseInt(each[1]);
- String[] attributes = new String[3];
- int[] values = new int[3];
- for (int i = 2; i < each.length; i++) {
- if ((i & 1) == 0) {
- attributes[(i/2)-1] = each[i];
- } else {
- values[(i/2)-1] = parseInt(each[i]);
- }
- }
- aunts.add(new Sue(number, attributes, values));
- }
- System.out.println(adv.find(aunts));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment