-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
# Then: | ||
# make DAY=1 DEBUG=0 | ||
|
||
DAY ?= 9 | ||
DAY ?= 14 | ||
DEBUG ?= 1 | ||
|
||
SAMPLE = day$(DAY)/sample.txt | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Register A: 37293246 | ||
Register B: 0 | ||
Register C: 0 | ||
|
||
Program: 2,4,1,6,7,5,4,4,1,7,0,3,5,5,3,0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
|
||
|
||
def main(): | ||
with open(sys.argv[1]) as input: | ||
lines = input.read().splitlines() | ||
|
||
a, b, c = [int(line.split()[2]) for line in lines[0:3]] | ||
program = [int(op) for op in lines[4].split()[1].split(",")] | ||
ip = 0 | ||
stdout = [] | ||
|
||
def combo(operand): | ||
assert 0 <= operand < 7 | ||
|
||
if 0 <= operand <= 3: | ||
return operand | ||
elif operand == 4: | ||
return a | ||
elif operand == 5: | ||
return b | ||
elif operand == 6: | ||
return c | ||
|
||
while ip < len(program) - 1: | ||
opcode = program[ip] | ||
operand = program[ip + 1] | ||
|
||
# adv, division | ||
if opcode == 0: | ||
numerator = a | ||
denominator = 2 ** combo(operand) | ||
a = numerator // denominator | ||
# bxl, bitwise xor | ||
elif opcode == 1: | ||
b ^= operand | ||
# bst, modulo | ||
elif opcode == 2: | ||
b = combo(operand) % 8 | ||
# jnz | ||
elif opcode == 3: | ||
if a != 0: | ||
ip = operand | ||
continue | ||
# bxc, bitwise xor | ||
elif opcode == 4: | ||
b ^= c | ||
# out | ||
elif opcode == 5: | ||
stdout.append(combo(operand) % 8) | ||
# bdv | ||
elif opcode == 6: | ||
numerator = a | ||
denominator = 2 ** combo(operand) | ||
b = numerator // denominator | ||
# cdv | ||
elif opcode == 7: | ||
numerator = a | ||
denominator = 2 ** combo(operand) | ||
c = numerator // denominator | ||
|
||
ip += 2 | ||
|
||
# part one | ||
print(",".join(map(str, stdout))) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1,5,0,1,7,4,1,0,3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Register A: 729 | ||
Register B: 0 | ||
Register C: 0 | ||
|
||
Program: 0,1,5,4,3,0 |