Skip to content

Commit

Permalink
day 14 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagowfx committed Dec 25, 2024
1 parent 672c7e5 commit b9e9dd4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 2024/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Then:
# make DAY=1 DEBUG=0

DAY ?= 9
DAY ?= 14
DEBUG ?= 1

SAMPLE = day$(DAY)/sample.txt
Expand Down
2 changes: 1 addition & 1 deletion 2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
I've been blogging about my solutions.
Start with Day 1) here: https://www.perrotta.dev/2024/12/advent-of-code-2024-day-1/

![Completion Status](https://img.shields.io/badge/stars%20⭐-18/50-yellow)
![Completion Status](https://img.shields.io/badge/stars%20⭐-19/50-yellow)

The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months! Last anyone heard, he was visiting locations that are historically significant to the North Pole; a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.

Expand Down
5 changes: 5 additions & 0 deletions 2024/day17/input.txt
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
70 changes: 70 additions & 0 deletions 2024/day17/main.py
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()
1 change: 1 addition & 0 deletions 2024/day17/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,5,0,1,7,4,1,0,3
5 changes: 5 additions & 0 deletions 2024/day17/sample.txt
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

0 comments on commit b9e9dd4

Please sign in to comment.