DEV Community 👩‍💻👨‍💻

Thibaut Patel
Thibaut Patel

Posted on

Solving day 8 of Advent of Code 2020 in JavaScript

Top comments (2)

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!

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.