Skip to content

Instantly share code, notes, and snippets.

@roccodev
Last active December 8, 2017 10:15
Show Gist options
  • Select an option

  • Save roccodev/4796e1d7c2fbf452a70c192b8d33aaae to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/4796e1d7c2fbf452a70c192b8d33aaae to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day8
private static HashMap<String, Integer> tings = new HashMap<String, Integer>();
public static void main(String[] args) throws IOException {
List<String> bloccs = Files.readAllLines(Paths.get(new File("res/day8").getPath())).stream()
.collect(Collectors.toList());
for(String s : bloccs) {
String[] data = s.split(" ");
String index = data[0];
String operation = data[1];
String factor = data[2];
String condIndex = data[4];
String condOperator = data[5];
String condFactor = data[6];
if(!tings.containsKey(index)) {
tings.put(index, 0);
}
if(!tings.containsKey(condIndex)) {
tings.put(condIndex, 0);
}
if(shouldApply(condIndex, condOperator, condFactor)) {
applyWithOperator(index, operation, factor);
}
}
System.out.println(Collections.max(tings.values()));
}
private static void applyWithOperator(String index, String operator, String factor) {
int before = tings.get(index);
switch(operator) {
case "inc":
update(index, before + Integer.parseInt(factor));
break;
case "dec":
update(index, before - Integer.parseInt(factor));
break;
}
}
private static boolean shouldApply(String conditionIndex, String condOperator, String factor) {
int toCheck = tings.get(conditionIndex);
int factorI = Integer.parseInt(factor);
switch(condOperator) {
case ">":
return toCheck > factorI;
case ">=":
return toCheck >= factorI;
case "==":
return toCheck == factorI;
case "!=":
return toCheck != factorI;
case "<":
return toCheck < factorI;
case "<=":
return toCheck <= factorI;
}
return false;
}
private static void update(String index, int value) {
tings.remove(index);
tings.put(index, value);
}
private static HashMap<String, Integer> tings = new HashMap<String, Integer>();
private static int highest = 0;
public static void main(String[] args) throws IOException {
List<String> bloccs = Files.readAllLines(Paths.get(new File("res/day8").getPath())).stream()
.collect(Collectors.toList());
for(String s : bloccs) {
String[] data = s.split(" ");
String index = data[0];
String operation = data[1];
String factor = data[2];
String condIndex = data[4];
String condOperator = data[5];
String condFactor = data[6];
if(!tings.containsKey(index)) {
tings.put(index, 0);
}
if(!tings.containsKey(condIndex)) {
tings.put(condIndex, 0);
}
if(shouldApply(condIndex, condOperator, condFactor)) {
applyWithOperator(index, operation, factor);
}
}
System.out.println(highest);
}
private static void applyWithOperator(String index, String operator, String factor) {
int before = tings.get(index);
switch(operator) {
case "inc":
update(index, before + Integer.parseInt(factor));
break;
case "dec":
update(index, before - Integer.parseInt(factor));
break;
}
}
private static boolean shouldApply(String conditionIndex, String condOperator, String factor) {
int toCheck = tings.get(conditionIndex);
int factorI = Integer.parseInt(factor);
switch(condOperator) {
case ">":
return toCheck > factorI;
case ">=":
return toCheck >= factorI;
case "==":
return toCheck == factorI;
case "!=":
return toCheck != factorI;
case "<":
return toCheck < factorI;
case "<=":
return toCheck <= factorI;
}
return false;
}
private static void update(String index, int value) {
tings.remove(index);
if(value > highest) highest = value;
tings.put(index, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment