Created
December 8, 2015 03:18
-
-
Save snarkbait/01938702f8f9e1fd2264 to your computer and use it in GitHub Desktop.
Advent of Code Day 7
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
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.IntBinaryOperator; | |
/** | |
* @author /u/Philboyd_Studge on 12/6/2015. | |
*/ | |
public class Advent7 { | |
enum Gate { | |
AND( (x, y) -> ( x & y)), | |
OR ( (x, y) -> (x | y)), | |
NOT ( (x, y) -> ( ~y & 0xffff) ), | |
RSHIFT ( (x, y) -> (x >> y)), | |
LSHIFT ( (x, y) -> (x << y)), | |
EQUALS( (x, y) -> (x)); | |
private final IntBinaryOperator func; | |
Gate(IntBinaryOperator func) { | |
this.func = func; | |
} | |
} | |
static Map<String, Wire> wires = new HashMap<>(); | |
static int count; | |
static boolean part1 = true; // false for part 2 | |
public static Wire findWire(String name) { | |
return wires.getOrDefault(name, new Wire(name)); | |
} | |
public static void process() { | |
Wire root = findWire("a"); | |
while(root.value <=0) { | |
process(root); | |
System.out.println("Iteration: " + (++count)); | |
} | |
System.out.println(root.value); | |
} | |
public static void process(Wire node) { | |
if (node==null) return; | |
if (node.input==null) { | |
return; | |
} | |
if (node.input.isComplete()) { | |
node.input.apply(node); | |
} else { | |
process(node.input.left); | |
process(node.input.right); | |
} | |
} | |
public static void init(List<String[]> inList) { | |
for (String[] each : inList) { | |
Gate operator = null; | |
Wire result = null; | |
Wire left = null, right = null; | |
switch (each.length) { | |
case 3 : | |
if (Character.isDigit(each[0].charAt(0))) { | |
left = new Wire("",Integer.parseInt(each[0])); | |
} else { | |
left = findWire(each[0]); | |
} | |
result = findWire(each[2]); | |
// horrible hack | |
if (!part1){ | |
if (result.name.equals("b")) left.value = 16076; | |
} | |
operator = Gate.EQUALS; | |
break; | |
case 4 : | |
right = findWire(each[1]); | |
result = findWire(each[3]); | |
operator = Gate.NOT; | |
break; | |
case 5 : | |
if (Character.isDigit(each[0].charAt(0))) { | |
left = new Wire("",Integer.parseInt(each[0])); | |
} else { | |
left = findWire(each[0]); | |
} | |
if (Character.isDigit(each[2].charAt(0))) { | |
right = new Wire("",Integer.parseInt(each[2])); | |
} else { | |
right = findWire(each[2]); | |
} | |
result = findWire(each[4]); | |
operator = Gate.valueOf(each[1]); | |
break; | |
} | |
result.input = new LogicGate(left,right,operator); | |
wires.put(result.name, result); | |
if (left!=null) wires.put(left.name, left); | |
if (right !=null) wires.put(right.name, right); | |
} | |
} | |
static class LogicGate { | |
Wire left; | |
Wire right; | |
Gate operator; | |
public LogicGate(Wire left, Wire right, Gate operator) { | |
this.left = left; | |
this.right = right; | |
this.operator = operator; | |
} | |
public boolean isComplete() { | |
return (left == null || left.value >= 0) && | |
(right == null || right.value >= 0); | |
} | |
public void apply(Wire wire) { | |
wire.value = operator.func.applyAsInt(left != null ? left.value : 0 | |
, right != null ? right.value : 0); | |
} | |
} | |
static class Wire { | |
String name; | |
int value; | |
LogicGate input; | |
public Wire(String name) { | |
this.name = name; | |
this.value = -1; | |
} | |
public Wire(String name, int value) { | |
this(name); | |
this.value = value; | |
} | |
} | |
public static void main(String[] args) { | |
List<String[]> list = FileIO.getFileLinesSplit("advent7.txt", " "); | |
init(list); | |
long time = System.currentTimeMillis(); | |
process(); | |
time = System.currentTimeMillis() - time; | |
System.out.println("Time for process: " + time + "ms"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment