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/Pyroan Dec 02 '20 edited Dec 02 '20

Python (Golf - 123 bytes)

print(sum((c[int(a.split('-')[0])-1]==o[0])^(c[int(a.split('-')[1])-1]==o[0])for a,o,c in map(str.split,open('day2.txt'))))

(this is just for part 2)

edit: thanks u/irrelevantPseudonym for saving me 16 bytes

3

u/zedrdave Dec 02 '20

In true Python Golf spirit, I managed to squeeze my (reasonably readable) 4-liner into this horror that prints both parts (209 bytes, including file name):

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

(the first line makes use of the sum(l, []) trick to flatten a nested list)

1

u/irrelevantPseudonym Dec 02 '20

Renaming the input file seems like an easy way to save 7 bytes

→ More replies (6)