Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Python
- from functools import cmp_to_key
- r,u = open('input').read().split('\n\n')
- r = {(x[0], x[1]) for x in (line.split("|") for line in r.splitlines())} #rules
- u = [x.split(',') for x in u.splitlines()] #updates
- cmp = lambda x, y: -1 if (x, y) in r else (1 if (y, x) in r else 0)
- 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))))
- 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))))
- #Julia
- r, u = split(read("input", String), "\n\n")
- r = Set(Tuple(parse.(Int, split(x, '|'))) for x in eachsplit(r))
- u = [parse.(Int, split(x, ",")) for x in eachsplit(u)]
- in(x, y) = (x, y) ∈ r
- println(sum(list[(end+1) ÷ 2] for list in u if issorted(list, lt=in)))
- 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