Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 10, 2022 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnyman/e19c5cc188ae3667ec011bbe1d00920b to your computer and use it in GitHub Desktop.
Save gnyman/e19c5cc188ae3667ec011bbe1d00920b to your computer and use it in GitHub Desktop.
import sys
# Parse the input file from the command line arguments
input_file = sys.argv[1]
# Read the program from the input file
program = []
with open(input_file, "r") as f:
for line in f:
# Split the line into the instruction and the value (if applicable)
parts = line.split()
if len(parts) == 1:
# If there is no value, the instruction is "noop"
program.append(("noop", 0))
else:
# Otherwise, the instruction is "addx" and the value is the second element of the split line
program.append(("addx", int(parts[1])))
# Initialize the X register to 1
x = 1
# Loop through each instruction in the program
for cycle, (instruction, value) in enumerate(program):
if instruction == "noop":
# If the instruction is "noop", do nothing
pass
else:
# Otherwise, increment the X register by the specified value
x += value
# Check if the current cycle is one of the ones we need to compute the signal strength for
if cycle in [19, 59, 99, 139, 179, 219]:
# If it is, compute the signal strength by multiplying the cycle number by the current value of the X register
signal_strength = cycle * x
print(signal_strength)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment