Advertisement
icarussiano

day 2 python

Dec 2nd, 2023 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #PART 1
  2. with open("input") as file:
  3.     data = file.read().splitlines()
  4. d={'red':12,'green':13,'blue':14}
  5. sum = 0
  6. for line in data:
  7.     id, games = line.split(": ")
  8.     games = games.split("; ")
  9.     id = int(id[5:])
  10.     p = True
  11.     for game in games:
  12.         g=game.split(", ")
  13.         for _ in g:
  14.             n,c = _.split(" ")
  15.             if c in d and int(n)>d.get(c):
  16.                 p=False
  17.                 break
  18.     if (p == True):
  19.         sum+= id
  20. print(sum)
  21.  
  22. #PART 2
  23. from functools import reduce
  24. sum=0
  25. for line in data:
  26.     d = {'red': [], 'green': [], 'blue': []}
  27.     id, games = line.split(": ")
  28.     games = games.split("; ")
  29.     id = int(id[5:])
  30.     for game in games:
  31.         g=game.split(", ")
  32.         for _ in g:
  33.             n,c = _.split(" ")
  34.             if c in d:
  35.                 d[c].append(int(n))
  36.     sum+= reduce(lambda x,y:x*y,[max(d[x]) for x in d])
  37. print(sum)
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement