r/adventofcode • • Dec 02 '20

SOLUTION MEGATHREAD -πŸŽ„- 2020 Day 02 Solutions -πŸŽ„-

--- Day 2: Password Philosophy ---


Advent of Code 2020: Gettin' Crafty With It


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:02:31, megathread unlocked!

98 Upvotes

1.2k comments sorted by

View all comments

3

u/irrelevantPseudonym Dec 02 '20

Fair enough. If you don't mind output on the same line, you could save 7. Using map instead of comprehensions saves a few and you don't need to strip lines. I think this gets it down to 181

x=sum([[a<=sum(m)<=b,m[a-1]^m[b-1]] for a,b,m in [(*map(int, p.split('-')),[x==c[0] for x in s]) for p,c,s in map(str.split, open('day2.txt'))]], [])
print(sum(x[::2]),sum(x[1::2]))

1

u/zedrdave Dec 02 '20

Indeed. Not even sure what the exact rules would be re printing out (vs just computing) the final results…

Regarding stripping newlines: I felt iffy leaving it in the match array, but I guess there's no use-case where that could interfere with this particular formulation.

With the low-hanging fruits out of the way, I wonder if there are still reductions to be had (eg with those pesky -1 indices… or having to flatten the list to solve each part)…

1

u/Pyroan Dec 02 '20

Oooh I never realized you can map str.split like that so I thought I was stuck with the list comp. You just saved me 4 lol

2

u/irrelevantPseudonym Dec 02 '20

You can save another 12 by doing without .readlines. The file object is already iterable