Advertisement
icarussiano

day 5 python/Julia

Dec 5th, 2024 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #Python
  2. from functools import cmp_to_key
  3. r,u = open('input').read().split('\n\n')
  4. r = {(x[0], x[1]) for x in (line.split("|") for line in r.splitlines())} #rules
  5. u = [x.split(',') for x in u.splitlines()] #updates
  6. cmp = lambda x, y: -1 if (x, y) in r else (1 if (y, x) in r else 0)
  7. print(sum(int(x[(len(x) - 1) // 2]) for x in u if all((x[i], x[i + 1]) in r for i in range(len(x) - 1))))
  8. print(sum(int(sorted(x, key=cmp_to_key(cmp))[(len(x) - 1) // 2]) for x in u if not all((x[i], x[i + 1]) in r for i in range(len(x) - 1))))
  9.  
  10. #Julia
  11. r, u = split(read("input", String), "\n\n")
  12. r = Set(Tuple(parse.(Int, split(x, '|'))) for x in eachsplit(r))
  13. u = [parse.(Int, split(x, ",")) for x in eachsplit(u)]
  14. in(x, y) = (x, y) ∈ r
  15. println(sum(list[(end+1) ÷ 2] for list in u if issorted(list, lt=in)))
  16. println(sum(sort(list, lt=in)[(end+1) ÷ 2] for list in u if !issorted(list, lt=in)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement