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/coccosoids Dec 01 '20 edited Dec 01 '20

My python solution:

import sys

with open("input_day01.txt") as f:
    text = [int(x) for x in f.readlines()]

answer = []

for i in text:
	for j in text:
		for k in text:
			if i + j + k== 2020:
				answer = i * j * k
				break

			
print answer

Can someone tell me why the execution time is 8.4s and how can I optimize?

PS: how do you paste code in reddit?

1

u/daggerdragon Dec 01 '20

PS: how do you paste code in reddit?

It's in our wiki under "How do I format code?"!

1

u/coccosoids Dec 01 '20

Sadly it doesn't work correctly.

1

u/nofacade Dec 01 '20

I would take a closer look at your loops. Can you see a way to reduce the total amount of iterations? Are you running the same set multiple times?

1

u/coccosoids Dec 01 '20

Is it in any way possible that you can give another hint?

1

u/nofacade Dec 01 '20

You can always be super explicit and print out i,j,k and see what you get. It might be useful to use the test values to lower the amount of text.

Look especially at j and k's loops. What is the starting value for each loop? Do they need to iterrate over all of the values in the input?

1

u/kaprawiec Dec 01 '20

Use set instead of arrays

1

u/StandardIssueHuman Dec 01 '20

Note that the string-to-integer conversion is slow. It pays off to do it only once in the beginning, instead of each time you do addition

1

u/i_have_no_biscuits Dec 01 '20

Style Note

You should probable avoid calling your variable 'input', as 'input' is one of the Python built in functions. You've now removed your ability to get user input!

It's similar to writing something like

int = 2

print = 5

which is valid Python code but should make your eyes twitch!

Speed Note

As to why the code is running slowly... One reason is that you are converting from string to integer repeatedly in the middle of your loop. It would be better to convert everything to an integer first, before you start looping, with something like:

data = [int(n) for n in data]

This will also have the advantage of making the inside of your loop look much nicer:

if i + j + k == 2020:

answer = i * j * k

break

The next speedups would come from thinking about how you are iterating, as u/nofacade has mentioned. I'll let you think about that yourself!

(looks like I can't paste code properly into Reddit these days either!)

1

u/dead_fish_bob Dec 01 '20

If you look at mine above, I got it significantly reduced by using the sorted option on the list and then added the two smallest numbers together, I then subtracted those from 2020, and that gave me the largest possible number. I then popped every number larger than that off the list.

1

u/jslepicka Dec 01 '20

Convert your input to an int one time when you're reading in the file instead of every time you reference it. e.g.,

with open("input_day01.txt") as f:
    input = [int(x) for x in f.readlines()]

And then change all of the int(i), etc. references to just i. Runtime will drop significantly.

1

u/coccosoids Dec 01 '20

Cool tip.

Reduced to 0.5s How did you paste your code?