Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Last active December 16, 2015 08:18
Show Gist options
  • Save desrtfx/40e4424bdaffd26ea02f to your computer and use it in GitHub Desktop.
Save desrtfx/40e4424bdaffd26ea02f to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day16 {
private final static String FILENAME = ".\\InputDay16.txt";
private final static Pattern SUE_NUMBER = Pattern.compile("Sue (\\d+):");
private final static Pattern DATA = Pattern
.compile("(?<item>.+): (?<count>\\d+)");
private List<Sue> sues = new ArrayList<>();
private Sue target;
class Sue {
int number;
Map<String, Integer> properties = new HashMap<>();
public Sue(String s) {
int pos = s.indexOf(":"); // get the position of the first ":"
String tmp = s.substring(0, pos + 1); // capture first part
// including the ":"
Matcher m = SUE_NUMBER.matcher(tmp);
if (m.matches()) {
number = Integer.parseInt(m.group(1));
}
tmp = s.substring(pos + 2); // remaining String
String[] values = tmp.split(", "); // split on ", "
for (String p : values) {
m = DATA.matcher(p);
if (m.matches()) {
String item = m.group("item");
int count = Integer.parseInt(m.group("count"));
properties.put(item, count);
}
}
}
public double percentMatchRange(Sue other, boolean part1) {
double score = 0d;
double step = (100.0 / other.properties.size()); // percental
// increment per
// matched
// property
for (Map.Entry<String, Integer> e : properties.entrySet()) {
String s = e.getKey();
int v = e.getValue();
if (part1) {
if (other.properties.get(s) == v) {
score += step;
}
} else {
if ("cats trees".contains(s)) {
if (other.properties.get(s) < v) {
score += step;
}
} else {
if ("pomeranians goldfish".contains(s)) {
if (other.properties.get(s) > v) {
score += step;
}
} else {
if (other.properties.get(s) == v) {
score += step;
}
}
}
}
}
return score;
}
}
public Day16() {
List<String> data = FileIO.getFileAsList(FILENAME);
for (String s : data) {
Sue sue = new Sue(s);
sues.add(sue);
}
String tmp = "Sue 0: children: 3, cats: 7, samoyeds: 2, pomeranians: 3, akitas: 0, vizslas: 0, goldfish: 5, trees: 3, cars: 2, perfumes: 1";
target = new Sue(tmp);
}
public static void main(String[] args) {
Day16 day16 = new Day16();
day16.solvePart1();
day16.solvePart2();
}
public void solvePart1() {
int sueNumber = solve(true);
System.out
.println("Part 1: The Sue with the highest matching score is number: "
+ sueNumber);
}
public void solvePart2() {
int sueNumber = solve(false);
System.out
.println("Part 2: The Sue with the highest matching score is number: "
+ sueNumber);
}
public int solve(boolean part1) {
double maxScore = Double.MIN_VALUE;
int sueNumber = Integer.MIN_VALUE;
for (Sue sue : sues) {
double match = sue.percentMatchRange(target, part1);
if (maxScore < match) {
maxScore = match;
sueNumber = sue.number;
}
}
return sueNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment