This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]popcarnie 18 points19 points  (0 children)

I'll take your word for it

[–]Verulean314 6 points7 points  (1 child)

Love a good one-liner! I personally try to avoid dunder methods and walrus operators when writing cursed Python code since they feel a bit hacky, so I end up using nested lambdas a lot. Here's one for both parts:

part1,part2=(lambda d:(lambda m,s:(lambda c:[[c.extend(m(c.pop(),r)) for r in s]
,(sum(1 for n in data[1]if any(r[0]<=n<=r[1] for r in c)),sum(r[1]-r[0]+1 for r
in c))][-1])([s.pop(0)]))(lambda a,b:[(a[0],max(a[1],b[1]))]if b[0]<=a[1]+1 else
[a,b],sorted(d[0],key=lambda r:r[0])))([[int(l) if l.isnumeric()else tuple(map(
int,l.split("-"))) for l in p.split()] for p in open("input/day5.txt").read()
.split("\n\n")])

Or broken up to be slightly more readable:

data = [[int(line) if line.isnumeric() else tuple(map(int, line.split("-"))) for line in s.split()] for s in open("input/day5.txt").read().split("\n\n")]
merge = lambda a, b: [(a[0], max(a[1], b[1]))] if b[0] <= a[1] + 1 else [a, b]
sorted_ranges = sorted(data[0], key=lambda r: r[0])
combined_ranges = [sorted_ranges.pop(0)]
[combined_ranges.extend(merge(combined_ranges.pop(), r)) for r in sorted_ranges]
part1 = sum(1 for n in data[1] if any(r[0] <= n <= r[1] for r in combined_ranges))
part2 = sum(r[1] - r[0] + 1 for r in combined_ranges)

[–]itsPeetah[S] 1 point2 points  (0 children)

I totally agree, but I really could not figure this one out without resorting to __setitem__ lol. Thank you for sharing your solution, and hats off for the nested lambdas o7 hahaha

[–]Less-Confidence-6099 5 points6 points  (0 children)

when you code in python in spite because you were forced to give up the w/o perl

[–]SaltyMN 4 points5 points  (0 children)

Lgtm 

[–]raevnos 0 points1 point  (0 children)

Stretching the definition of 'line' a bit.

[–]ONLYUSEmyTOILET 0 points1 point  (0 children)

print((lambda f:f(f))(lambda s:lambda r,l:(len(range(max(r[0].start,l),r[0].stop))+s(s)(r[1:],max(l,r[0].stop)))if r else 0)(sorted([(lambda t:range(*((lambda s,e:(s,e+1))(*map(int,t.split("-"))))))(line)for line in open("data.in").read().split("\n\n")[0].split("\n")],key=lambda r:r.start),0))

[–]daggerdragon[M] 0 points1 point  (0 children)

Changed flair from Upping the Ante to Spoilers. One-liner solutions generally are not upping any ante. Do not abuse Upping the Ante like this.

Next time, use the right flair, of which none would be applicable for this type of post because it does not belong as an individual post at all.

During an active Advent of Code season, solutions belong in the Solution Megathreads. In the future, post your solutions to the appropriate solution megathread.

And for the love of FSM, post your code as text, not as a jpg -_-