I know I can not be on even top 1000 but I love to solve challenges. Lets share your solution too. And the solution to day 8 is not that harder but kind of brute force has to be done. Link to the challenge is here. I have updated my notebook at this repository.
with open("day8.txt", "r") as fp:
lines = fp.readlines()
lines = [line.rstrip() for line in lines]
# print(lines)
# challenge 1
def challenge1(lines):
curr_acc = 0
visited_line = set()
curr_line = 0
valid_sol = True
while True:
# viewed all lines and we got lucky, no loops
if len(lines)-1 == curr_line:
valid_sol = False
# for challenge 1 solution
if curr_line in visited_line:
valid_sol = False
return curr_acc, valid_sol
inst, acc = lines[curr_line].split(" ")
acc = int(acc)
visited_line.add(curr_line)
if inst == "nop":
curr_line += 1
if inst == "acc":
curr_acc += acc
curr_line += 1
if inst == "jmp":
curr_line+=acc
# we got lucky
if valid_sol == False:
return curr_acc, True
return curr_acc, False
print("Current Accumulator: ", challenge1(lines)[0])
# challenge 2
def challenge2(lines):
curr_acc = 0
visited_line = []
curr_line = 0
new_lines = lines.copy()
for curr_line in range(1, len(new_lines)):
inst, acc = lines[curr_line].split(" ")
acc = int(acc)
if inst == "nop":
inst = "jmp"
elif inst == "jmp":
inst = "nop"
visited_line = []
new_lines = lines.copy()
new_lines[curr_line] = " ".join((inst, str(acc)))
acc, valid = challenge1(new_lines)
if valid:
return acc
print("Current Accumulator: ", challenge2(lines))
I write blogs about Computer Vision projects on my GitHub page q-viper.github.io and if you got some time please share yours too.
Top comments (2)
Found a very similar solution:
github.com/r0f1/adventofcode2020/b...
I never learned how to write shorter codes. Thanks for sharing. Your solution is neat.