diff --git a/2015/d6.py b/2015/d6.py new file mode 100644 index 0000000..d2de676 --- /dev/null +++ b/2015/d6.py @@ -0,0 +1,33 @@ +from lib import * + +data = open(0).read() + +part_2 = True +lights = [[0 for _ in range(1000)] for _ in range(1000)] + +for line in data.splitlines(): + x1, y1, x2, y2 = str_to_ints(line) + for x in range(x1, x2 + 1): + for y in range(y1, y2 + 1): + if part_2: + if "on" in line: + lights[x][y] += 1 + elif "off" in line: + lights[x][y] = max(0, lights[x][y] - 1) + else: + lights[x][y] += 2 + else: + if "on" in line: + lights[x][y] = 1 + elif "off" in line: + lights[x][y] = 0 + else: + if lights[x][y] == 1: + lights[x][y] = 0 + else: + lights[x][y] = 1 + +res = 0 +for row in lights: + res += sum(row) +print(res) diff --git a/2015/d7.py b/2015/d7.py new file mode 100644 index 0000000..fbe26a1 --- /dev/null +++ b/2015/d7.py @@ -0,0 +1,90 @@ +from lib import * + +data = open(0).read() + +part_2 = False +if part_2: + gates = {"b": 16076} +else: + gates = {} + +while "a" not in gates: + for line in data.splitlines(): + lhs, rhs = line.split(" -> ") + if part_2 and rhs == "b": + continue + lhs = lhs.strip() + if "NOT" in lhs: + op, op1 = lhs.split(" ") + assert op == "NOT" + try: + op1 = int(op1) + gates[rhs] = ~op1 & 0xffff + except ValueError: + if op1 in gates and isinstance(gates[op1], int): + gates[rhs] = ~gates[op1] & 0xffff + elif "OR" in lhs: + op1, op, op2 = lhs.split(" ") + assert op == "OR" + try: + op1 = int(op1) + except ValueError: + if op1 in gates and isinstance(gates[op1], int): + op1 = gates[op1] + try: + op2 = int(op2) + except ValueError: + if op2 in gates and isinstance(gates[op2], int): + op2 = gates[op2] + + if not (isinstance(op1, int) and isinstance(op2, int)): + continue + gates[rhs] = (op1 | op2) + elif "AND" in lhs: + op1, op, op2 = lhs.split(" ") + assert op == "AND" + try: + op1 = int(op1) + except ValueError: + if op1 in gates and isinstance(gates[op1], int): + op1 = gates[op1] + try: + op2 = int(op2) + except ValueError: + if op2 in gates and isinstance(gates[op2], int): + op2 = gates[op2] + + if not (isinstance(op1, int) and isinstance(op2, int)): + continue + gates[rhs] = (op1 & op2) + elif "LSHIFT" in lhs: + op1, op, op2 = lhs.split(" ") + op2 = int(op2) + try: + op1 = int(op1) + except ValueError: + if op1 in gates: + op1 = gates[op1] + else: + continue + gates[rhs] = (op1 << op2) & 0xffff + elif "RSHIFT" in lhs: + op1, op, op2 = lhs.split(" ") + op2 = int(op2) + try: + op1 = int(op1) + except ValueError: + if op1 in gates: + op1 = gates[op1] + else: + continue + gates[rhs] = (op1 >> op2) & 0xffff + else: + try: + lhs = int(lhs) + gates[rhs] = lhs + except ValueError: + if lhs in gates: + gates[rhs] = gates[lhs] + +print(gates["a"]) diff --git a/README.md b/README.md index 45045b3..c578332 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,6 @@ written in Python. - Day 3: 7:26 ... :/ - Day 4: 5:11 ... - Day 5: 14:05 o.O +- Day 6: 15:00 +- Day 7: 17:00 +-