Advent of Code 2021 Python Solution: Day 7

qviper

Viper

Posted on December 7, 2021

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?

💖 💪 🙅 🚩
qviper
Viper

Posted on December 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of Code 2021 Python Solution: Day 16
adventofcode Advent of Code 2021 Python Solution: Day 16

December 17, 2021

Advent of Code 2021 Python Solution: Day 15
adventofcode Advent of Code 2021 Python Solution: Day 15

December 15, 2021

Advent of Code 2021 Python Solution: Day 14
adventofcode Advent of Code 2021 Python Solution: Day 14

December 14, 2021

Advent of Code 2021 Python Solution: Day 13
adventofcode Advent of Code 2021 Python Solution: Day 13

December 13, 2021

Advent of Code 2021 Python Solution: Day 12
adventofcode Advent of Code 2021 Python Solution: Day 12

December 12, 2021