DEV Community


Solving day 8 of Advent of Code 2020 in JavaScript

thibpat profile image Thibaut Patel ・1 min read

Discussion (2)

Editor guide
philippurwoko profile image
Philip Purwoko

The puzzle is getting difficult for me. I don't really understand the question ( My bad English 🙁 ). I watch your videos since days 7. Here my part 1 solution in python.

with open('input.txt') as file:
    console = file.readlines()
    console = [c[:-1] for c in console]
    console = [{c.split()[0]:int(c.split()[1])} for c in console]

class Program:
    def __init__(self, commands):
        self.commands = commands
        self.pointer = 0
        self.accumulator = 0
        self.history = set()

    def run(self):
        while True:
            # Infinite Loop control
            if self.pointer in self.history:
                print('Infinite Loop ! Accumulator : ', self.accumulator)

            com = [(k, v) for k, v in self.commands[self.pointer].items()][0]
            if com[0] == 'nop':
                self.pointer += 1
            elif com[0] == 'acc':
                self.pointer += 1
                self.accumulator += com[1]
            elif com[0] == 'jmp':
                self.pointer += com[1]

program = Program(console)
Enter fullscreen mode Exit fullscreen mode
thibpat profile image
Thibaut Patel Author

Looks great 👍
The puzzles are indeed more and more complex!