DEV Community

Viper
Viper

Posted on

 

Advent of Code 2020: Python Solution Day 10

I nearly forgot about doing the challenge. Part one was easy and part two was bit tricky so I did wrong at first and got help from this repo. Again I tried it first on test input and only then on real one. My solution is at this repository.

Part one

with open("day10.txt", "r") as fp:
    lines = [int(line.rstrip()) for line in fp.readlines()]

one_jolt = 0
two_jolt = 0
three_jolt = 0 
outlet_rating = 0
lines.append(max(lines)+3) # because max jolt is added


while True:
    #print(1 in lines)
    if (outlet_rating + 1) in lines:
        one_jolt+=1
        outlet_rating += 1
    elif outlet_rating+2 in lines:
        two_jolt+=1
    elif (outlet_rating + 3) in lines:
        three_jolt += 1
        outlet_rating+=3
    else:
        break
print("Part 1 answer is ", one_jolt*three_jolt)
Enter fullscreen mode Exit fullscreen mode

Part two

# part 2
sol = {0:1}
for line in sorted(lines):
    sol[line] = 0
    if line - 1 in sol:
        sol[line]+=sol[line-1]
    if line - 2 in sol:
        sol[line]+=sol[line-2]
    if line - 3 in sol:
        sol[line]+=sol[line-3]

print(sol[max(lines)])
Enter fullscreen mode Exit fullscreen mode

Lets Share Your Solution too.

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 (8)

Collapse
 
thetalent profile image
thetalent • Edited
data = sorted([int(i) for i in  puzzle.input_data.split(lf)])

 #heads of paths counted over all paths.
resb={0:1}
for i in range(len(data)-1):
    # sum of all paths so far
    value = sum(resb.values()) 
   # next number differs by3 ? all paths apply n
    if data[i+1]-data[i]==3:
       resb={data[i]:value}
  else:
    try:
        # applying is a must for a path with head 3 lower than n
        # as we know from part 1 - a diff of 2 is not there
        resb[data[i]-3]=0
    except:
        pass
  resb[data[i]]=value
Enter fullscreen mode Exit fullscreen mode

print(sum(resb.values()))

Collapse
 
thetalent profile image
thetalent • Edited

Part One

data = sorted([int(i) for i in  puzzle.input_data.split(lf)])
r={1:0,2:0,3:0}
s=0
for i in data:
 r[i-s]+=1
 s=i
print(r[1]*(r[3]+1))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
qviper profile image
Viper

This is very short. And absolutely tricky. Thanks for sharing.

Collapse
 
amanmad1 profile image
Aman Madan

I really liked the part2 implementation, a subtle dp.

Collapse
 
qviper profile image
Viper

Yeah it is so dope.

Collapse
 
thetalent profile image
thetalent

Did u have a difference of 2 in part1 ?

Collapse
 
philippurwoko profile image
Philip Purwoko

I don't

Collapse
 
laojala profile image
Laura Ojala (she/they)

Thank you for sharing this! I got "strong inspiration" (read copied 😅) your part 2 solution. My solution in Github.

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.