Unable to load image
Reported by:

rDrama Advent of Code Day 1: Warm up

I hope this fine morning finds you well, saars.

The word of the day is collections.Counter

The rdrama leaderboard invite code is 632268-30587026

I created the ping group: !AOC, I'm sure it will be useful offseason too.

Charts! https://github.com/jeroenheijmans/advent-of-code-charts

19
Jump in the discussion.

No email address required.

#AOC 1. des 1
column1 =[]
column2 =[]

theData = open("Datasets/ids1.12")
theData = theData.readlines()

for line in theData:
  parts = line.split()
  column1.append(int(parts[0]))
  column2.append(int(parts[1]))

column1.sort()
column2.sort()

distance = 0

for i in range(len(column1)):
  distance += abs(column1[i] - column2[i])
print(distance)

Every time I have to use loops I feel like shit, because that is where you get performance bottlenecks. But then I realized I don't have to submit any actual code and just did what came most natural. I am not comfortable with Python and have to relearn shit every time I use it.

#AOC 1. des 2
column1 =[]
column2 =[]

theData = open("Datasets/ids1.12")
theData = theData.readlines()

for line in theData:
  parts = line.split()
  column1.append(int(parts[0]))
  column2.append(int(parts[1]))

column2Dict = {}

for i in column2:
  column2Dict.update({i: 0})

for i in column2:
  column2Dict[i] = column2Dict[i] + 1

simScore = 0

for i in column1:
  if i not in column2Dict:
    continue
  simScore += (i * column2Dict[i])

print(simScore)

The scoring on this seems a little sleep-phobic, not gonna lie. This solution is a little ungabunga, I think I could have avoided one loop if I knew how to python better.


:#marseydisintegrate: :!#marseyflamewar::space::!marseyagree:

Jump in the discussion.

No email address required.

I am not comfortable with Python and have to relearn shit every time I use it.

Then dont do it :marseyconfused:

Jump in the discussion.

No email address required.

I'm like this but for every language I use. I assumed this was just part of the Process?

Jump in the discussion.

No email address required.

Oh ok I thought you had some hangup on python specifically

Jump in the discussion.

No email address required.

I'm not the person in the picture.

Jump in the discussion.

No email address required.

If you really care,

distance = 0
for i in range(len(column1)):
  distance += abs(column1[i] - column2[i])

could be

distance = sum(abs(column1[i] - column2[i]) for i in range(len(column1)))

or

distance = sum(abs(x - y) for x, y in zip(column1, column2))

but in principle they're all for loops. Whatever obscure performance benefits one syntax might offer isn't worth worrying about. Readability is king in python.

Jump in the discussion.

No email address required.

Every time I have to use loops I feel like shit, because that is where you get performance bottlenecks. But then I realized I don't have to submit any actual code and just did what came most natural. I am not comfortable with Python and have to relearn shit every time I use it.

Read other people's code and learn from it. Python is a remarkably nice language, pretty much every time you feel like shit for writing verbose code, that's a red flag telling you that there's a way to write it five times shorter and more to the point.

Jump in the discussion.

No email address required.

dict has get and setdefault methods that deal with nonexistent keys in a streamlined fashion. So column2Dict[i] = column2Dict.get(i, 0) + 1 would work. Even better, use collections.Counter which does all that for you, see my code in a comment here.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.