r/adventofcode • • Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
  • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


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:04:35, megathread unlocked!

66 Upvotes

1.2k comments sorted by

View all comments

1

u/Flavorless_Quark Dec 06 '20 edited Dec 06 '20

Python 3 - Part 1

inp= open('input').read().rstrip()

alpha = ["a","b","c","d", "e","f", "g", "h", "i", "j","k", 
"l","m","n","o","p","q","r","s", "t","u","v","w","x","y","z"]

s = 0
inpu = inp.split("\n\n")
for x in inpu:
    tot = sum(1 for y in alpha if y in x)
    s +=tot

print(s)

Part 2

inp= open('input').read().rstrip()

alpha = ["a","b","c","d", "e","f", "g", "h", "i", "j","k", 
"l","m","n","o","p","q","r","s", "t","u","v","w","x","y","z"]

s = 0
inpu = inp.split("\n\n")
for x in inpu:
    line = x.split("\n")
    for letter in alpha:
	    if all([letter in z for z in line ]):
		    s+=1
print(s)

I think it'd have done a lot better if I didn't keep misspelling my variables, also had to write the alphabet....

2

u/backtickbot Dec 06 '20

Hello, Flavorless_Quark: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/daggerdragon Dec 06 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

1

u/theboxboy Dec 06 '20

Just to save time in the future, use python's list comprehension syntax to avoid making a whole list for each letter in the alphabet (https://www.pythonforbeginners.com/basics/list-comprehensions-in-python). Something like [c for c in 'abcdefghijklmnopqrstuvwxyz'] is easier to type and is functionally the same!

1

u/Flavorless_Quark Dec 06 '20 edited Dec 06 '20

Yep, I know, I was trying to remember how to do it with range but couldnt come up with it on the spot... I've been told to make a VSCode snippet for it too but I thought "there 's no way I'll have to write the alphabet again.." lo and behold...

what I did instead: alpha = [chr(c) for c in range(ord('a'), ord('z') + 1)]

also thinking about it I could just have made it a string instead of a list...

1

u/Glass-Water Dec 06 '20

Nice, similar to mine. Just FYI Python3 has built in `string.ascii_lowercase`
https://docs.python.org/3/library/string.html