r/adventofcode • • Dec 01 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 1 Solutions -🎄-

It's been one heck of a crappy year, so let's make the holidays bright with Advent of Code 2020! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!

We're following the same general format as previous years' megathreads, so make sure to read the full description in the wiki (How Do the Daily Megathreads Work?) before you post! If you have any questions, please create your own thread and ask!

Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!


[Update @ 00:04] Oops, server issues!

[Update @ 00:06]

  • Servers are up!

[Update @ 00:27]

[Update @ 01:26]

  • Many thanks to our live deejay Veloxxmusic for providing the best tunes I've heard all year!!!

NEW AND NOTEWORTHY THIS YEAR

  • Created new post flair for Other
  • When posting in the daily megathreads, make sure to mention somewhere in your post which language(s) your solution is written in

COMMUNITY NEWS

Advent of Code Community Fun 2020: Gettin' Crafty With It

  • Last year y'all got real creative with poetry and we all loved it. This year we're gonna up our own ante and increase scope to anything you make yourself that is related to Advent of Code. Any form of craft is valid as long as you make it yourself!
  • Several folks have forked /u/topaz2078's paste (source on GitHub) to create less minimalistic clones. If you wished paste had code syntax coloring and/or other nifty features, well then, check 'em out!

--- Day 1: Report Repair ---


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, thread unlocked at 00:??:??!

137 Upvotes

1.4k comments sorted by

View all comments

2

u/dead_fish_bob Dec 01 '20 edited Dec 01 '20

EDIT: Sorry I did it in Python took 0.005983829498291016 seconds

import time

start_time = time.time()

expense_file = open('expense_report.txt')
expenses = expense_file.readlines()

for i in range(len(expenses)):
    expenses[i] = int(expenses[i].strip())

expenses = sorted(expenses)
smallest_two = expenses[0] + expenses[1]

largest_single = 2020 - smallest_two

while expenses[-1] > largest_single:
    expenses.pop()       
        

for i in range(len(expenses)):
    x = i+1
    while x < (len(expenses)-1):
        y = x + 1
        while y < len(expenses):
            if 2020 !=(int(expenses[i]) + int(expenses[x]) + int(expenses[y])):
                y += 1
            else:
                print(f'The values are {expenses[i]} and {expenses[x]} and {expenses[y]} and multipled together they equal { int(expenses[i]) * int(expenses[x]) * int(expenses[y])}')
                print("--- %s seconds ---" % (time.time() - start_time))
                break
        x += 1

2

u/daggerdragon Dec 01 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

Or consider using /u/topaz2078's paste or an external repo instead.

Thanks!

2

u/dead_fish_bob Dec 01 '20

I don't use reddit much, I tried switching to the markdown mode and it got worse and turned it all into a paragraph. Was there something I wasn't clicking?

2

u/daggerdragon Dec 01 '20

Yeah, I bet the Markdown parser was real confused when you switched over to it. Try copying your post text in the fancypants editor first, then switch to Markdown mode and paste your code, then add the four spaces to each line.

2

u/dead_fish_bob Dec 01 '20

It wasn't the only one confused :)

2

u/dead_fish_bob Dec 01 '20

oo did that work?

2

u/daggerdragon Dec 01 '20

There we go! 👍

1

u/peteroh9 Dec 01 '20

I had something that was taking forever to calculate. I saw your solution, learned about pop, and my calculation took .0019919872283935547 seconds :D thank you!

1

u/dead_fish_bob Dec 01 '20

No problem!

Ya mine was taking forever and then I realized if I know what the lowest two are, why not just get rid of everything else.

Now, I don't know how much it would time it would save in this list but if I turned that pop section into a function and called it each time I moved to the next item in the list, that might be beneficial on a larger list.

2

u/peteroh9 Dec 01 '20

Someone else in here reduced all of part one to print(reduce(lambda x,y: x*y, [x for x in data if (2020-x) in set(data)])), so using a function is definitely on the right track lol

1

u/dead_fish_bob Dec 01 '20

Mercy...I really need to sit down and look at lambda expressions and the functools library.

1

u/peteroh9 Dec 01 '20

That one works because of reduce. It was really interesting to learn about it for this.

1

u/tobega Dec 01 '20

You could probably still shave a bit more. My julia solution runs about 1000 times faster than that, 1us to sort, 2.5us to make a set, 400ns for part1 and 3.8us for part2, so 0.000008s total. Now Julia is faster than Python, but I don't think its 1000 times, more like 50, so the rest must be algorithm.

1

u/dead_fish_bob Dec 01 '20

Oh I bet, especially looking at some of the other folks code. I'm more a novice hobbyist.