| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import os, argparse
- def solve_task(lines):
- c = CRT()
- signal_strengths = []
- for line in lines:
- signal_strengths.extend(c.parse_instruction(line))
-
- total = sum(signal_strengths)
-
- print(signal_strengths)
- print(f"Total signal strength: {total}")
- class CRT:
- def __init__(self):
- self.x = 1
- self.cycle = 0
-
- def parse_instruction(self, instruction):
- states_to_return = []
- tokens = instruction.split()
- if tokens[0] == "noop":
- states_to_return.extend(self.tick())
- else:
- xdiff = int(tokens[1])
- states_to_return.extend(self.tick())
- states_to_return.extend(self.tick())
- self.x += xdiff
- return states_to_return
- def tick(self):
- to_return = []
- self.cycle += 1
- if (self.cycle+20) % 40 == 0:
- to_return = [self.cycle*self.x]
- return to_return
-
- def read_lines(filename):
- lines = []
- with open(filename) as infile:
- for raw_line in infile:
- line = raw_line.rstrip()
- lines.append(line)
- return lines
- def parse_arguments():
- parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
- parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
- args = parser.parse_args()
- return args
- def main():
- args = parse_arguments()
- lines = read_lines(args.filename)
- solve_task(lines)
- if __name__ == "__main__":
- main()
|