DEV Community

Viper
Viper

Posted on • Updated on

Advent of Code 2021 Python Solution: Day 7

I found today's solution to be easier than previous day's.

Part 1

data,data1 = get_data(day=7)

data = [int(d) for d in data[0].split(",")]
data1 = [int(d) for d in data1[0].split(",")]

l = len(data1)
f = []

for v in range(l):
    f.append((sum([abs(d-v) for d in data1])))
print(min(f))        
Enter fullscreen mode Exit fullscreen mode

One Liner Solution

min([sum([abs(d-v) for d in data1]) for v in range(len(data1))])
Enter fullscreen mode Exit fullscreen mode

Part 2

l = len(data1)
f = []

for v in range(l):
    diff = [abs(d-v) for d in data1]
    diffs = sum([sum(list(range(dif+1))) for dif in diff])
    f.append(diffs)
print(min(f))        
Enter fullscreen mode Exit fullscreen mode

One Liner Solution

min([sum([sum(list(range(abs(d-v)+1))) for d in data1]) for v in range(len(data1))])
Enter fullscreen mode Exit fullscreen mode

Why not read more?

Discussion (0)