Created
December 7, 2015 18:12
-
-
Save xathien/bf72ef70758a2c7c3b88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AdventOfCode7 | |
{ | |
static class Command | |
{ | |
enum CommandType | |
{ | |
and, | |
or, | |
rshift, | |
lshift, | |
not, | |
set; | |
static CommandType herp(String derp) | |
{ | |
return CommandType.valueOf(derp.toLowerCase()); | |
} | |
} | |
CommandType type; | |
String target; | |
String left; | |
String right; | |
Command(CommandType type, String target, String left, String right) | |
{ | |
this.type = type; | |
this.target = target; | |
this.left = left; | |
this.right = right; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Command<" + type + ", " + left + ", " + right + " -> " + target + ">"; | |
} | |
} | |
public static void main(String[] args) | |
{ | |
Map<String, Integer> wireValues = new HashMap<>(); | |
Map<String, Command> wireCommands = new HashMap<>(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/deleteMe"))); | |
String line; | |
while ((line = reader.readLine()) != null) | |
{ | |
String[] tokens = line.split(" "); | |
String target = tokens[tokens.length-1]; | |
Command command; | |
switch(tokens.length) | |
{ | |
case 3: | |
command = new Command(CommandType.set, target, tokens[0], null); | |
break; | |
case 4: | |
command = new Command(CommandType.not, target, tokens[1], null); | |
break; | |
default: | |
CommandType type = CommandType.herp(tokens[1]); | |
command = new Command(type, target, tokens[0], tokens[2]); | |
break; | |
} | |
wireCommands.put(target, command); | |
} | |
wireValues.put("b", 16076); // Only change for Part 2 | |
Command start = wireCommands.get("a"); | |
int value = evaluate(start, wireCommands, wireValues); | |
System.out.println("a: " + value); | |
} | |
int uShort = 0xFFFF; | |
private int evaluate(Command current, final Map<String, Command> wireCommands, final Map<String, Integer> wireValues) | |
{ | |
if (wireValues.containsKey(current.target)) | |
return wireValues.get(current.target); | |
System.out.println("Evaluating " + current); | |
int value; | |
switch (current.type) | |
{ | |
case set: | |
value = evalLeft(current, wireCommands, wireValues); | |
break; | |
case not: | |
value = ~evalLeft(current, wireCommands, wireValues) & uShort; | |
break; | |
case and: | |
value = evalLeft(current, wireCommands, wireValues) & evalRight(current, wireCommands, wireValues); | |
break; | |
case or: | |
value = evalLeft(current, wireCommands, wireValues) | evalRight(current, wireCommands, wireValues); | |
break; | |
case lshift: | |
value = (evalLeft(current, wireCommands, wireValues) << evalRight(current, wireCommands, wireValues)) & uShort; | |
break; | |
case rshift: | |
value = (evalLeft(current, wireCommands, wireValues) >>> evalRight(current, wireCommands, wireValues)) & uShort; | |
break; | |
default: | |
throw new IllegalArgumentException("herp"); | |
} | |
wireValues.put(current.target, value); | |
System.out.println("Wire " + current.target + " value " + value); | |
return value; | |
} | |
int evalLeft(Command current, final Map<String, Command> wireCommands, final Map<String, Integer> wireValues) | |
{ | |
int value; | |
try | |
{ | |
value = parseInt(current.left) & uShort; | |
} | |
catch (NumberFormatException ex) | |
{ | |
value = evaluate(wireCommands.get(current.left), wireCommands, wireValues); | |
} | |
return value; | |
} | |
int evalRight(Command current, final Map<String, Command> wireCommands, final Map<String, Integer> wireValues) | |
{ | |
int value; | |
try | |
{ | |
value = parseInt(current.right) & uShort; | |
} | |
catch (NumberFormatException ex) | |
{ | |
value = evaluate(wireCommands.get(current.right), wireCommands, wireValues); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment