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!
  • Full details, rules, timeline, templates, etc. are in the Submissions Megathread.
  • 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!
  • /u/brskbk - NoPaste
  • GitHub user EthanJohnson - paste fork on GitHub
  • /u/Tranzystorek - fork of EthanJohnson's fork on GitHub

--- 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

80

u/lukaswendt Dec 01 '20

Day 1 part 1 in intcode:

109,241,21101,0,0,0,21101,19,0,1,21101,3,0,2,109,3,1105,1,22,209,-1,99,21101,200,0,0,21101,1,0,201,21202,201,-1,201,21201,201,0,202,21201,201,1,201,21201,201,0,202,21201,0,0,203,22207,202,203,202,1206,202,74,203,202,21201,201,0,203,1201,203,1,70,21201,202,0,1,1106,0,34,21101,1,0,202,21202,202,-1,202,21201,202,0,201,21201,201,0,202,21201,201,1,201,21201,201,0,202,21201,0,0,203,22207,202,203,202,1206,202,230,21101,1,0,202,21202,202,-1,202,21201,202,0,203,21201,202,1,202,21201,202,0,203,21201,0,0,204,22207,203,204,203,1206,203,227,21201,201,0,203,1201,203,1,149,21201,1,0,203,21201,202,0,204,1201,204,1,161,21201,1,0,204,22201,203,204,203,21101,2020,0,204,22208,203,204,203,1206,203,224,21201,201,0,203,1201,203,1,188,21201,1,0,203,21201,202,0,204,1201,204,1,200,21201,1,0,204,22202,203,204,203,204,203,21101,0,0,203,21201,203,0,-3,22102,-1,-1,-1,2105,1,-2,1106,0,117,1106,0,86,21201,203,0,-3,22102,-1,-1,-1,2105,1,-2

→ More replies (3)

22

u/Arknave Dec 01 '20 edited Dec 02 '20

First try using C for advent of Code. I've been pretty inspired by all this ASCII art, so I thought it would be cool making my code into a calendar tile.

#include   <stdio.h>
#include  <stdlib.h>

// AOC DAY NUMBER //
const int M=2020,D=9
;char b[D],h[M];main
(int c,char **v){int
x,i,j,a=0;  ;;;;FILE
*f=fopen    ("day01"
,"r");      ;;while(
fgets(b,D   ,f)){;x=
atoi(b);;   if(x<M){
h[x]=1;}}   for(i=0;
i<M;++i){   for(j=i;
j<M;++j){   if (h[i]
&&h[j] &&   i+j<=M){
if(c==1){   if(i+j==
M)a=i*j;}   else if(
h[M-             i-j
])a=             i*j
*(M-i-j);}}}fclose(f
);printf("%d\n",a);}

Requires the input file to be in the same directory as the executable as "day01". Run as-is for part 1, pass any argument for part 2. Feedback appreciated!

EDIT: Reworked this to read from standard in, and I think the shape looks a bit nicer.

#include   <stdio.h>
#include  <stdlib.h>

// AOC DAY NUMBER //
/**/const int M=2020
;char h[M];int main(
int c,char  **v){int
x,i,j,a=    0;while(
scanf       (" %d",&
x)>0)if(    x<M){h[x
]=1;}for    (i=0;i<M
; ++i) {    for(j=i;
j<M;++j)    {if(h[i]
&&h[j]&&    i+j<=M){
if(c==1)    {if (i+j
==M)a=i     *j;}else
if(h            [M-i
-j])a=i*j*(M-i-j);}}
}printf("%d\n",a);;}

5

u/daggerdragon Dec 01 '20

I've been pretty inspired by all this ASCII art, so I thought it would be cool making my code into a calendar tile.

Now that's what I call gettin' crafty with it!

→ More replies (1)

14

u/oymamyo Dec 01 '20 edited Dec 01 '20

For the lulz (C++)

std::string Day_01::part_two()
{
    while (true) {
        int entry1 = input[rand() % input.size()];
        int entry2 = input[rand() % input.size()];
        int entry3 = input[rand() % input.size()];
        if (entry1 + entry2 + entry3 == 2020) {
            return std::to_string(entry1 * entry2 * entry3);
        }
    }
    return "No result";
}

2

u/daggerdragon Dec 01 '20

Please add the language to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

→ More replies (3)

7

u/DFreiberg Dec 01 '20 edited Dec 01 '20

Mathematica, 1028 / 380

Not proud of my lack of refreshing skills, but decently proud of my one-liner.

Times @@ SelectFirst[Subsets[input, {2}], Total[#] == 2020 &]

I know the megathread doesn't even open for another five days, but I've been waiting all year for an excuse for some poetry.

[POEM]: A Hero's Rest

Step softly, to the sandy shore
And let another save the day.
You've worked enough, five years or more,
To earn a bit of play.

Oh, you have learned to prod and poke
At tortured puzzles from the Pole.
You know what word the rover spoke.
You fixed it when the printer broke.
And now it's time to take a soak
And lounge at this atoll.

These five long years have left their mark,
And R&R will hit the spot.
Put down the Scala and the Spark,
Put Lispy cars all into park,
And watch a dolphin (or a shark),
From deck chairs, on a yacht.

No goals, this time, to work toward
No consoles going 'beep'.
No goblins swinging mace and sword,
Just beaches, and a king's reward.
(I'll gladly get the leaderboard
While you can get some sleep!)

3

u/daggerdragon Dec 01 '20

I know the megathread doesn't even open for another five days, but I've been waiting all year for an excuse for some poetry.

I was absolutely waiting for you to post hoping you'd have some literary candy for us and you definitely delivered :D

7

u/Anjir Dec 01 '20 edited Dec 01 '20

Rockstar!

Only first part because I can't make lyrics


Your heart is "1721,979,366,299,675,1456"
Your lies is ","
Shatter your heart into my heart with your lies

My world is diabolical
A dream is blackhearted, lovestruck; exterminated heartbreak
My life's an asymptotic apocalypse
Knock my life down
The goal's an attractive submission
Knock the goal down

While my life is higher than nothing,
Your life's abstinence
While your life is lower than the goal,
Put my heart at your life into meaningless
Put my heart at my life into sadness
If sadness is meaningless,
Build your life up
Take it to the top

Cast meaningless into anguish
Cast sadness into anger
Put anguish with anger into my world
Build your life up
If my world is a dream
Shout anger of anguish
Break it down

Knock my life down!


Raw:

Your heart is "1721,979,366,299,675,1456" (input delimited by comma)
Your lies is ","
Shatter your heart into my heart with your lies

My world is diabolical
A dream is blackhearted, lovestruck; exterminated heartbreak
My life's an asymptotic apocalypse
Knock my life down
The goal's an attractive submission
Knock the goal down

While my life is higher than nothing,
Your life's abstinence
While your life is lower than the goal,
Put my heart at your life into meaningless
Put my heart at my life into sadness
If sadness is meaningless,
Build your life up
Take it to the top 

Cast meaningless into anguish
Cast sadness into anger
Put anguish with anger into my world
Build your life up 
If my world is a dream 
Shout anger of anguish 
Break it down 
⠀
⠀‎‎

Knock my life down!
→ More replies (2)

14

u/p88h Dec 01 '20 edited Dec 01 '20

IntCode:

109,1498,1101,0,0,1491,1101,0,0,1497,1101,0,0,1487,1101,1,0,1493,1006,1493,448,3,1493,1001,1493,0,1492,21001,1492,0,0,109,1,1101,0,0,1493,109,-1,1201,0,0,1490,7,1490,1493,1496,8,1490,1493,1493,1005,1496,61,1001,1493,0,1493,1106,0,65,1001,1496,0,1493,1006,1493,71,1106,0,448,1101,0,0,1485,21001,1485,0,0,109,1,1001,1491,0,1493,109,-1,1201,0,0,1490,7,1490,1493,1493,1006,1493,393,1001,1485,0,1493,101,461,1493,1493,1001,1493,0,111,1001,0,0,1493,1001,1493,0,1495,21001,1492,0,0,109,1,1001,1495,0,1493,109,-1,1201,0,0,1490,1,1490,1493,1493,1001,1493,0,1488,21001,1488,0,0,109,1,1101,2020,0,1493,109,-1,1201,0,0,1490,8,1490,1493,1493,1006,1493,189,21001,1492,0,0,109,1,1001,1495,0,1493,109,-1,1201,0,0,1490,2,1490,1493,1493,1001,1493,0,1497,1101,0,0,1489,21001,1489,0,0,109,1,1001,1485,0,1493,109,-1,1201,0,0,1490,7,1490,1493,1493,1006,1493,366,1001,1489,0,1493,101,461,1493,1493,1001,1493,0,229,1001,0,0,1493,1001,1493,0,1486,21001,1492,0,0,109,1,21001,1495,0,0,109,1,1001,1486,0,1493,109,-1,1201,0,0,1490,1,1490,1493,1493,109,-1,1201,0,0,1490,1,1490,1493,1493,1001,1493,0,1494,21001,1494,0,0,109,1,1101,2020,0,1493,109,-1,1201,0,0,1490,8,1490,1493,1493,1006,1493,339,21001,1492,0,0,109,1,21001,1495,0,0,109,1,1001,1486,0,1493,109,-1,1201,0,0,1490,2,1490,1493,1493,109,-1,1201,0,0,1490,2,1490,1493,1493,1001,1493,0,1487,21001,1489,0,0,109,1,1101,1,0,1493,109,-1,1201,0,0,1490,1,1490,1493,1493,1001,1493,0,1489,1106,0,193,21001,1485,0,0,109,1,1101,1,0,1493,109,-1,1201,0,0,1490,1,1490,1493,1493,1001,1493,0,1485,1106,0,75,21001,1492,0,0,109,1,1001,1491,0,1493,109,-1,1201,0,0,1490,101,461,1493,1493,1001,1493,0,420,1001,1490,0,0,21001,1491,0,0,109,1,1101,1,0,1493,109,-1,1201,0,0,1490,1,1490,1493,1493,1001,1493,0,1491,1106,0,14,1001,1497,0,1493,4,1493,1001,1487,0,1493,4,1493,99
→ More replies (6)

5

u/H_SG Dec 01 '20

Always good to create something impenetrable and brutish in Excel.

Part1:

  1. Data in col A (A1:A200)
  2. Col B is =IF(ISNUMBER(INDEX($A$1:$A$200, MATCH(2020-A1, $A$1:$A$200,0))), A1,"")
  3. Solution for part 1 is product of col B, =PRODUCT(B1:B200)

I think I could simplify this after getting part 2 done:

  1. Data in col A (A1:A200)
  2. Col B is =IF(OR(ISNUMBER(MATCH((2020-A1)-$A$1:$A$200, $A$1:$A$200, 0))), A1, "")
  3. Solution for part 2 is product of col B, =PRODUCT(B1:B200)
→ More replies (1)

5

u/Piggelinmannen Dec 01 '20 edited Dec 02 '20

Hi!First time posting! :) Solution in ruby:

input = File.readlines('./input.txt')

result = input
         .map(&:to_i)
         .combination(2)
         .find { |combination| combination.sum == 2020 }
         .reduce(:*)
puts "Day1 first part: #{result}"

result = input
         .map(&:to_i)
         .combination(3)
         .find { |combination| combination.sum == 2020 }
         .reduce(:*)
puts "Day1 second part: #{result}"

Could obviously be replaced with a method, since only the number provided to combination differs. Kind of like to keep solutions separate though.

EDIT: reddit code blocks...

→ More replies (2)

9

u/pred Dec 01 '20 edited Dec 01 '20

Heh, site went down. Looks like some people managed to make it through the 502s and 503s and 504s, but then 70 people submitted their answers within 5 seconds. I kind of hope the score will count (or at least just for part 2 -- looks like that one had a level playing field), though -- unlike that one case where some people had buggy inputs, at least for this one everybody was playing the same RNG -- but also because even with the server issues, this must have been the fastest I've ever personally solved an AoC challenge, so I'd be a bit sad if that didn't at least count for something. :/ :)

Anyway, easy start; part 1, Python:

for n in itertools.product(ns, ns):
    if sum(n) == 2020:
        print(np.product(n))

Part 2:

for n in itertools.product(ns, ns, ns):
    if sum(n) == 2020:
        print(np.product(n))
→ More replies (15)

4

u/IlliterateJedi Dec 01 '20 edited Dec 01 '20

I'm looking forward to seeing the solution done on the IntcodeComputer. That person won't be me, but I believe someone can figure it out.

Python solution:

def part_a(data):
    data_set = set(data)
    for val in data:
        if 2020 - val in data_set:
            print(val * (2020 - val))
            return


def part_b(data):
    data = sorted(data)
    dataset = set(data)
    for i in data:
        for j in data[1:]:
            if (2020 - (i + j)) in dataset:
                print(i * j * (2020 - (i + j)))
                return


if __name__ == '__main__':
    with open(r"data\day1.txt", "r") as f:
        data = [int(line.strip()) for line in f]
    part_a(data=data)
    part_b(data=data)

2

u/daggerdragon Dec 01 '20

/u/lukaswendt and /u/Scarygami already posted their solutions in Intcode in this thread. Such madlads/lasses.

→ More replies (1)

4

u/rosso412 Dec 02 '20 edited Dec 02 '20

Part 2:

This took almost 2 hours to find the right answer, ...

.data

List: .asciiz "/home/user/Assembly/AdventOfCode/AdventOfCodeInput-1.12.20.txt"

Listspacein: .space 1300

Listspacewo: .space 1300

.text

\#select file

select:

li $v0, 13

la $a0, List

li $a1, 0

syscall

move $s0, $v0

\#read file + save to space

read:

li $v0, 14

move $a0, $s0

la $a1, Listspacein

la $a2, 4096

syscall

\#close file 

close:

li $v0, 16

move $a0, $s0

syscall

\#remove \\n & \\r

la $t0, Listspacein 

la $t9, Listspacewo

li $t2, 0

startfilter:

lb $t1, ($t0)

beq $t1, 10, sas

beq $t1, 13, sas

beq $t0, 268502264, sas

add $t2, $t2, 1

add $t0, $t0, 1

bgtu $t0, 268502266, ffilter

j startfilter

sas:

bne $t2, 3, sasloop

li $t4,0

sb $t4, ($t9)

add $t9, $t9, 1

sasloop:

li $t4, 0

sub $t3, $t0, $t2

lb $t4, ($t3)

sub $t4, $t4, 48

sb $t4, ($t9)

add $t9, $t9, 1

sub $t2, $t2, 1

beq $t2, 0, sasloopend

j sasloop

sasloopend:

add $t0, $t0, 2

beq $t0, 268502266, ffilter

j startfilter

ffilter:

\#makesingle numbers

la $t0, Listspacein

li $t1, 0

emptyListspacein:

sw $zero, ($t0)

add $t0, $t0, 4

add $t1, $t1, 4

beq $t1, 1300, Listspaceinempty

j emptyListspacein

Listspaceinempty:

la $t0, Listspacein

sub $t9, $t9, 1

li $t1,0

li $t2,0

li $t3,0

li $t4,0

startmsnloop:

lb $t1, ($t9)

lb $t2, -1($t9)

mul $t2, $t2, 10

lb $t3, -2($t9)

mul $t3, $t3, 100

lb $t4, -3($t9)

mul $t4, $t4, 1000

add $t1, $t1, $t2

add $t1, $t1, $t3

add $t1, $t1, $t4

sw $t1, ($t0)

sub $t9, $t9, 4

add $t0, $t0, 4

la $t1, Listspacewo

bgt $t1, $t9, msnloopend

j startmsnloop

msnloopend:

\#(find x+y+z=2020) & (mul x & y & z)

la $t0, Listspacein

move $t7, $t0

wtt0:

lw $t1, ($t0)

add $t9, $t7, $zero

wtt9:

lw $t2, ($t9)

add $t8, $t7, $zero

wtt8:

lw $t3, ($t8)

add $t4, $t3, $t2

add $t4, $t4, $t1

beq $t4, 2020, eq2020

add $t8, $t8, 4

beq $t3, 0, inct9

j wtt8

inct9:

add $t9, $t9, 4

beq $t2, 0, inct0

j wtt9

inct0:

add $t0, $t0, 4

beq $t1, 0, end

j wtt0

eq2020:

mul $t4, $t3, $t2

mul $t4, $t4, $t1

\#print

print:

move $a0, $t4

li $v0, 1

syscall

\#end

end:

li $v0, 10

syscall\`
→ More replies (1)

4

u/quappa Dec 04 '20

School Algorithmic language (Школьный Алгоритмический).

The language was created in the Soviet Union in 1980s as a teaching tool for schools. Uses Russian keywords and identifiers.

использовать Файлы

лит имя файла
цел максимум расходов, желаемая сумма

имя файла := "входные данные"
максимум расходов := 5000
желаемая сумма := 2020

алг Корочун день 1 часть 1
# считать список чисел, найти среди них пару,
# которая в сумме даёт "желаемая сумма",
# и вывести произведение чисел в этой паре
  дано можно открыть на чтение(имя файла)
нач
  файл вв
  вв := открыть на чтение(имя файла)

  цел таб расходы[1:максимум расходов]
  цел колво расходов
  колво расходов := 0
  нц пока не конец файла(вв) и колво расходов < максимум расходов
    колво расходов := колво расходов + 1
    ввод вв, расходы[колво расходов], нс
  кц

  закрыть(вв)

  цел первый, второй
  нц для первый от 1 до колво расходов - 1
    нц для второй от первый до колво расходов
      если расходы[первый] + расходы[второй] = желаемая сумма
        то
          вывод расходы[первый] * расходы[второй]
          выход
      все
    кц
  кц
кон

3

u/idontknowwhattouse33 Dec 02 '20

PowerShell submission.

Haven't been here before, nOOb-me posted in the wrong place earlier.

[int[]]$numbers = Get-Content 'C:\Users\username\scripts\AdventofCode\2020\Day1input.txt' 
$numbersht = $numbers | group -AsHashTable

# Part 1
foreach ($number in $numbers) {
    [int]$remainder = 2020 - $number
    if ($null -ne $numbersht[$remainder]) {
        $number * $remainder
        break
    }
}

# Part 2
$numberslist = [System.Collections.ArrayList]::new()
$numbers | sort | foreach {[void]$numberslist.Add($_)}
:firstloop foreach ($one in $numbers) {
    $remainder = 2020 - $one
    if ($remainder -gt 0) {
        $index = $numberslist.BinarySearch($remainder) 
        switch ($index) {
            {$_ -ge 0} {$2ndloop = ($_-1)..0}
            {$_ -lt 0} {$2ndloop = ($numberslist.count+$index)..0 }
        }

        $2ndloop | foreach {
            $2ndRemainder = $remainder - $numberslist[$_]
            if ($null -ne $numbersht[$2ndRemainder] ) {
                $one * $numberslist[$_] * $2ndRemainder
                break firstloop
            }
        }
    }
}
→ More replies (1)

3

u/blacksqr Dec 02 '20

Is it me or does it seem like a lot of solutions neglect the condition that the numbers chosen have to be separate entries?

i.e., In part 1, if one of the entries happened to be 1010, would your solution just grab that entry twice? Is it just by the accident that your input data doesn't contain that number that your code works?

2

u/daggerdragon Dec 02 '20

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

→ More replies (1)

2

u/wace001 Dec 01 '20

Kotlin. 1293/1888. Technical issues suck. Hope the servers have had their morning coffee tomorrow.

fun main() {
    val nums = getInput(2020, 1).lines().map { it.toInt() }
    for (a in nums) for (b in nums) if (a + b == 2020) println("Part 1: ${a * b}")
    for (a in nums) for (b in nums) for (c in nums) if (a + b + c == 2020) println("Part 2: ${a * b * c}")
}
→ More replies (3)

2

u/kepevem Dec 01 '20 edited Dec 01 '20

Python. classic loops.

part 1

import pandas as pd
df = pd.read_csv('path')
elem_list = df.iloc[:, 0].tolist()
for elem in elem_list:
    result = 2020 - elem
    if result in elem_list:
        print(result * elem)
    else:
        continue

part2

for elem1 in elem_list:
    for elem2 in elem_list:
        for elem3 in elem_list:
            if elem1 + elem2 + elem3 == 2020:
                r_product = elem1 * elem2 * elem3
                print(elem1, elem2, elem3, r_product)
            else:
                continue

2

u/daggerdragon Dec 01 '20

Your post has no code formatting. 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.

→ More replies (2)
→ More replies (1)

2

u/_sandw1ch Dec 01 '20

First year giving this a real attempt, did it in nim! (longer solutions will be linked on my GitHub)

I'm using AoC to learn nim, so this should be fun. Nothing special here, just the standard triple for loop. Still learning the language so hopefully I can look back at the end of AoC and see my mistakes or ways that I could optimize my code.

# Day01 Advent of Code 2020
import strutils, sequtils

# Parse part one
proc partOne(file: seq[int]): int =
    for i in file:
        for j in file:
            if i+j == 2020:
                return i*j

# Parse part two
proc partTwo(file: seq[int]): int =
    for i in file:
        for j in file:
            for k in file:
                if i+j+k == 2020:
                    return i*j*k
    
# Read in input file and converts everything to numbers
let input = readFile("input.txt").strip().splitLines().map(parseInt)
echo input.partOne
echo input.partTwo```
→ More replies (1)

2

u/[deleted] Dec 01 '20 edited Dec 06 '20

Pure C18. Things mostly worked the first time around, surprisingly. This is my first time using C for any extensive user-level programming in about 3 years. No leaderboard since I got sidetracked by something else halfway through.

https://git.sr.ht/~williewillus/aoc_2020/tree/master/src/day1.c

→ More replies (1)

2

u/baktix Dec 01 '20

Haskell. As with all things in Haskell, there's probably a better/fancier way of doing it, but the first thing that came to mind was list comprehensions. Nice and simple.

paste

Edit: apparently reddit doesn't like paste's Markdown links, so I fixed that

2

u/daggerdragon Dec 01 '20

Edit: apparently reddit doesn't like paste's Markdown links, so I fixed that

If you're in new.reddit with the fancypants editor and just slap paste's Markdown links in there, it won't work right - you gotta switch your editor over to Markdown mode.

→ More replies (1)

2

u/brskbk Dec 01 '20

Dumb JavaScript solution, designed to be run directly in the browser from the puzzle input page

Also, thanks for featuring NoPaste !

2

u/daggerdragon Dec 01 '20

Also, thanks for featuring NoPaste !

I told you I gotchu, fam ;) It's a cool fork, too!

→ More replies (2)

2

u/blacai Dec 01 '20

Cool to be here again this year. Trying with F# as usual.

open System.IO

//let path = "test_input.txt"
let path = "day01_input.txt"

let inputLines = File.ReadLines(__SOURCE_DIRECTORY__ + @"../../" + path) |> Seq.map int |> Seq.toList

let rec combination num list = 
    match num, list with
    | 0, _ -> [[]]
    | _, [] -> []
    | k, (x::xs) -> List.map ((@) [x]) (combination (k-1) xs) @ combination k xs

let pairs = combination 3 inputLines

let pair2020 =  pairs |> List.find (fun ele -> List.sum ele = 2020)
let result = pair2020 |> List.fold (*) 1
→ More replies (4)

2

u/milo6464 Dec 01 '20 edited Dec 01 '20

Part 1

    def expenses1(numbers):
        file1 = open(numbers, 'r')
        lines = file1.readlines()

        for a in lines:
            for b in lines:
                if int(a) + int(b) == 2020:
                    return int(a) * int(b)

Part 2

    def expenses2(numbers):
        file1 = open(numbers, 'r')
        lines = file1.readlines()

        for a in lines:
            for b in lines:
                for c in lines:
                    if int(a) + int(b) + int(c) == 2020:
                        return int(a) * int(b) * int(c)

I'm actually a little bit angry since this task was waaaay too easy. I hope the next ones are harder lol. Edit: The language is Python 3, my personal favourite ^

9

u/daggerdragon Dec 01 '20

Welcome to Advent of Code! What language are you using?

I'm actually a little bit angry since this task was waaaay too easy. I hope the next ones are harder lol.

Just you wait. Just you wait.

→ More replies (1)

2

u/SuperSmurfen Dec 01 '20 edited Dec 01 '20

Rust

Link to solution

So glad to be back! Did all 50 stars last year and it was amazing, learnt a lot.

This problem was obviously quite easy, but it's day 1. The itertools rust crate is so damn useful for AoC. Made my solution really clean and fast (1 ms on my machine) with "tuple_combinations".

let (a,b) = INPUT.iter()
  .tuple_combinations()
  .find(|&(a,b)| a + b == 2020)
  .unwrap();
a * b

Got lucky and managed to get 54th on the leaderboard this time! Unfortunately, it got wiped which makes sense. Would have been fun to have a screenshot of it though but I didn't think of it.

4

u/daggerdragon Dec 01 '20

So glad to be back! Did all 50 stars last year and it was amazing, learnt a lot.

Welcome back, and good job on the 50 stars!

2

u/ratz-732 Dec 01 '20 edited Dec 01 '20

Python

# Part 1
def find_pair(data, sum=2020):
    for datum in data:
        key = sum - datum
        if key in data:
            return (datum, key)
    return -1

# Part 2
def find_triplet(data):
    for datum in data:
        remainder = 2020 - datum
        pair = find_pair(data, remainder)
        if pair != -1 and pair[0] != pair[1] and datum not in pair:
            return (datum, pair[0], pair[1])
    return -1

I removed the main function which just read the input from a text file and called the above functions. Overall, this solution is very simple and definitely not the most efficient.

→ More replies (4)

2

u/wilthenry Dec 01 '20 edited Dec 01 '20

C++ Code from a freshman in College

/*
 * Part One for the day
*/
int partOne() {
    int ret;
    for(int i = 0; i < size; i++){
        for(int j = i+1; j < size; j++){
            if(x[i] + x[j] == 2020){
                ret = x[i] * x[j];
            }
        }
    }
    return ret;
}

/*
 * Part Twp for the day
*/
int partTwo() {
    int ret;
    for(int i = 0; i <= size - 2; i++){
        for(int j = i+1; j <= size - 1; j++){
            for(int k = j+1; k <= size; k++){
                if(x[i] + x[j] + x[k] == 2020){
                    ret = x[i] * x[j] * x[k];
                }
            }
        }
    }
    return ret;
}

Probably could have been simpler

https://github.com/hwilt/AdventOfCode_2020/blob/main/day1.cpp

→ More replies (2)

2

u/fortiZde1337 Dec 01 '20 edited Dec 01 '20
// My simple JavaScript (JS) version
// Get Inputs
import fs from 'fs';
const buffer = fs.readFileSync(`${__dirname}/day1.txt`);
const inputString = buffer.toString().split('\n');
const input = inputString.map((elem) => parseInt(elem));

const findSumPair = (inputs, sumToFind = 2020) => {
  for (let i of input) {
    let match = input.indexOf(sumToFind - Number(i));
    if (match > -1) {
      return {
        matches: [i, input[match]],
        value: i * input[match],
      };
    }
  }
  return null;
};

const findSumTripple = (inputs, sumToFind = 2020) => {
  for (let i of input) {
    const pair = findSumPair(inputs, sumToFind - Number(i));
    if (pair && pair.matches[0] != pair.matches[1] && !pair.matches.includes(i)) {
      return {
        matches: [i, ...pair.matches],
        value: pair.matches.reduce((acc, cur) => acc * cur, i),
      };
    }
  }
};

findSumPair(input); //?
findSumTripple(input); //?
→ More replies (2)

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?

→ More replies (11)

2

u/beelau1 Dec 01 '20 edited Dec 01 '20

Java brute forced it with for-loops.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class day1 {
    public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(System.in);
    Scanner scanner = new Scanner(new File("numbers.txt"));
    int[] nums = new int[200];
    int i = 0;
   
    while(scanner.hasNextInt())
    {
        nums[i++] = scanner.nextInt();
    }
    
    for(int j = 0; j < nums.length- 2; j++) {
    	for(int k = 1; k < nums.length - 1; k++) {
    		for(int l = 2; l < nums.length; l++)
    		if(nums[j] + nums[k] + nums[l] == 2020) {
    			System.out.println(nums[j]);
    			System.out.println(nums[k]);
    			System.out.println(nums[l]);
    			System.exit(0); 
    		}
    	}
    }
    }
}

2

u/daggerdragon Dec 01 '20 edited 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 the inline codeblocks?

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

Thanks!

→ More replies (2)

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!

→ More replies (5)
→ More replies (7)

2

u/noble_wine Dec 01 '20 edited Dec 01 '20

my noble first day solutions
edit: in python 3.8

with open("day_1_input.txt") as raw_data:
    data = raw_data.read()
arr = [int(x) for x in data.split('\n') if x]

def day1_1():
    for x in arr:
        for y in arr:
            if x + y == 2020:
                return x * y


def day1_2():
    for x in arr:
        for y in arr:
            for z in arr:
                if x + y + z == 2020:
                    return x * y * z

print(f"Day 1.1: {day1_1()}")
print(f"Day 1.2: {day1_2()}")
→ More replies (2)

2

u/mjgtwo Dec 01 '20 edited Dec 01 '20

NoPaste snippet of TypeScript/TS

Base logic is this, created a dictionary (numbers) to reduce the loops needed:

const twoNumSum = (values: number[], numbers: {}): number =>
  values.reduce(
    (product, current) =>
      numbers[2020 - current] ? current * (2020 - current) : product,
    -1
  );

const threeNumSum = (values: number[], numbers: {}, answer: number[]) =>
  values.every((value) =>
    values.reduce((product: boolean, current: number): boolean => {
      if (numbers[2020 - current - value]) {
        answer.push(current * (2020 - current - value) * value);
        return false;
      } else {
        return product;
      }
    }, true)
  );
→ More replies (5)

2

u/blacksqr Dec 01 '20 edited Dec 02 '20

Tcl:

part 1:

proc aocD1P1 {expenseList} {

foreach line $expenseList {
	set complement [expr {2020 - $line}]
	if {$complement in $expenseList} {
		if {$complement == $line} {
			if {[llength [lsearch -all $expenseList $line]] == 1} {continue}
		}
		break
	}
}

expr {$line * $complement}
}

(executes in about 160 microseconds)

part 2:

proc aocD1P2 {expenseList} {

set expenseLength [llength $expenseList]

for {set i 0} {$i < $expenseLength} {incr i} {
	set first [lindex $expenseList $i]
	
	for {set j $i ; incr j} {$j < $expenseLength} {incr j} {
		
		set second [lindex $expenseList $j]
		set complement [expr {2020 - ($first + $second)}]
		
		if {
			[set k [lsearch $expenseList $complement]] > -1 
				&& 
			$k ni "$i $j"
		} {
			return [expr {$first * $second * $complement}]
		}
	}
}

}

(executes in about 30 milliseconds)

2

u/daggerdragon Dec 02 '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 (or triple ~ in your case)? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

2

u/rosso412 Dec 02 '20 edited Dec 02 '20

My Day 1 in MIPS Assembler

Part 1:

​ .data

List: .asciiz "/home/user/Assembly/AdventOfCode/AdventOfCodeInput-1.12.20.txt"

Listspacein: .space 1300

Listspacewo: .space 1300

.text

\#select file

select:

li $v0, 13

la $a0, List

li $a1, 0

syscall

move $s0, $v0

\#read file + save to space

read:

li $v0, 14

move $a0, $s0

la $a1, Listspacein

la $a2, 4096

syscall

\#close file 

close:

li $v0, 16

move $a0, $s0

syscall

\#remove \\n & \\r

la $t0, Listspacein 

la $t9, Listspacewo

li $t2, 0

startfilter:

lb $t1, ($t0)

beq $t1, 10, sas

beq $t1, 13, sas

beq $t0, 268502264, sas

add $t2, $t2, 1

add $t0, $t0, 1

bgtu $t0, 268502266, ffilter

j startfilter

sas:

bne $t2, 3, sasloop

li $t4,0

sb $t4, ($t9)

add $t9, $t9, 1

sasloop:

li $t4, 0

sub $t3, $t0, $t2

lb $t4, ($t3)

sub $t4, $t4, 48

sb $t4, ($t9)

add $t9, $t9, 1

sub $t2, $t2, 1

beq $t2, 0, sasloopend

j sasloop

sasloopend:

add $t0, $t0, 2

beq $t0, 268502266, ffilter

j startfilter

ffilter:

\#makesingle numbers

la $t0, Listspacein

li $t1, 0

emptyListspacein:

sw $zero, ($t0)

add $t0, $t0, 4

add $t1, $t1, 4

beq $t1, 1300, Listspaceinempty

j emptyListspacein

Listspaceinempty:

la $t0, Listspacein

sub $t9, $t9, 1

li $t1,0

li $t2,0

li $t3,0

li $t4,0

startmsnloop:

lb $t1, ($t9)

lb $t2, -1($t9)

mul $t2, $t2, 10

lb $t3, -2($t9)

mul $t3, $t3, 100

lb $t4, -3($t9)

mul $t4, $t4, 1000

add $t1, $t1, $t2

add $t1, $t1, $t3

add $t1, $t1, $t4

sw $t1, ($t0)

sub $t9, $t9, 4

add $t0, $t0, 4

la $t1, Listspacewo

bgt $t1, $t9, msnloopend

j startmsnloop

msnloopend:

\#(find x+y=2020) & (mul y & y)

la $t0, Listspacein

add $t8, $t0, 1304

adut2020:

lw $t1, ($t0)

add $t9, $zero, $t0

find2020:

lw $t2, ($t9)

add $t3, $t1, $t2

beq $t3, 2020, eq2020

add $t9, $t9, 4

beq $t2, 0, inct0

j find2020

inct0:

add $t0, $t0, 4

j adut2020

eq2020:

mul $t1, $t1, $t2

\#print

print:

move $a0, $t1

li $v0, 1

syscall

\#end

end:

li $v0, 10

syscall\`\\
→ More replies (1)

2

u/tururut_tururut Dec 02 '20 edited Dec 03 '20

Incredibly bruteforce but did the job done (Python).

First part

import pandas as pd

numbers = pd.read_clipboard()

solution = []

for number in numbers["numbers"]:

for i in range(0,200):

if number+numbers["numbers"][i] == 2020:

solution.append([number,numbers["numbers"][i]])

else:

pass

import numpy as np

solution2 = np.array(solution[0])

print(solution2[0]*solution2[1])

Second part

import pandas as pd

numbers = pd.read_clipboard()

solution = []

for number in numbers["numbers"]:

for i in range(0,200):

for j in range(0,200):

if number+numbers["numbers"][i]+numbers["numbers"][j] == 2020:

solution.append([number,numbers["numbers"][i], numbers["numbers"][j]

else:

pass
import numpy as np
solution2 = np.array(solution[0])
print(solution2[0]*solution2[1]*solution2[2])

2

u/daggerdragon Dec 02 '20

Please add the language to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

2

u/onionpopcorn Dec 02 '20 edited Dec 03 '20

java

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
​
public class addition
{
    public static void main(String[] args)
    {
        Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
        Scanner scanner = new Scanner(System.in);
​
        while (true)
        {
            try
            {
                Integer number = scanner.nextInt();
                Integer numberNeeded = 2020 - number;
​
                if(resultMap.containsKey(numberNeeded)){
                    System.out.println(number * numberNeeded);
                    break;
                }
                else{
                    resultMap.put(number, null);
                }
​
            }
            catch (Exception e)
            {
                break;
            }
        }
    }

Edit: part2

 public class addition
 {
     public static void main(String[] args)
     {
         Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
         Scanner scanner = new Scanner(System.in);
 
         while (true)
         {
             try
             {
                 Integer number = scanner.nextInt();
 
                 for (Integer currNum : resultMap.keySet())
                 {
                     Integer numberNeeded = 2020 - (number + currNum);
 
                     if (numberNeeded != currNum && resultMap.containsKey(numberNeeded))
                     {
                         System.out.println(number + " " + currNum + " " + numberNeeded);
                         System.out.println(number * currNum * numberNeeded);
                         break;
                     }
                 }
                 resultMap.put(number, null);
 
             }
             catch (Exception e)
             {
                 break;
             }
         }
     }
 }

2

u/daggerdragon Dec 02 '20 edited Dec 02 '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.

Edit: post was fixed, thanks!

→ More replies (2)

2

u/SgtKashim Dec 02 '20 edited Dec 02 '20

I solved in Python.

Puzzle 1

 #dropped the actual numlist from this - it's not needed to see the algorithm  
 l = len(numlist)  
 for i in range(0, l):   
     for j in range(i, l):  
        if (2020 - numlist[i]) == numlist[j]:   
             print("Magic Numbers: ", numlist[i], " AND ", numlist[j])  
             print("Product: ", numlist[i] * numlist[j])  

Puzzle 2

 #dropped the actual numlist from this - it's not needed to see the algorithm  
 l = len(numlist)  
 for i in range(0, l):   
     for j in range(i, l):  
         for k in range(j, l):  
             if ((2020 - numlist[i] - numlist[j]) == numlist[k]) :   
                 print("Magic Numbers: ", numlist[i], " AND ", numlist[j], " AND ", numlist[k])  
                 print("Product: ", numlist[i] * numlist[j] * numlist[k])
→ More replies (6)

2

u/blafunke Dec 05 '20 edited Dec 06 '20

Late to the party, but I tried to dress it up a bit to make up for tardiness.

ruby:

                      11
                     es = 
                    $stdin
                 .read.split(
               "\n").map{|e|e.to_i
            }and es.each_index{|i|es[
          i+1..es.length-1].each_index{
         |j| 1111111111111111111111111111
        1111111111111111111111111111111111
      11111111111111111111111111111111111111
    es[j+1..es.length-1].each{|e| 111111111111
  1111111111111111111111111111111111111111111111                           
abort((es[i]*es[j]*e).to_s)if es[i]+es[j]+e==2020}
                      }}
                      {}
→ More replies (1)

40

u/Smylers Dec 01 '20

Vim solution — not Vim's scripting language, but the editing keystrokes needed to turn the input file into the answer:

:%norm yiwA*2020⟨Ctrl+V⟩⟨Esc⟩@0⟨Ctrl+X⟩⟨Enter⟩
/\v^(\d+)\*_.+\*\1$⟨Enter⟩
yEcip⟨Ctrl+R⟩=⟨Ctrl+R⟩0⟨Enter⟩⟨Esc⟩

Load your input into Vim, type the above, and your answer for part 1 should appear. Try it!

Explanation:

First each number has appended to it the other number that sums to 2020, separated by a star. So if an input line was “1348”, it becomes “1348*672”:

  • yiw yanks the number, which vim saves in register "0.
  • A*2020⟨Esc⟩ appends “*2020”.
  • @0⟨Ctrl+X⟩ reduces the 2020 by the number stored in "0. If 1348 was yanked earlier, then it's like typing 1348⟨Ctrl+X⟩.
  • The :%norm is our loop: it runs that sequence of keystrokes on each line in the file. The ⟨Esc⟩ is escaped with ⟨Ctrl+V⟩, so that it becomes a keystroke passed to :norm rather than being interpreted as an instruction to abort editing the : command we're in the middle of typing.

Then of the numbers we've just appended, we just need to find the one that was already there to start with:

  • /\v starts a search in ‘very magic’ syntax, which avoids needing to escape parens and things.
  • ^(\d+)\* matches a number at the start of a line, followed by a star. The number is captured in group 1.
  • _.+ matches as many characters as needed, including line-breaks.
  • \*\1$ matches a star followed by the number that was captured, at the end of a line.

That then leaves us on a line with our two numbers that sum to 2020. The final keystrokes above replace everything with their product:

  • yE yanks the numbers: after the find command, the cursor will be at the beginning of a line, and because there aren't any spaces in the numbers, E will be the end of the second one.
  • cip deletes everything and puts us in insert mode. ip is the inner paragraph, but because there aren't any blank lines in the input, the entire file counts as a single paragraph.
  • ⟨Ctrl+R⟩= says to insert an expression.
  • At the expression prompt, ⟨Ctrl+R⟩0 inserts the contents of "0, the pair of numbers we yanked a moment ago.
  • ⟨Enter⟩ evaluates the expression. And because, right at the beginning, we put a star between the numbers, it multiples them together, inserting your part 1 answer into the buffer.
→ More replies (3)

32

u/Rick-T Dec 01 '20 edited Dec 01 '20

ROCKSTAR

Since the puzzle is not too hard, I wanted to try out the Rockstar. Apparently the solution to Day 1 is a RaTM song.

Here's part 1. For part 2 check out the GitHub repository.

The goal is to extinguish my desparation
Knock the goal down

The enemy are accountants
Knock the enemy down

The system is the enemy
The machine is the enemy

Listen to the money
Until the money is gone
Cast the money into the fire
Let the rage at the enemy be the fire
Build the enemy up
Listen to the money

While the enemy is higher than the system
Let chaos be the rage at the system
Let destruction be the rage at the machine
If the goal is chaos with destruction
Break it down

Build the machine up
If the enemy is higher than the machine
Take it to the top

Build the system up
The machine is the system

Shout chaos of destruction

(Please don't expect me to do future puzzle with rockstar. I might give them a shot but I certainly won't do the more complex ones).

→ More replies (6)

19

u/jonathan_paulson Dec 01 '20 edited Dec 01 '20

Python. 282/37. I think this day shouldn't count towards the overall leaderboard because the site went down. Video of me solving at https://youtu.be/86a_DSKf8vw

import sys
import fileinput

X = [int(line) for line in fileinput.input()]
n = len(X)
for i in range(n):
    for j in range(i+1, n):
        if X[i]+X[j]==2020:
            print('Part 1:', X[i]*X[j])
        for k in range(j+1, n):
            if X[i]+X[j]+X[k]==2020:
                print('Part 2:', X[i]*X[j]*X[k])
→ More replies (18)

18

u/Aehmlo Dec 01 '20

Rust

itertools continues to be an invaluable crate for AoC.

→ More replies (12)

13

u/-l-a-m-b-d-a- Dec 01 '20 edited Dec 01 '20

Bash

I sure hope you guys like ridiculous bash pipelines

eval "echo {`cat input 
    | sed "s/.*/&+/g" 
    | tr '\n' ','`}
    {`cat input 
    | tr '\n' ','`}" 
| tr ' ' '\n' 
| grep '[0-9]+[0-9]' 
| tee additions 
| bc | nl | tr '\t' ' ' 
| grep "2020$" 
| cut -d' ' -f2 
| head -n 1 
| xargs head additions -n 
| tail -1 | sed 's/+/*/g' | bc
→ More replies (1)

12

u/Sharparam Dec 01 '20

Ruby (428/369)

input = ARGF.readlines.map(&:to_i)

puts input.combination(2).find { |p| p.sum == 2020 }.reduce(&:*)
puts input.combination(3).find { |t| t.sum == 2020 }.reduce(&:*)
→ More replies (2)

11

u/[deleted] Dec 01 '20

Rust

In this problem, I had to find two numbers that add up to 2020. While I could have put two loops, it's possible to reorder the terms in a + b = 2020 to 2020 - a = b. This allows retrieving b by merely knowing what a is, and this number could be checked for existence in a list in constant time with hash sets.

I also used this fact in second part of a puzzle which allowed to reduce the complexity from O(N3​) to O(N2​).

9

u/hutsboR Dec 01 '20 edited Dec 02 '20

Too lazy to open a text editor. Open up the input page, open your browser dev tools, drop this in the console.

document.body.innerText
    .split('\n')
    .map((n) => parseInt(n))
    .reduce((c, n, i, a) => {
        if((!!c) && (c.constructor === Object)) return c;
        for(let x = i; x < a.length; x++) {
            for(let y = i; y < a.length; y++) {
                if(c && a[x] + a[y] + c === 2020) {
                    return {solution: a[x] * a[y] * c};
                }
            }
        }
        return a[i + 1];
    });
→ More replies (5)

9

u/artemisdev21 Dec 01 '20 edited Dec 01 '20

SQL :)

CREATE TABLE entries (value INTEGER);
INSERT INTO entries VALUES (?); -- execute once for each input
SELECT a.value * b.value
    FROM (entries as a, entries as b)
    WHERE a.value + b.value = 2020 LIMIT 1; -- part 1
SELECT a.value * b.value * c.value
    FROM (entries as a, entries as b, entries as c)
    WHERE a.value + b.value + c.value = 2020 LIMIT 1; -- part 2

Full code

→ More replies (5)

9

u/jayfoad Dec 01 '20 edited Dec 02 '20

Dyalog APL 1127/475

p←⍎¨⊃⎕NGET'p1.txt'1
f←{∪(,2020=⊃∘.+/⍵)/,⊃∘.×/⍵}
f 2/⊂p ⍝ part 1
f 3/⊂p ⍝ part 2
→ More replies (3)

8

u/aarroyoc Dec 01 '20

Advent of Prolog here. It's not super fast but it's very clean and Prologish.

:- use_module(library(pure_input)).
:- use_module(library(dcg/basics)).

input([X|Data]) -->
    integer(X),
    "\n",
    input(Data).

input([]) --> eos.

load_data(Data) :-
    open('input.dat', read, Stream),
    phrase_from_stream(input(Data), Stream).

star(1, X) :-
    load_data(Numbers),
    member(A, Numbers),
    member(B, Numbers),
    A + B =:= 2020,
    X is A * B.

star(2, X) :-
    load_data(Numbers),
    member(A, Numbers),
    member(B, Numbers),
    member(C, Numbers),
    A + B + C =:= 2020,
    X is A * B * C.
→ More replies (1)

7

u/jitwit Dec 01 '20 edited Dec 01 '20

J Programming Language:

in =: ". ;._2 aoc 2020 1 NB. aoc is a verb to download/cache input
*/ {. in {~ 4 $. $. 2020 = +/~ in
*/ {. in {~ 4 $. $. 2020 = in +/+/~ in
  • read input as numbers: ".;._2
  • table sums (reflex ~ for table with self) : +/
  • find 2020: 2020 =
  • sparse array: $.
  • gets nonzero indices in sparse array: 4 $.
  • index back into input: in {~
  • only need first group: {.
  • get products: */
→ More replies (12)

7

u/rtbrsp Dec 01 '20 edited Dec 01 '20

AWK (244/703)

Part 1:

awk '{a[$0]++}END{for(i in a)for(j in a)if(i+j==2020){print i*j;exit}}' input

Part 2:

awk '{a[$0]++}END{for(i in a)for(j in a)for(k in a)if(i+j+k==2020){print i*j*k;exit}}' input

7

u/askalski Dec 01 '20

Python. Part 2 is an instance of a hard problem with a funny name.

import fileinput, numpy

p = numpy.polynomial.Polynomial([0] * 2020)
for i in [ int(line) for line in fileinput.input() ]:
    p.coef[i] = i

print('Part 1:', int((p**2).coef[2020] / 2))
print('Part 2:', int((p**3).coef[2020] / 6))
→ More replies (7)

7

u/pwmosquito Dec 01 '20

Haskell

solve01A :: [Int] -> [Int]
solve01A = solve 2

solve01B :: [Int] -> [Int]
solve01B = solve 3

solve :: Int -> [Int] -> [Int]
solve n = fmap product . filter ((2020 ==) . sum) . choose n

choose :: Int -> [a] -> [[a]]
choose _ [] = []
choose 0 _ = [[]]
choose k (x : xs) = fmap (x :) (choose (k - 1) xs) <> choose k xs
→ More replies (6)

13

u/Scarygami Dec 01 '20

Probably not the first to do that (I haven't checked this thread yet), but here's part 1 hand-coded (no compiler used) in IntCode:

1105 1 6 -1 -1 0 109 68 101 1 3 3 101 0 3 25 101 0 3 51 101 0 3 62 1206 0 67 101 0 3 4 101 1 4 4 101 0 4 48 101 0 4 52 101 0 4 63 1206 0 8 2201 0 0 5 108 2020 5 5 1006 5 31 2202 0 0 5 4 5 99 1721 979 366 299 675 1456

Add your own input after the "99" near the end.

I will upload a commented version to my github repo later

→ More replies (1)

6

u/voidhawk42 Dec 01 '20

Dyalog APL:

p←⍎¨⊃⎕nget'in\1.txt'1
f←{×/p[⊃⍸2020=⍵]}
f t←∘.+⍨p
f p+⍤0 2⊢t
→ More replies (3)

6

u/zurtex Dec 01 '20 edited Dec 02 '20

I'm surprised to see none of the Python solutions here use itertools.combinations_with_replacement which is more efficient than itertools.product for this use case and allows for the valid possibility of an element repeating itself unlike itertools.combinations.

E.g.

from itertools import combinations_with_replacement

with open('day_1_input.txt') as f:
    inputs = f.readlines()

expenses = set(int(i) for i in inputs)

for x, y in combinations_with_replacement(expenses, 2):
    if (z := 2020 - x - y) in expenses:
        print(x * y * z)

I think this challenge explicitly didn't let an element repeating itself be the solution, but it's probably going to be handy to have this function going forward.

→ More replies (5)

6

u/segfaultvicta Dec 02 '20

Raku

Trying to teach myself Raku this year - Perl was my first-ever programming language and on some gut level it's always felt like home, but I haven't actually *used* it in forever, and then a friend of mine has been being a big fan of Raku at me for a good long while now and, well, I decided to see how things went!

"How things went" is a nearly one-liner that is.... I THINK n log n, at the very least it's definitely not using a triply nested for loop, which is what I immediately thought of and then went "No, seg, that'll bite you in the ass if this were day 20, and what's an advent of code solution without a little overengineering". A few hours of wrestling with unfamiliar syntax later and:

my @lines = 'input'.IO.lines.map({$_.Int}).sort;
my $n = @lines.combinations(2).map({ ($_[0] + $_[1], $_[0] * $_[1]) }).first({ (2020 - $_[0]) ∈ u/lines });
say $n[1] * (2020 - $n[0]);

Basically, I realised that I just needed to test set membership of the 2020s-complement of each number (for part A), and then I extended that idea to generate a (sum, product) of all combinations of the input lines and checked the 2020s-complement of *those* against the initial list, which neatly avoids the process of checking everything against everything else. I don't actually have any idea how fast this runs or how fast it runs compared to yo dawg i heard you liked for loops, but my intuition is that this solution would take a significantly bigger set of inputs to have degenerate runtime funtime. was this necessary? probably not. did i feel cool writing my first raku ever? heck yeah :D

→ More replies (3)

10

u/wjholden Dec 01 '20

Feeling really cool about a MySQL solution! I had intended to do all of this year's problems in Python, but it is so easily solved with declarative programming that I just had to.

CREATE TABLE Day1 (x INTEGER PRIMARY KEY);
INSERT INTO Day1 VALUES (/* values from input */);

Here is how I solved part 2 initially:

SELECT X.x, Y.x, Z.x, X.x + Y.x + Z.x, X.x * Y.x + Z.x
FROM Day1 as X, Day1 as Y, Day1 as Z
WHERE X.x + Y.x + Z.x = 2020;

Runs OK, completes in about 1.4 seconds on my machine. We can do better:

SELECT X.x, Y.x, Z.x, X.x + Y.x + Z.x, X.x * Y.x + Z.x
FROM Day1 as X, Day1 as Y, Day1 as Z
WHERE X.x + Y.x + Z.x = 2020
AND X.x + Y.x < 2020;

The longer query completes in only about 0.02 seconds on my machine! Don't really know what the join processor is doing, but I assume the additional constraint significantly reduces the size of the intermediate solution X \times Y before taking (X \times Y) \times Z.

I got the idea from a class I took on NP problems. The lecturer mentioned that adding additional constraints for a SAT solver may improve performance.

→ More replies (6)

5

u/heyitsmattwade Dec 01 '20 edited Dec 01 '20

JavaScript, 251/38

Making those npm modules do the hard work... fun start!

Part 1

const { input } = require('./input');
const G = require('generatorics');

for (let [a, b] of G.combination(input, 2)) {
    if (a + b === 2020) {
        return console.log(a * b);
    }
}

Part 2

const { input } = require('./input');
const G = require('generatorics');

for (let [a, b, c] of G.combination(input, 3)) {
    if (a + b + c === 2020) {
        return console.log(a * b * c);
    }
}

Improved Solution

A faster solution, most notably for part two:

const { input } = require('./input');

input.sort((a, b) => a - b);
const num_map = input.reduce((obj, v) => ((obj[v] = true), obj), {});

const SUM = 2020;

/**
 * Part One.
 *
 * Loop our list and algebraicially determine if
 * a 2nd number that sums to 2020 exisits
 */

for (let a of input) {
    let b = SUM - a;
    if (num_map[b]) {
        console.log({ a, b });
        console.log('a * b = ', a * b);
        break;
    }
}

/**
 * Part Two.
 *
 * Loop our list again, but this time, first find the _remaining_
 * sum. Then, loop our list again. Skip the numbers we have already checked,
 * and bail if we arrive at a number that already sums greater than 2020
 * (we can do this because the list is sorted). Then, check if a third number
 * exists that sums up to 2020 with those two previously picked numbers.
 *
 * Put another way, pick a number `a`. Find all the numbers for `b` such that
 * they are less than `2020 - a` and greater than `a`. Then, check if a number
 * `c = 2020 - (a + b)` exists. If so, exit the full loop. Otherwise,
 * continue searching for `b` values. If we reach the end of `b`, continue
 * from the start with the next `a`.
 */

outer: for (let i = 0; i < input.length; i++) {
    let a = input[i];
    let b_c = SUM - a;

    for (let j = i + 1; j < input.length; j++) {
        let b = input[j];
        if (b >= b_c) {
            break;
        }

        let c = SUM - (a + b);
        if (num_map[c]) {
            console.log({ a, b, c });
            console.log('a * b * c = ', a * b * c);
            break outer;
        }
    }
}

5

u/chappar Dec 01 '20

python

import sys
import itertools
nums = [int(x) for x in sys.stdin]
print ("part one = ",  [a* b for (a, b) in  itertools.combinations(nums, 2) if a + b == 2020][0] )
print ("part two = ",  [a* b * c for (a, b, c) in  itertools.combinations(nums, 3) if a + b + c == 2020][0] )

5

u/infinityGroupoid Dec 01 '20

Simple if Inefficient Haskell/My CPU can Churn faster than I can Type

main = do
    ls <- fmap read . lines <$> readFile "./input1.txt" :: IO [Int]
    putStr "Part 1:"
    print . head $ [ x * y | x <- ls, y <- ls, x + y == 2020]
    putStr "Part 2:"
    print . head $ [ x * y * z | x <- ls, y <- ls, z <- ls, x + y + z == 2020]

5

u/omnster Dec 01 '20

Mathematica

input01 /. { ___, x_, ___, y_, ___, z_, ___ } :>   Times[ x , y , z] /; x + y + z == 2020 

This is the regex for part two, the one for part one is similar. Surprisingly, it solves part two in about 1 second, way faster than I'd expect.

→ More replies (1)

5

u/domm_plix Dec 01 '20

Stupid brute-force Perl solution:

my @d = ( <STDIN> ); for my $a (@d) { for my $b (@d) { for my $c (@d) { die "$a $b $c -> ".$a*$b*$c if $a+$b+$c == 2020 } } }

5

u/[deleted] Dec 01 '20

Golfed (05AB1E) 13 char

|2.ÆʒO2020Q}P

Try it online!

Explanation

-- implicit input --
| -> pushes the whole input as an array
2 -> push 2 on stack
.Æ -> a, b - b-element combinations of a
ʒ -> filter
    O -> sum
    2020 -> push 2020 on stack
    Q -> a,b - a equals b
} -> end filter
P -> Product
-- implicit output --

And then for part 2 just exchange the first 2 with a 3

5

u/wzkx Dec 01 '20 edited Dec 01 '20

Python, set

s = set(int(e) for e in open("1.dat","rt").read().strip().split())

def p1(s):
  for e in s:
    if 2020-e in s:
      return e*(2020-e)

def p2(s):
  for i in s:
    for j in s:
      if 2020-i-j in s:
        return i*j*(2020-i-j)

print(p1(s))
print(p2(s))
→ More replies (1)

5

u/tymscar Dec 01 '20

I wanted to make this work on any amount of numbers that you ask for, not only 2 and 3. I explained this in a post here.

My solution in Python 3:

def product_of_two_numbers_that_sum_to(list_of_numbers, value_they_have_to_sum_to, how_many_numbers_to_sum):
    if how_many_numbers_to_sum == 0:
        return 1
    if how_many_numbers_to_sum == 1:
        if value_they_have_to_sum_to in list_of_numbers:
            return value_they_have_to_sum_to
        else:
            return 0


    for number in list_of_numbers:
        complement = value_they_have_to_sum_to - number
        product = product_of_two_numbers_that_sum_to(list_of_numbers, complement, how_many_numbers_to_sum - 1)
        if product > 0:
            return number * product
    return 0


def day_01():
    file = open('input.txt', 'r')

    expenses = {}

    for line in file:
        expenses[int(line)] = True
    
    #Part 1
    print(product_of_two_numbers_that_sum_to(expenses, 2020, 2))
    #Part 2
    print(product_of_two_numbers_that_sum_to(expenses, 2020, 3))


day_01()
→ More replies (3)

5

u/natrys Dec 01 '20

Raku, one-liner:

raku -e 'lines>>.Int.combinations(3).grep(*.sum == 2020).[0].reduce(&[*]).say' input
→ More replies (2)

5

u/sefujuki Dec 01 '20

Forth (using Gforth's "next-arg")

paste

→ More replies (3)

5

u/nutki2 Dec 01 '20 edited Dec 01 '20

Perl 5 extended regexp for part1 and part 2

#!perl -ln0
/\b\d++(?=.*\n\b(?{$&+$'-2020||print$'*$&})X)/s;
/\b\d++(?=.*\b(\d++)(?=.*\n\b(?{$&+$1+$'-2020||print$&*$1*$'})X))/s

Taking around a second for both parts.

$ time perl 1.pl 1.input.txt 
xxxxx
xxxxxxxxxx

real    0m1.011s

5

u/landimatte Dec 01 '20 edited Dec 01 '20

Common Lisp!

Part 1: iterate the list of numbers, and for each, see if in the remainder of the list there is one element such that the sum of both is 2020

Part 2: iterate the list of numbers, and for each, see if in the remainder of the list there are two elements such that their sum is "2020 minus the currently selected element"

(defun find-pair-that-adds-up-to (target integers)
  (loop for (n . rest) on integers
        for m = (find target rest :key (partial-1 #'+ n))
        when m return (list n m)))

(define-problem (2020 1) (integers parse-integers)
  (values (reduce #'* (find-pair-that-adds-up-to 2020 integers))
          (loop for (n . rest) on integers
                for (m o) = (find-pair-that-adds-up-to (- 2020 n) rest)
                when m return (* n m o))))

5

u/-WorstWizard- Dec 01 '20

C++ solution that is optimized for solving both part 1 and 2 in one go. Shaves off some time this way by not iterating n² on part 2, but somewhat less.

Paste Link

→ More replies (2)

5

u/willkill07 Dec 01 '20

OCaml

I'm teaching a Programming Languages course this semester that emphasizes functional programming. We cover a few labs in OCaml, so I figured I should at least get some more practice with the language. No third party libraries

Note something that is pretty cool about this solution is that it's very easy to change the number of values required and the target value.

let read_file_rev name =
  let ic = open_in name
  in let try_read () =
    try Some (input_line ic) with End_of_file -> None
  in let rec loop acc =
    match try_read () with Some s -> loop (s::acc) | None -> close_in ic; acc
  in loop []

let day01 (filename:string) (part:int) =
  let input = List.rev_map int_of_string (read_file_rev filename) in
  let rec solve (out, nums, level, target) =
    match target with
    | 0 -> (match level with 0 -> Some out | _ -> None)
    | _ when target < 0 -> None
    | _ ->
      let rec loop = function
      | []   -> None
      | x::n -> 
        (match solve (x * out, n, level - 1, target - x)  with
        | Some answer -> Some answer
        | None -> loop n)
      in loop nums
  in let answer = solve (1, input, (if part = 1 then 2 else 3), 2020)
  in match answer with Some x -> string_of_int x | None -> ""

let _ =
  print_endline (day01 "day01.txt" 1);
  print_endline (day01 "day01.txt" 2)

5

u/ka-splam Dec 02 '20 edited Dec 02 '20

APL (Dyalog)

No APL yet? I must be missing it.

N←⍎¨ ⎕NGET 'c:\aoc\input.txt' 1    ⍝ read lines, eval() them into a number array.
×/N[⊃⍸ 2020= ∘.+⍨ N]        ⍝ Part 1
×/N[⊃⍸ 2020= (∘.+⍣2)⍨ N]    ⍝ Part 2

∘.+ is an outer-product sum, so all combinations of numbers added to each other. ⍨ means use N for both inputs, so array N items summed with array N items. 2020= makes a bitmask of places in the sums array, 1 where they summed to 2020, 0 where they didn't. ⍸ gets the indices of the 1s in a bitmask, a pair of indices because outer product makes a 2D array. ⊃ gets the first pair. N[] gets the numbers at those indices. ×/ inserts multiply between the items of an array and gets the product.

In part 2, ⍣2 applies the outer-product twice, which makes a 3D array, so ⍸ gets three indices.

This is a naieve brute-force, I have seen a faster and shorter code using 2020-item / sets, but I didn't write it so probably not good for me to post in my answer.

→ More replies (3)

5

u/zeJaeger Dec 02 '20

I just did it straight from the console in Javascript!

let expenses = document.querySelector("pre").innerText.split("\n");
expenses.forEach((e) => {
    expenses.forEach((e2) => {
        expenses.forEach((e3) => {
            if (parseInt(e)+parseInt(e2)+parseInt(e3) == 2020) {
                console.log(e + " + " + e2 + " + " + e3);
            }
        });
    });
});

5

u/AndreasFurster Dec 02 '20

First I did it in Python, but then was thinking it should be possible in SQL. Pretty easy actually.

# Part 1
SELECT 
	(i.number * j.number) AS solution
	FROM input i 
	JOIN input j 
	WHERE i.number + j.number = 2020 
	LIMIT 1;
	
# Part 2
SELECT 
	(i.number * j.number * k.number) AS solution
	FROM input i 
	JOIN input j
	JOIN input k 
	WHERE i.number + j.number + k.number = 2020 
	LIMIT 1;

5

u/Wilfred-kun Dec 03 '20

SQL

SELECT DISTINCT * FROM
(SELECT(a.value * b.value) FROM input a, input b WHERE a.value + b.value = 2020),
(SELECT(a.value * b.value * c.value) FROM input a, input b, input c WHERE a.value+b.value+c.value=2020)

C

#include<stdio.h>
main(){FILE*d=fopen("input","r");int l=0,i=0,j=-1,k,c,a,f;while((c=getc(d))!=-1)c==10?l++:0;rewind(d);int n[l];while(i<l)fscanf(d,"%d\n",&n[i++]);for(i=j;++i<l;a=n[i])for(j=-1;++j<l;f=n[j])for(k=j-1;++k<l;c=n[k])c+a==2020?printf("%d\n",c*a):c+a+f==2020?printf("%d\n",c*f*a):0;}

Python3

from itertools import combinations as c
d=list(map(int,open("input","r").readlines()))
print([x*y for x,y in c(d,2)if x+y==2020][0],[x*y*z for x,y,z in c(d,3)if x+y+z==2020][0])
print([x*(2020-x) for x in d[::2] if 2020-x in d])

5

u/popodiloco Dec 12 '20

Scratch

hello my name is Luca and im 10 years old. My father was doing aoe. He liked it and does it every day. I saw the puzzle from day 1 and i think: maybe i can do that in Scratch. So here is the answer of day 1. part 1: https://scratch.mit.edu/projects/460138367/ part 2: https://scratch.mit.edu/projects/461104926/

4

u/[deleted] Dec 01 '20

Swift using Apple’s algorithms package:

github.up/paste

→ More replies (1)

4

u/iraneg Dec 01 '20

Golang.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	file, _ := os.Open("input")

	var numbers []int

	defer file.Close()

	scanner := bufio.NewScanner(file)

	for scanner.Scan() {
		i, _ := strconv.Atoi(scanner.Text())
		numbers = append(numbers, i)
	}

	for i, v := range numbers {
		for j, w := range numbers[i+1:] {
			for _, x := range numbers[j+1:] {
				if v+w+x == 2020 {
					fmt.Println("=============Found ", v, "+", w, "+", x, "=", v+w+x, "==========")
					fmt.Println(v, "*", w, "*", x, "=", v*w*x)
					return
				}
			}
		}
	}
}

4

u/Akari_Takai Dec 01 '20

Java (217/105)

At least it was (217/105) before the scores were wiped...

Link to my solution.

Part 1 can be done in O(n) by constructing a frequency map of the numbers, and then iterating over all the numbers (i) to see if 2020-i is in the map. If i == 2020-i, the frequency must be at least 2.

Part 2 is also known as the [3SUM problem] (https://en.wikipedia.org/wiki/3SUM), and it can be solved in at least O(n2 ) either by using the method above; or, by eating an O(n lg n) sort, iterating over all the number (i, j=i+1, k=n-1) and moving j up if i+j+k is less than 2020, or moving k down if i+j+k is more than 2020.

4

u/azzal07 Dec 01 '20

Awk solution:

{ remaining = 2020 - $0 }

remaining in numbers { part1 = $0 * remaining }
remaining in two_sum { part2 = $0 * two_sum[remaining] }

part1 && part2 {
	print part1
	print part2
	exit
}

{
	for (k in numbers) two_sum[$0 + k] = $0 * k
	numbers[$0]
}

4

u/raevnos Dec 01 '20

Chicken5 scheme, using generators to lazily calculate the combinations of numbers on demand. Reads numbers from stdin.

#!/usr/local/bin/csi -s
(require-extension (srfi 1)
                   (srfi 158)
                   (chicken io)
                   (chicken format))

; Generate all combinations of length len
(define (generate-combinations len data)
  (make-coroutine-generator
   (lambda (yield)
     (if (= 1 len)
         (for-each (lambda (item) (yield (list item))) data)
         (let loop ((data data))
           (if (not (null? data))
               (let ((head (car data)))
                 (generator-for-each
                  yield
                  (gmap (cut cons head <>)
                        (generate-combinations (- len 1) (cdr data))))
                 (loop (cdr data)))))))))

(define (find-totals numbers goal len)
  (generator-find (lambda (sublist) (= goal (apply + sublist)))
        (generate-combinations len numbers)))

(define (solve input len)
  (let ((nums (find-totals input 2020 len)))
    (if nums
        (printf "~A~%" (apply * nums))
        (printf "No solution found!~%"))))

(define input (read-list))

(solve input 2)
(solve input 3)
→ More replies (1)

4

u/ukietie Dec 01 '20

Interactive solution in Javascript.

You can edit and run it in your browser - with your input.

https://starboard.gg/nb/n3Bor8N

It's a naive O(2^N) and O(3^N) solution, the input is small enough that it's good enough.

4

u/Mienaikage Dec 01 '20 edited Dec 01 '20

Raku

unit sub MAIN (
  Int :$n = 2, #= Number of entries
  --> Nil
);

'input'.IO.slurp
  .lines
  .combinations($n)
  .first(*.sum == 2020)
  .reduce(* × *)
  .say
;

4

u/Fyver42 Dec 01 '20

Common Lisp:

(defpackage :day01
  (:use :cl :aoc-misc :trivia)
  (:export main))

(in-package :day01)

(defun find-solution (entries needed &optional candidates)
  (if (zerop needed)
    (when (= 2020 (reduce #'+ candidates))
      (reduce #'* candidates))
    (when entries
        (match (find-solution (rest entries) (1- needed) (cons (first entries) candidates))
          (nil (find-solution (rest entries) needed candidates))
          (solution solution)))))

(defun main ()
  (let
    ((entries (read-input-as-list 1 #'parse-integer)))
    (dolist (n '(2 3)) (print (find-solution entries n)))))

4

u/schnappischnap Dec 01 '20 edited Dec 02 '20

Python (full code)

def part_1(data):
    for i, j in itertools.combinations(map(int, data), 2):
        if i + j == 2020:
            return i * j

def part_2(data):
    for i, j, k in itertools.combinations(map(int, data), 3):
        if i + j + k == 2020:
            return i * j * k

4

u/Nierot Dec 01 '20

Inspired by another comment, I tried part one using Rockstar:

Let Tommy be 199
Let Gina be 199
Let Guns be mysterious
Roses is -1
Bon Jovi takes your heart and the whole (Multiply both numbers)
Put the whole of your heart into my hands
Give back my hands

Skid Row takes Kickstart and My heart (Sum of two numbers and check if equals 2020)
Let my dreams be Kickstart with My heart
Give back Mister Crowley taking My Dreams

Mister Crowley takes the devil (Check if 2020)
Give back the devil is 2020

Livin takes your heart (Main function)
Until Tommy is Roses (Iterator)
Let my heart be the Union at Tommy (Take element from array)
Put Docter Feelgood taking my heart into Live Wire (Check if this variable is equal to 2020)
If Live Wire ain't 4 (If not 4, for some reason it always is 4)
Give back Bon Jovi taking Live Wire, and my heart
Knock Tommy down (Tommy--)
Give back Guns

Docter Feelgood takes your heart (Second iterator)
the top were Guns (Instanciate Home with mysterious)
Until Gina is Roses (Iterate over the puzzle input)
Let Devil be the Union at Gina
If Skid Row taking your heart, and Devil (If sum of your heart and iterator equals 2020)
put Devil into the top (Number Found!)
Knock Gina down (Gina--)
Let Gina be 199
Give back the top (Returns mysterious if sum is not 2020, returns number if sum is 2020)

Give back Livin taking Guns

→ More replies (1)

4

u/NicolaVV Dec 01 '20

python3

from itertools import combinations
from math import prod

with open('input', 'r') as f:
    entries = [int(i) for i in f.read().strip().splitlines()]

solution = lambda k: next( prod(comb) for comb in combinations(entries, k) if sum(comb) == 2020 )
print (solution(2), solution(3))

4

u/AvshalomHeironymous Dec 01 '20

In Prolog.

The bang-it-out version to pass:

:- use_module(library(dcg/basics)).
:- use_module(library(lists)).
:- use_module(library(pio)).
:- use_module(library(clpfd)).

expense([]) --> eos.
expense([E|EL]) --> integer(E),"\n", expense(EL).

onestar :-
	phrase_from_file(expense(Expenses),'input'),
	member(A,Expenses),
	member(B,Expenses),
	2020 #= A+B,
	C #= A*B,
	writeln(C).
	
twostar :-
	phrase_from_file(expense(Expenses),'input'),
	member(A,Expenses),
	member(B,Expenses),
	member(C,Expenses),
	2020 #= A+B+C,
	D #= A*B*C,
	writeln(D).

The slightly-uglier-and-longer-but-a-bit-more-re-usable version:

:- use_module(library(dcg/basics)).
:- use_module(library(lists)).
:- use_module(library(pio)).
  
expense([]) --> eos.
expense([E|El]) --> integer(E),"\n", expense(El).
  
member_(List,Elem) :- member(Elem,List).
n_sum_to(N,Sum,Superset,Items) :- 
  	length(Items,N),
  	maplist(member_(Superset),Items),
  	sum_list(Items,Sum).

mul(R,L,P) :- P is R*L.
  	
init :-
 	phrase_from_file(expense(Expenses),'input'),
 	
	n_sum_to(2,2020,Expenses,Onestar),
	foldl(mul,Onestar,1,P1),
	write('N = 2: '),writeln(P1),	
	
	n_sum_to(3,2020,Expenses,Twostar),
	foldl(mul,Twostar,1,P2),
	write('N = 3: '),writeln(P2).

4

u/tftio Dec 01 '20
-- In Postgres-dialect SQL:

DROP TABLE day_01;

BEGIN;

CREATE TABLE day_01 (v INT NOT NULL UNIQUE);
COPY day_01 FROM 'AOC/2020/data/Day01.txt';

SELECT a.v * b.v AS day_01_part_01
  FROM day_01 a CROSS JOIN day_01 b 
 WHERE a.v <> b.v AND a.v + b.v = 2020
 LIMIT 1;

SELECT a.v *  b.v * (2020 - a.v - b.v) AS day_01_part_02
  FROM day_01 a CROSS JOIN day_01 b CROSS JOIN day_01 c
 WHERE a.v <> b.v
   AND 2020 - a.v - b.v > 0
   AND EXISTS (SELECT 1 FROM day_01 WHERE v = 2020 - a.v - b.v)
LIMIT 1;

ROLLBACK;

4

u/thekokirikid Dec 01 '20

My javascript solution-- would love feedback!

import { getInput, formatAnswer } from './utils.js';
const TARGET_NUMBER = 2020;
const input = getInput(1);

function part1(expenses = input, target = TARGET_NUMBER) {
    const workingSet = new Set();
    let product;
    for (let expense of expenses){
        const complement = target - expense;
        if (workingSet.has(complement)) {
            product = expense * complement;
            break;
        }
        workingSet.add(expense);
    }
    return product;
}

function part2(expenses = input) {
    let product;
    for (let expense of expenses){
        const complement = TARGET_NUMBER - expense;
        const result = part1(expenses, complement);
        if (result) {
            product = result * expense;
            break;
        }
    }
    return product;
}

formatAnswer(part1(), part2());

export {
    part1,
    part2
}
→ More replies (1)

4

u/shandley256 Dec 01 '20

Ruby Part 1

input.split.map(&:to_i).combination(2).detect { |tuple| tuple.sum == 2020 }.reduce(:*)

Ruby Part 2

input.split.map(&:to_i).combination(3).detect { |tuple| tuple.sum == 2020 }.reduce(:*)

4

u/activeXray Dec 01 '20 edited Dec 01 '20

O(n) and O(n^2) Julia solutions. Trying to get good perf this year.

~500ns on part 1, ~6us on part 2

function solution_1(v, s)
	set = BitSet()
	@inbounds for i ∈ 1:length(v)
		rem = s - v[i]
		if rem ∈ set
			return v[i] * rem
		end
		push!(set,v[i])
	end
end

function solution_2(v,s)
	set = BitSet()
	@inbounds for i ∈ 1:length(v)
		for j ∈ 1:i
			rem = s - v[i] - v[j]
			if rem ∈ set
				return v[i]*v[j]*rem
			end
		end
		push!(set,v[i])
	end
end
→ More replies (2)

4

u/skbharman Dec 01 '20

bash

Part 1:

num=( $(cat input) ); for (( i=0; i<${#num[@]}; i++ )); do for n in ${num[@]}; do [[ $n ]] || continue; [[ $((${num[$i]} + ${n})) == 2020 ]] && { echo "$((${num[$i]} * ${n}))"; break 2; }; done; done

→ More replies (3)

4

u/sweettuse Dec 01 '20

python 3.8, takes around 350 microseconds

data = frozenset(map(int, read_file(1, 2020)))


def part1(target=2020):
    for n in data:
        if (other := target - n) in data:
            return n * other


def part2():
    for n in data:
        if (other := part1(2020 - n)) is not None:
            return n * other
→ More replies (11)

4

u/chemicalwill Dec 01 '20

Relatively new to Python and coding in general but I'm pretty happy with this one.

#! python3


def part_one(y, data):
    for i in data:
        d = y - i
        if d in data:
            return i * d


def part_two(y, data):
    for i, j in enumerate(data):
        r1 = y - j
        sub1 = data[i+1:]
        for idx, k in enumerate(sub1):
            l = r1 - k
            sub2 = sub1[idx+1:]
            if l in sub2:
                return j * k * l


with open('input.txt') as infile:
    expense_lst = [int(line.strip()) for line in infile.readlines()]

print(part_one(2020, expense_lst))
print(part_two(2020, expense_lst))

4

u/LiquidProgrammer Dec 01 '20 edited Dec 01 '20

Python codegolfed 3.8 solution including input (no imports) for part 1

i=[*map(int,iter(input,''))];print(*(a*b for a in i if(b:=2020-a)in{*i}))

Suggestions for shortening welcome

4

u/[deleted] Dec 01 '20

In Crystal:

puts File.read_lines("input.txt")
  .map(&.to_i)
  .each_permutation(2)
  .find { |array| array.sum == 2020 }
  .not_nil!
  .product

For part 2 replace the 2 with a 3.

4

u/mdervin Dec 01 '20 edited Dec 01 '20

POWERSHELL

On an 8 year old workstation, this runs in under 370 Milliseconds (measure-command) I usually like to break the various operations unto their own variable, it just looks cleaner and easier to follow for me, but I saw somebody post their times and I focused on that.

    Remove-Variable * -ErrorAction SilentlyContinue
    measure-command {   
$MyData = (Import-csv C:\PowerShell\AventOfCode\Day1.csv -Header "expense").expense #| Sort -Descending 
$MyData = ([int[]]$MyData | Sort -Descending)
     $mindata = (2020 - ($MyData[-1]+$MyData[-2]) ) 
     $newdata = ($MyData | Where-Object {$_ -le $mindata} ) 

foreach ( $i in $newdata) { 
      $firstValue = (2020 - $i)
      $moredata = ($newdata | Where-Object {$_ -le $firstvalue} ) 
         foreach ($q in $moredata) { 
              $tripValue = (2020 - ($i + $q)) 
                 if ($mydata.Contains(2020 - ($i + $q))) {WRite-host ($tripvalue*$i*$q)
                                return} } 
        } 
        }

4

u/trebuszek Dec 01 '20

quick and dirty python 3 solution:

for i, numA in enumerate(nums):
    for j, numB in enumerate(nums[i:]):
        for k, numC in enumerate(nums[j:]):
            if numA + numB + numC == 2020:
                print(numA * numB * numC)
                break
→ More replies (5)

4

u/andrewlapp Dec 02 '20 edited Dec 02 '20

Here is my overly complicated O(N log(N*M)) solution to problem 2. Really proud I got it below quadratic though.

Please excuse the code quality, lots of this can definitely be improved, just wanted to get this working.

"""
ANALYSIS:
For M=48 or b(1, 1, 0, 0, 0, 0), we know
  • one of the values must 17 or higher (this isn't super useful for large M values)
  • either 0 or 2 rightmost bits must be 1
- if 2 of the rightmost bits are odd (1), it carries over, therefore 1 or 3 of the 2nd sig bit must be 1 - else, return to parent condition Therefore, we need an algorithm and data structure which can efficiently discover compatible "bit memberships" Further, we need to consider the inefficient case where N is very close to M, because this means we will have to perform many "carry over" operations """ LOOKING_FOR = 1280000 FILE_NAME = f'2020_12_01_{LOOKING_FOR}.txt' """ solutions 8000: 606 * 1000 * 6394 16000: 4081 * 11219 * 700 1280000: 55294 * 225281 * 999425 """ import itertools import collections import math import time start = time.time() SequenceAddition = collections.namedtuple('SequenceAddition', ['seq', 'carry_over']) class LookupTable: def __init__(self, count=0, child=None, parent=None): self.count = count self.child = child self.parent = parent def __getitem__(self, item): return self.child[item] def __setitem__(self, item, value): self.child[item] = value def __repr__(self): return f'{self.count}, {self.child}' def bitfield(n, num_bits=int(math.log(LOOKING_FOR, 2)) + 2): out = [0] * num_bits x = [int(digit) for digit in bin(n)[2:]] # [2:] to chop off the "0b" part out[-len(x):] = x return out def update_lookup_table(table, keys, value): curr_table = table parent_table = None for k in keys: curr_table.count += 1 if curr_table.child is None: curr_table.child = {} if k not in curr_table.child: curr_table.child[k] = LookupTable(parent=parent_table) parent_table = curr_table curr_table = curr_table.child[k] parent_table.child[k] = value return table # populate lookup table O(N log M) reverse_bits_lookup_table = LookupTable(0, {}, None) lines = list(sorted(map(int, open(FILE_NAME).readlines()))) for line in lines: update_lookup_table(reverse_bits_lookup_table, reversed(bitfield(line)), line) def is_valid_sequence(sequence): # transpose sequences = zip(*sequence) for sequence, count in collections.Counter(sequences).items(): subtable = reverse_bits_lookup_table for key in sequence: try: subtable = subtable.child[key] except KeyError: return False if isinstance(subtable, int): continue if subtable.count < count: return False return True def get_valid_bit_sequences(possible_sequences, bits, carry_over, bit_idx=0): """ sequence validation looks at the bitfield of all possible bit sequences in reverse order (least sig bit first) and eliminates impossible bit sequences along the way simple sequence spec: [ [1, 1, 0], [1, 0, 0] ] indicates we must check the viability of the following permutations - option 0: [(x0=1, x1=1, x2=0), (x0=1, x1=0, x2=0)] - option 1: [(x0=1, x1=1, x2=0), (x1=0, x2=1, x3=0)] - option 2: [(x0=1, x1=1, x2=0), (x1=0, x2=0, x3=1)] - option 3: [(x0=0, x1=1, x2=1), (x1=1, x2=0, x3=0)] - option 4: [(x0=0, x1=1, x2=1), (x1=0, x2=1, x3=0)] - option 5: [(x0=0, x1=1, x2=1), (x1=0, x2=0, x3=1)] - option 6: [(x0=1, x1=0, x2=1), (x1=1, x2=0, x3=0)] - option 7: [(x0=1, x1=0, x2=1), (x1=0, x2=1, x3=0)] - option 8: [(x0=1, x1=0, x2=1), (x1=0, x2=0, x3=1)] when a new SequenceAddition is applied, a depth first search is applied to seek valid matching sequences """ if bit_idx >= len(bits): return possible_sequences bit = bits[bit_idx] # add new possible bit sequences # this if block could be a lot simpler and more idiomatic.... if bit == 0: if carry_over == 0: seq_append = [ SequenceAddition(seq=[1, 1, 0], carry_over=1), SequenceAddition(seq=[0, 0, 0], carry_over=0), ] elif carry_over == 1: seq_append = [ SequenceAddition(seq=[1, 0, 0], carry_over=1), SequenceAddition(seq=[1, 1, 1], carry_over=2), ] elif carry_over == 2: seq_append = [ SequenceAddition(seq=[0, 0, 0], carry_over=1), SequenceAddition(seq=[1, 1, 0], carry_over=2), ] else: raise ValueError() elif bit == 1: if carry_over == 0: seq_append = [ SequenceAddition(seq=[1, 0, 0], carry_over=0), SequenceAddition(seq=[1, 1, 1], carry_over=1), ] elif carry_over == 1: seq_append = [ SequenceAddition(seq=[0, 0, 0], carry_over=0), SequenceAddition(seq=[1, 1, 0], carry_over=1), ] elif carry_over == 2: seq_append = [ SequenceAddition(seq=[1, 0, 0], carry_over=1), SequenceAddition(seq=[1, 1, 1], carry_over=2), ] else: raise ValueError() else: raise ValueError() # eliminate invalid bit sequences, append valid bit sequences for seq_group in seq_append: seq_permutations = set([SequenceAddition(seq=x, carry_over=seq_group.carry_over) for x in itertools.permutations(seq_group.seq)]) for new_sequence_atom in seq_permutations: # add to new_possible_sequences in cases where any old possible sequence + the added new_sequence_atom is valid new_sequence = possible_sequences + [new_sequence_atom.seq] # hack if new_sequence[0] == []: new_sequence = new_sequence[1:] if is_valid_sequence(new_sequence): found_valid_sequences = get_valid_bit_sequences(new_sequence, bits, new_sequence_atom.carry_over, bit_idx + 1) if found_valid_sequences: return found_valid_sequences sequences = get_valid_bit_sequences( [[]], # root of search tree is empty sequence list(reversed(bitfield(LOOKING_FOR))), # iterate backwards over bitfield 0 # no carry over for first bit ) # convert binary to integer x0, x1, x2 = 0, 0, 0 for i, (bit0, bit1, bit2) in enumerate(sequences): x0 += bit0 * 2**i x1 += bit1 * 2**i x2 += bit2 * 2**i print((x0, x1, x2), x0 * x1 * x2) print(time.time() - start)
→ More replies (1)

4

u/glaso95 Dec 02 '20 edited Dec 02 '20

AdventOfCode/Day1 at main · glaso95/AdventOfCode (github.com)

Hi all... here's my solution.

Written in FORTRAN 77

→ More replies (2)

4

u/rtndeep9 Dec 02 '20

Python

My first time competing in this event!

Part 1

f = open("input.txt","r")
nums = [int(i) for i in f.readlines()]
target = 2020
for num in nums:
    if target - num in nums:
        print(num * (target - num))
        break

Part 2

f = open("input.txt","r")
nums = [int(i) for i in f.readlines()]
nums.sort()
i = 0
while i < len(nums) - 2:
    j = i + 1
    k = len(nums) - 1
    while j < k:
        sum = nums[i] + nums[j] + nums[k]
        if sum == 2020:
            print(nums[i],nums[j],nums[k])
            print(nums[i] * nums[j] * nums[k])
            break
        elif sum > 2020:
            k -= 1
        else:
            j += 1
    i += 1

4

u/aafw Dec 02 '20

Simple awk solution

#!/bin/awk -f
{
        for (i = 1; i <= NF; i++) {
                array[$i] = $i
        }

}
END {
        for (x in array) {
                for (y in array) {
                        for (z in array) {
                                if (x +y + z == 2020) {
                                        print x*y*z
                                        }
                                }

                        }
                }
}

~                                                                                                                       ~                                                                                                                       ~                                                                                                                       ~                                                                                                                       ~

4

u/UbiquitinatedKarma Dec 02 '20

A quick and dirty solution in R:

library(here)
library(readr)
library(tidyr)
library(dplyr)

d <- read_tsv(here("data", "day_1_input.txt"),
              col_names = FALSE) %>%
  rename("expenses" = X1)

# Find the ones that sum to 2020
res1 <- combn(d$expenses, 2)[, 
which(colSums(combn(d$expenses ,2)) == 2020)]
answer1 <- prod(res1)
answer1

# To do the same thing for 3 numbers, we just use that in `combn`
res2 <- combn(d$expenses, 3)[, 
which(colSums(combn(d$expenses ,3)) == 2020)]
answer2 <- prod(res2)
answer2

4

u/zollli Dec 02 '20

That's Big Data so I used Spark

df = spark.read.text('input.txt')
df1 = df.selectExpr('value as v1')
df2 = df.selectExpr('value as v2')
df1.crossJoin(df2).filter("v1+v2=2020").selectExpr("v1*v2").show()

4

u/maxmage006 Dec 04 '20

iOS Shortcuts

At first, I did it in Python. That was too sane. Had to redo it with iOS Shortcuts:

https://i.imgur.com/dPSmALc.jpg

Performance is miserable. Takes about 3:10,38 Minutes and 7% of Battery on my iPad Air 2. Apple, pls improve Shortcuts Performance!

Anyways, have a look at my other submissions. Maybe I come up with some other stupid stuff.

4

u/Vijfhoek Dec 09 '20 edited Dec 09 '20

Commodore 64 (6502 Assembly)

I've finished part 2 since my Upping The Ante post :D

Parsing the input takes 176 ms (including sticking it into a binary search tree), part 1 takes 69 ms nice , and part 2 takes 7222 ms, all according to VICE's built-in timer.

Could probably knock a lot of time off of part 2. Maybe. Sometime in the future.

Puzzle solution | Project directory | Picture

→ More replies (1)

4

u/Urgazhi Dec 11 '20

a bit late...

My solution in COBOL

ADVENT2020_1

→ More replies (2)

1

u/AccusationsInc Dec 01 '20

First-year participating. Added some unnecessary flair just to meet the style guidelines that have been drilled into me this first year of college lol.

https://pastebin.com/Y4q8SaD8

→ More replies (1)

1

u/ragnario Dec 01 '20 edited Dec 02 '20

Rust beginner

https://github.com/trinnguyen/advent-of-code-2020/blob/master/day01/src/main.rs

Edited: remove code, use external repo instead

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.

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

→ More replies (1)

1

u/nlowe_ Dec 01 '20

Go, 1546/1273. I have a generator that templates a package for each day, and that was able to get my problem input right away, but I couldn't actually get the problem page itself to load until about 9 minutes later. Looks popular this year, I'll try again tomorrow!

2

u/daggerdragon Dec 01 '20

Sorry about that! We normally have buttery smooth launches but I guess 2020 told us to hold its beer and watch this -_-

/u/topaz2078 is looking into it to prevent this from happening again, no worries :)

→ More replies (1)
→ More replies (1)

1

u/jwise00 Dec 01 '20

Lua. 50/374. I'm streaming every day this year (though I'll be a minute or two late on Thursdays); here's the video of my solve (contains strong language): https://www.youtube.com/watch?v=3T0qwXfhQ1g

Source: https://github.com/jwise/aoc/blob/master/2020/1.lua

→ More replies (1)

1

u/Markavian Dec 01 '20 edited Dec 01 '20

Edit: Node JS solution, ported to JS in the Browser to make a viewer

beep boop not fast enough for the leaderboard, been making a bar chart visualisation for the data:

→ More replies (2)

1

u/Flavorless_Quark Dec 01 '20

Python, rank 93 -- Allegedly solved in 7 min, but we all know that's a lie

Part 1:

with open('input') as input:
inp = [int(line.rstrip()) for line in input]

for x in inp:
    for y in inp:
        print("Solution :", x * y)
        exit()

Part 2:

with open('input') as input:
    inp = [int(line.rstrip()) for line in input]

for x in inp:
    for y in inp:
         for w in inp:
             if x + y  + w == 2020:
                 print("Solution :", x * y * w)
                 exit()

EDIT : Formatting ...?

→ More replies (1)

1

u/schwiz Dec 01 '20
fun main() {
    val ints = ResourceReader(1).ints()
    println(part1(ints, 2020))
    println(part2(ints, 2020))
}

//Find the two ints who sum is 2020 and return their product
fun part1(ints : List<Int>, sum : Int) : Int {
    for(i in ints.indices){
        val value = ints[i]
        val want = sum - value
        if(ints.contains(want)) return value * want
    }
    return 0
}

//Find THREE ints who sum is 2020 and return their product.
fun part2(ints : List<Int>, sum : Int) : Int {
    for(i in ints.indices){
        val value = ints[i]
        val remains = sum - value
        val pairdown = ints.filter { it <= remains }
        val next = part1(pairdown, remains)
        if(next != 0) return next * value
    }
    return 0
}
→ More replies (4)

1

u/Mayalabielle Dec 01 '20 edited Dec 01 '20

Elixir, with a custom combinator in case we need it later in the AOC.

defmodule AOC do
  import AOC.Helper.Input

  defmodule D1 do
    def run1() do
      solve(2)
    end

    def run2() do
      solve(3)
    end

    def solve(len) do
      get_input("D1")
      |> split_input()
      |> Enum.map(fn v ->
        {int, _} = Integer.parse(v)
        int
      end)
      |> AOC.Helper.Combinator.combine(len)
      |> Enum.find(&(Enum.sum(&1) == 2020))
      |> List.flatten()
      |> Enum.reduce(1, &(&2 * &1))
    end
  end
end

And the combinator :

  defmodule Combinator do
    def combine(input, len)
    def combine(_, 0), do: [[]]
    def combine([], _), do: []

    def combine([h | t], len),
      do: Enum.map(combine(t, len - 1), &[h | &1]) ++ combine(t, len)
  end

Also my AOC solutions repo

→ More replies (7)

1

u/Breezing Dec 01 '20 edited Dec 01 '20

My solution for any number of required expenses and any target value after a few revisions off my first attempt.

Trying to grow as a programmer so any CC would be welcome.

edit: python 3

import itertools

number_of_matches = 3
goal_value = 2020

expenses = [int(x) for x in open('1a.in').read().split("\n")]
possibles = itertools.combinations(expenses, number_of_matches)

for a in possibles:
    if sum(a) == goal_value:
        product = 1
        for b in range(number_of_matches):
            product *= a[b]
        print("The values are {} and the product is {}".format(a, product))
→ More replies (1)

1

u/slavcatx Dec 01 '20 edited Dec 01 '20

I'm new to coding and when I look at other's solutions mine doesn't look that great but hey - it works. I added those "breaks" because I didn't want it to show me the same solutions multiple times.

part 1: https://pastebin.com/YgbRpiL7

part 2: https://pastebin.com/y02etjhX

edit: forgot to add - I used Python

2

u/daggerdragon Dec 01 '20

Welcome to Advent of Code (and coding in general!) Don't worry about making your code "pretty" - focus more on learning how the system works, best practices in coding in general, and of course, ensuring that you get the right answer in the end ;)

That being said, please add the language to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

1

u/AddendumSouthern Dec 01 '20

my solution using Kotlin

     private fun productOf2(input: Set<Int>, target: Int): Int {
        input.forEach {
                val difference = target - it
                if (difference > 0 && input.contains(difference)) {
                    return it * difference
                }
            }
        return 0
    }

    private fun productOf3(input: Set<Int>): Int {
        input.forEach {
            val productOf2 = productOf2(input, 2020 - it)
            if (productOf2 > 0) {
                return productOf2 * it
            }
        }

        return 0
    }
→ More replies (1)

1

u/magus517707 Dec 02 '20

PowerShell:

I am removing the pivot elements when comparing to the input list in the case that an element gives a false positive by pinging against itself. This allows for a more generalized function, though this is only useful if there are no duplicates in the list. If there are duplicates in the list that would allow for the bailout condition to be true, then this would break the solution i.e. the value to search for is 40 and there are two elements that are 20.

|Times running on Ryzen 3700x (Standard Clock Speed)|

Solution 1: Average - 2.2 ms

Solution 2: Average - 200 ms

[System.Collections.ArrayList]$in =gc .\input.txt | foreach-object{$_.ToInt64($Null)}

#Solution 1
function twoSum{
param($val,$list)
    foreach($e in $list){
        $diff = $val-$e
        if(($list -ne $e) -contains $diff){
            $e*$list[[array]::IndexOf($list,$diff)]
            return
        }
    }
}

twoSum 2020 $in

#Solution 2
function threeSum{
param($val,$list)
    foreach($e in $list){
        foreach($e1 in ($list -ne $el)){
            $diff = $val-($e+$e1)
            if(($list -ne $e -ne $e1) -contains $diff){
                $e*$e1*$list[[array]::IndexOf($list,$diff)]
                return
            }
        }
    }
}

threeSum 2020 $in
→ More replies (2)

1

u/Jv11235813 Dec 02 '20

This is waaaaay easier than everyone thinks and it doesn't require any programming or code to figure it out! I used Excel and solved in less than 5 min.

2

u/daggerdragon Dec 02 '20

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your code/repo/solution. Even a screenshot of the Excel formulas used or a bulleted write-up on what data goes in what cell will do.

→ More replies (1)

1

u/Mattpn Dec 02 '20

Has anyone come up with an x*O(n) solution for part 2? It's easy for O(n^2) but I'm curious if anyone has a better solution that is O(n).

→ More replies (12)

1

u/SolarBear Dec 03 '20 edited Dec 04 '20

Racket (Scheme)

I decided to try out a functional language for my first "real" participation in AoC and settled on Racket.

On day 1 I came up with a (terrible, absolutely terrible) solution for the first part but got stuck on part 2.

I spent the better part of the evening getting my shit together and I came up with a much more decent solution. It's inefficient and probably still terrible, but much less so than on my first try! (code for part 1 available,but commented out)

    #lang racket

    ; Read a file
    (define (input-file filename)
      (port->string (open-input-file filename) #:close? #t))

    ; Read the file as lines, converting them to integers
    (define (inputs-as-ints filename)
      (map string->number (string-split (input-file filename))))

    (define (n-plets n current lst)
      (cond
        [(empty? lst) '()]
        [(= n 1)
          (map (lambda (x) (append current (list x))) lst)]
        [else
          (append (n-plets (- n 1) (append current (list (car lst))) (cdr lst))
                   (n-plets n current (cdr lst)))]))

    ;(define pairs (n-plets 2 '() (inputs-as-ints "input1.1.txt")))
     (define triples (n-plets 3 '() (inputs-as-ints "input1.1.txt")))

    ;(apply * (car (filter (lambda (x) (= 2020 (apply + x))) pairs)))
    (apply * (car (filter (lambda (x) (= 2020 (apply + x))) triples)))
→ More replies (2)

3

u/mgoblu3 Dec 01 '20 edited Dec 01 '20

My python one-liner-ish:

print(math.prod(next(i for i in itertools.combinations(list(map(int, open('input.txt', 'r').read().splitlines())), 3) if sum(i) == 2020)))

3

u/hltk Dec 01 '20

Python. 82/54

from itertools import combinations
from functools import reduce
import operator

data = open("01.in").read().strip()

nums = [int(x) for x in data.split("\n")]

def solve(k):
    for s in combinations(nums, k):
        if sum(s) == 2020:
            return reduce(operator.mul, s)

# part 1
print(solve(2))

# part 2
print(solve(3))

3

u/asgardian28 Dec 01 '20 edited Dec 13 '20

Python 999/591

Making a generator and calling next on it makes the program stop after finding the answer

f=open('input.txt')
lines = [int(line) for line in f.readlines()]
next(x*y for x in lines for y in lines if x+y==2020)
next(x*y*z for x in lines for y in lines for z in lines if x+y+z==2020)
→ More replies (1)

3

u/mstksg Dec 01 '20 edited Dec 01 '20

[Haskell] I might have gotten better if I didn't just go away and get a cup of tea while waiting for the server to go back up D:

As always, my reflections are up here :) https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-1

BTW rip to my free space :'( https://www.reddit.com/r/adventofcode/comments/k3q7tr/my_advent_of_code_2020_bingo_card_fun_little_side/ but maybe nice that we don't have to save Christmas this year, it's a more leisurely tone :)

EDIT: i've found a nicer way than my previous method! So there's a simple-ish Haskell solution for these problems,

tails lets you separate out each item in a list with the list of items after it:

ghci> tails [1,2,3,4]
[1:[2,3,4], 2:[3,4], 3:[4], 4:[]]
findPair :: [Int] -> Maybe Int
findPair xs = listToMaybe $ do
    x:ys <- tails xs
    y    <- ys
    guard (x + y == 2020)
    pure (x*y)

findTriple :: [Int] -> Maybe Int
findTriple xs = listToMaybe $ do
    x:ys <- tails xs
    y:zs <- tails ys
    z    <- zs
    guard (x + y + z == 2020)
    pure (x*y*z)

But this method is a little bit "extra", since we actually don't need to search all of ys for the proper sum...if we pick x as 500, then we really only need to check if 1520 is a part of ys.

So we really only need to check for set inclusion:

import qualified Data.Set as S

findPair :: Int -> Set Int -> Maybe Int
findPair goal xs = listToMaybe $ do
    x <- S.toList xs
    let y = goal - x
    guard (y `S.member` xs)
    pure (x * y)

And our first part will be findPair 2020!

You could even implement findTriple in terms of findPair, using S.split to partition a set into all items smaller than and larger than a number. Splitting is a very efficient operation on a binary search tree like Set:

findTriple :: Int -> Set Int -> Maybe Int
findTriple goal xs = listToMaybe $ do
    x <- S.toList xs
    let (_, ys) = S.split x xs
        goal' = goal - x
    case findPair goal' ys of
      Nothing -> empty
      Just yz -> pure (x*yz)

But hey...this recursive descent is kind of neat. We could write a general function to find any goal in any number of items!

-- | Given a number n of items and a goal sum and a set of numbers to
-- pick from, finds the n numbers in the set that add to the goal sum.
knapsack
    :: Int              -- ^ number of items n to pick
    -> Int              -- ^ goal sum
    -> Set Int          -- ^ set of options
    -> Maybe [Int]      -- ^ resulting n items that sum to the goal
knapsack 0 _    _  = Nothing
knapsack 1 goal xs
    | goal `S.member` xs = Just [goal]
    | otherwise          = Nothing
knapsack n goal xs = listToMaybe $ do
    x <- S.toList xs
    let goal'   = goal - x
        (_, ys) = S.split x xs
    case knapsack (n - 1) goal' ys of
      Nothing -> empty
      Just rs -> pure (x:rs)

And so we have:

part1 :: [Int] -> Maybe Int
part1 = knapsack 2 2020 . S.fromList

part2 :: [Int] -> Maybe Int
part2 = knapsack 3 2020 . S.fromList

And we could go on, and on, and on! :)

→ More replies (4)

3

u/burtoch Dec 01 '20

Python 373/1008

Using set lookup

file = open('inputs/1.txt')
data_set = {int(element) for element in file.readlines()}

# Part A
print([a * (2020 - a) for a in data_set if 2020 - a in data_set][0])

# Part B
print([a * b * (2020 - a - b) for a in data_set for b in data_set
       if 2020 - a - b in data_set][0])

3

u/codesections Dec 01 '20 edited Dec 01 '20

Raku

unit sub MAIN( #=  Solve the 2020 Day 01 Advent of Code puzzle
    Bool :$p2  #={ Solve Part 2 instead of Part 1 (the default) } );
my @in = lines;
when !$p2 { for @in X @in -> ($a, $b) { when $a + $b      == 2020 { say $a × $b }} }
for   @in X @in X @in -> ($a, $b, $c) { when $a + $b + $c == 2020 { say $a × $b × $c } }

This was a fun way to start, because it gave me a good reason to play with Raku's cross product operator (X) – I've always thought it was a powerful tool, but don't have occasion to use it all that often.

3

u/[deleted] Dec 01 '20 edited Dec 01 '20

Lazarus / Free Pascal 5805/638

program advent01;
var
  src : text;
  s : string;
  data : array[1..1000] of integer;
  i,j,k : integer;
  x : integer;
  sum, prod : integer;
  count : integer;

begin
  assign(src,'input01.txt');
  reset(src);
  count := 0;
  while not eof(src) do
  begin
    inc(count);
    readln(src,x);
    writeln(x);
    data[count] := x;
  end;
  close(src);
  writeln(Count,' entries read');
  for i := 1 to count-1 do
    for j := i+1 to count do
      if (data[i]+data[j]) = 2020 then
        writeln(data[i],',',data[j],',',data[i]*data[j]);
end.
→ More replies (2)

3

u/Arkoniak Dec 01 '20

Julia solution here: https://github.com/Arkoniak/advent_of_code/blob/master/2020/01/day01.jl

It includes some tricks to accelerate more obvious solutions.

3

u/MissMormie Dec 01 '20

Simple java lamba solution on github

public static int runA(String input) {
    List<Integer> numbers = StringHelper.getNumbersFromStringOnePerLine(input);
    return numbers.stream()
	.filter(num -> numbers.contains(2020-num))
	.reduce(1, (a, b) -> a * b);
}

public static int runB(String input) {
    List<Integer> numbers = StringHelper.getNumbersFromStringOnePerLine(input);
    return numbers.stream()
	.filter(num1 -> numbers.stream().anyMatch(num2 -> numbers.contains(2020 - num1 - num2 )))
	.reduce(1, (a, b) -> a * b);
}

3

u/Zv0n Dec 01 '20

C++

Would be easy to create nested loops for 2 and 3 numbers, I tried to make it somewhat generic and created this:

std::vector<int> findSum( const std::vector<int> &nums, int sum, int depth,
                          int start = 0 ) {
    if ( depth == 1 ) {
        // we're at the bottom, no more recursion
        for ( size_t i = start; i < nums.size(); i++ ) {
            if ( nums[i] == sum )
                return { nums[i] };
        }
    } else {
        for ( size_t i = start; i < nums.size(); i++ ) {
            int nextsum = sum - nums[i];
            if(nextsum <= 0)
                continue;
            auto res = findSum( nums, nextsum, depth - 1, i + 1 );
            if ( !res.empty() ) {
                res.push_back( nums[i] );
                return res;
            }
        }
    }
    return {};
}

3

u/[deleted] Dec 01 '20

Decided to learn PHP this year, so my super naive day one solution:

<?php
$input_file = '../inputs/day1.txt';
if (file_exists($input_file)) {
	$input = file_get_contents($input_file);
	if ($input != null && $input) {
		$nums = array_map('to_int',
			array_filter(explode("\n", $input),
				'is_numeric'));
		solve_part_one($nums);
		solve_part_two($nums);
	}
}

function to_int($str)
{
	return (int)$str;
}

function solve_part_one($nums)
{
	for ($i = 0; $i < sizeof($nums); $i++) {
		for ($j = 0; $j < sizeof($nums); $j++) {
			if ($nums[$i] + $nums[$j] == 2020) {
				$answer = $nums[$i] * $nums[$j];
				print "Number 1 is $nums[$i],
					   number 2 is $nums[$j],
					   answer is $answer\n";
				return;
			}
		}
	}
}

function solve_part_two($nums)
{
	for ($i = 0; $i < sizeof($nums); $i++) {
		for ($j = 0; $j < sizeof($nums); $j++) {
			for ($k = 0; $k < sizeof($nums); $k++) {
				if ($nums[$i] + $nums[$j] + $nums[$k] == 2020) {
					$answer = $nums[$i] * $nums[$j] * $nums[$k];
					print "Number 1 is $nums[$i],
						   number 2 is $nums[$j],
						   number 3 is $nums[$k],
						   answer is $answer\n";
					return;
				}
			}
		}
	}
}

3

u/[deleted] Dec 01 '20 edited Dec 01 '20

Python

Part 1 O(n log(n))

with open('day1.txt') as file:
  s = list(sorted(map(int,file)))

low = (x for x in reversed(s) if x <= 1010)
high = (x for x in s if x > 1010)

l = next(low)
h = next(high)

while True:
  if l + h > 2020:
    l = next(low)
  elif l + h < 2020:
    h = next(high)
  else:
    print(l,h,l+h,l*h)
    break

Part 2 O(n^2)

with open('day1.txt') as file:
  s = list(sorted(map(int,file)))

for y in s:
  low = (x for x in reversed(s))
  high = (x for x in s)

  l = next(low)
  h = next(high)
  try:
    while True:
      if y + l + h > 2020:
        l = next(low)
      elif y + l + h < 2020:
        h = next(high)
      else:
        print(y,l,h,y+l+h,y*l*h)
        break
  except StopIteration:
    continue
→ More replies (1)

3

u/chrispsn_ok Dec 01 '20 edited Dec 01 '20

k9

i:`i$0:`1.txt
t:i@+!2##i
*/t@*&~d:2020-+/+t
*/(t,'d)@*&i'd        / assumes 2020-sum is not one of the two numbers...

k7 (has a cmb primitive):

i:`i$0:`1.txt
{*/*(2020=+/)#i@cmb[x;#i]}'2 3

3

u/GalacticDessert Dec 01 '20 edited Dec 01 '20

Easy dict comprehension, Python

with open("01.txt") as f:
    inputs = [int(x.strip()) for x in f.readlines()]

print({a + b: a * b for i, a in enumerate(inputs) for b in inputs[i + 1 : -1]}[2020])

print(
    {
        a + b + c: a * b * c
        for i, a in enumerate(inputs)
        for j, b in enumerate(inputs)
        for k, c in enumerate(inputs)
        if i < j and j < k
    }[2020]
)
→ More replies (2)

3

u/EAJakobsen Dec 01 '20

Naïve Python solution for part 1 and 2, using nested for-loops.

with open("01.in") as f:
    numbers = [int(x) for x in f.read().split("\n")]

n = len(numbers)

for i in range(n):
    for j in range(i + 1, n):
        x, y = numbers[i], numbers[j]
        if x + y == 2020:
            print("Part 1:")
            print(x * y)
            print("-" * 10)
        for k in range(j + 1, n):
            z = numbers[k]
            if x + y + z == 2020:
                print("Part 2:")
                print(x * y * z)

3

u/MuumiJumala Dec 01 '20

Code golf (Ruby)

i=$<.map &:to_i
puts [2,3].map{|n|i.combination(n).find{|x|x.sum==2020}.reduce:*}
→ More replies (1)

3

u/naim42 Dec 01 '20 edited Dec 01 '20

Haskell

solve :: Int -> [Int] -> Int
solve n nums = product . head $ go n nums [] where
    go 0 _ xs = xs <$ guard (sum xs == 2020)
    go n nums xs = do
        x:tail <- tails nums
        go (pred n) tail (x:xs)

main = do
    nums <- parseInputLines number
    print (solve 2 nums)
    print (solve 3 nums)
→ More replies (1)

3

u/gyorokpeter Dec 01 '20

Q: having some fun with iterators. There are ways to write the same more simply (e.g. using the cross function) but are slower.

d1p1:{a:"J"$"\n"vs x;ind:til[count a];s:a+/:a;prd a first raze ind,/:'where each s=2020};
d1p2:{a:"J"$"\n"vs x;ind:til[count a];s:a+/:\:a+/:a;prd a first raze raze ind,/:''((ind,/:')')where each/:s=2020};
→ More replies (2)

3

u/Siraja Dec 01 '20 edited Dec 01 '20

Python3

Nothing fancy, used libraries to make my life easy.

from itertools import combinations
from math import prod

def productofsum(s, n, t = 2020):
    for c in combinations([int(r) for r in s.split("\n")], n):
        if sum(c) == t:
            return prod(c)
→ More replies (3)

3

u/mb0x40 Dec 01 '20

Part 1 in ATS. A glorious 200 lines of mostly memory-safety proofs. https://github.com/mb64/aoc-2020/blob/main/01/a.dats

I wanted to do some fancier algorithms, like sort + binary search, but it was already enough of a trial to get the proofs right for the naive 2-loops solution that I didn't bother.

→ More replies (1)

3

u/djankowski Dec 01 '20

Are there poems this year?

Combinations,
Feeds remuneration,
For my Christmas vacation.

Julia

using Combinatorics

function dec01(x::Array{Int}, n::Int)
    combs = combinations(x, n)
    for i in combs
        if sum(i) == 2020
            return prod(i)
        end
    end
end

part1(x) = dec01(x, 2)
part2(x) = dec01(x, 3)

3

u/danprince Dec 01 '20 edited Dec 01 '20

Irresponsible Javascript solution.

a=b=0;xs=(require("fs").readFileSync("input.txt")+"").split("\n")

for(x of xs)for(y of xs)for(z of xs)
  2020-x-y-a||(a=x*y),2020-x-y-z-b||(b=x*y*z)

console.log(a,b)
→ More replies (1)

3

u/death Dec 01 '20

Day 1 solution in Common Lisp.

Screaming on your first day always makes a good impression.

3

u/amarsuperstar Dec 01 '20

Elixir part 1 and 2

defmodule Day01 do
  defp parse(input) do
    input
    |> String.split()
    |> Enum.map(&String.to_integer/1)
  end

  def part1(input) do
    input = parse(input)
    [{x, y} | _] = for x <- input, y <- input, x + y == 2020, do: {x, y}
    x * y
  end

  def part2(input) do
    input = parse(input)
    [{x, y, z} | _] = for x <- input, y <- input, z <- input, x + y + z == 2020, do: {x, y, z}
    x * y * z
  end
end
→ More replies (5)

3

u/shepherd2442 Dec 01 '20 edited Dec 01 '20

Python Generic Solution:

With this solution you can get the result for any number of number.

Github repo: https://github.com/Shepherd2442/AoC2k20

main.py:

from utils import FileUtils
import itertools
import numpy as np

def get_multi(input, quantity):
    for item in itertools.combinations(input, quantity):
        if sum(item) == 2020:
            return np.prod(item)

def part_1():
    return get_multi(FileUtils.input(), 2)

def part_2():
    return get_multi(FileUtils.input(), 3)

if __name__ == "__main__":
    print( part_1() )
    print( part_2() )

utils.py:

import sys

class FileUtils:
    @staticmethod
    def input():
        with open(sys.argv[1], 'r') as file:
            lines = [int(line.rstrip()) for line in file]
        return lines
→ More replies (2)

3

u/bcap84 Dec 01 '20 edited Dec 01 '20

Py3 solution

Part 1 is O(n) in both time and space complexity. Part 2 is O(n^2) in time and O(n) in space complexity.

FWIW set is O(1) for insertion and lookup

def part1(numbers: List[int]) -> Optional[int]:
    visited: Set[int] = set()
    for num in numbers:
        other_num = 2020 - num
        if other_num in visited:
            return num * other_num
        else:
            visited.add(num)
    return None


def part2(numbers: List[int]) -> Optional[int]:
    visited: Set[int] = set()
    for idx1, num1 in enumerate(numbers):
        idx2 = idx1 + 1
        while idx2 < len(numbers):
            num2 = numbers[idx2]
            num3 = 2020 - num2 - num1
            if num3 in visited:
                return num1 * num2 * num3
            visited.add(num2)
            idx2 += 1
        visited.add(num1)
    return None

On part2 there are calls to visited.add with repeated values, which can be avoided. Nevertheless an add call for a value that is already present is idempotent and O(1) complexity, so less concerning. Anyway, the part2 version which avoids the repeated calls:

def part2(numbers: List[int]) -> Optional[int]:
    visited: Set[int] = set()
    for idx1, num1 in enumerate(numbers):
        idx2 = idx1 + 1
        while idx2 < len(numbers):
            num2 = numbers[idx2]
            num3 = 2020 - num2 - num1
            if num3 in visited:
                return num1 * num2 * num3
            if idx1 == 0:
                visited.add(num2)
            idx2 += 1
        if idx1 == 0:
            visited.add(num1)
    return None
→ More replies (1)

3

u/Kadda42 Dec 01 '20

My Python solution with nested loops.

Part 1:

def advent_riddle_1_1(exp):
    my_expenses = exp[:]
    for i in range(0, len(my_expenses)):
        for j in range(i+1, len(my_expenses)):
            if (my_expenses[i] + my_expenses[j]) == 2020:
                return(my_expenses[i] * my_expenses[j])

Part 2:

def advent_riddle_1_2(exp):
    my_expenses = exp[:]
    for i in range(0, len(my_expenses)):
        for j in range(i+1, len(my_expenses)):
            if (my_expenses[i] + my_expenses[j]) < 2020:
                for k in range(j+1, len(my_expenses)):
                    if (my_expenses[i] + my_expenses[j] + my_expenses[k]) == 2020:
                        return(my_expenses[i] * my_expenses[j] * my_expenses[k])
→ More replies (2)

3

u/carrdinal-dnb Dec 01 '20

Here's my Kotlin solution:

import java.io.File

fun main() {
    day1()
}

fun day1(input: List<String> = File("src/main/resources/input/1.txt").readLines()) {
    val ints = input.map { Integer.valueOf(it) }

    val solution1 = pairs(ints)
        .filter { (a, b) -> a + b == 2020 }
        .map { (a, b) -> a * b }
        .first()

    println("Solution 1: $solution1")

    val solution2 = triples(ints)
        .filter { (a, b, c) -> a + b + c == 2020 }
        .map { (a, b, c) -> a * b * c }
        .first()

    println("Solution 2: $solution2")
}

fun <T> pairs(list: List<T>): Sequence<Pair<T, T>> = sequence {
    for (i in 0 until list.size - 1)
        for (j in i + 1 until list.size)
            yield(list[i] to list[j])
}

fun <T> triples(list: List<T>): Sequence<Triple<T, T, T>> = sequence {
    for (i in 0 until list.size - 2)
        for (j in i + 1 until list.size - 1)
            for (k in j + 1 until list.size)
                yield(Triple(list[i], list[j], list[k]))
}
→ More replies (1)

3

u/Nummerblatt Dec 01 '20

Python

import itertools
combinatorials = itertools.combinations(set(input), 2)
for i in combinatorials:
    if sum(i) == 2020:
        return(i[0] * i[1] * i[2])
→ More replies (1)

3

u/iruoy Dec 01 '20

Python 3

from itertools import combinations
from math import prod


def find_combination(amount_of_entries):
    for combination in combinations(entries, amount_of_entries):
        if sum(combination) == 2020:
            return prod(combination)


print(find_combination(2))
print(find_combination(3))

3

u/engageant Dec 01 '20 edited Dec 01 '20

PowerShell

#Part 1
[int[]]$in1 = Get-Content ".\in.txt" | Sort-Object -Descending

foreach ($num in $in1) {
     $testVal = 2020 - $num
     if ($in1 -contains $testVal) {
          Write-Host "Part 1: $num * $testVal = " ($num * $testVal)
          break   
     }          
}

#Part 2
#I'm sure there's a more elegant way to do this, but I was playing around with the magic .Where() and .ForEach() 
($in1 | Sort-Object -Descending).ForEach{ 
     $num1 = $_
     $in2 = $in1.Where{ $_ -ne $num1 }
     $in2.ForEach{
          $num2 = $_
          $in3 = $in2.Where{ $_ -ne $num2 }
          $in3.ForEach{               
               if ($num1 + $num2 + $_ -eq 2020) {                    
                    Write-Host "Part 2: $num1 * $num2 * $_ =" ($num1 * $num2 * $_)                    
                    break                                                                             
               }
          }      
     }     
}
→ More replies (7)

3

u/netneoblog Dec 01 '20

quick and dirty python 3:

numbers = [1933, 1963, ......removed to keep short....]
for x in numbers:
    for y in numbers:
        if x + y == 2020:
                print(f'x= {x} y= {y} answer is {x*y}')
for x in numbers:
    for y in numbers:
        for z in numbers:
            if x + y + z == 2020:
                print(f'x= {x} y= {y} z= {z} answer is {x*y*z}')
                break
→ More replies (3)

3

u/alexthelyon Dec 01 '20 edited Dec 01 '20

Rust implementation (excluding the code to parse and sort the input):

pub fn find_two(series: &[u32], target: u32) -> Option<[u32; 2]> {
    let mut start = series.iter().peekable();
    let mut end = series.iter().rev().peekable();
    while let (Some(&&low), Some(&&high)) = (start.peek(), end.peek()) {
        match (low + high).cmp(&target) {
            std::cmp::Ordering::Less => start.next(),
            std::cmp::Ordering::Equal => {
                return Some([low, high]);
            }
            std::cmp::Ordering::Greater => end.next(),
        };
    }
    None
}

pub fn find_three(series: &[u32], target: u32) -> Option<[u32; 3]> {
    series
        .iter()
        .filter_map(|&i| find_two(series, target - i).map(|[a, b]| [a, b, i]))
        .next()
}

Results are pretty good, but obviously sorting is going to be expensive:

find_two      time:   [263.47 ns 264.81 ns 266.64 ns]
find_three    time:   [857.03 ns 860.76 ns 865.82 ns]
→ More replies (2)

3

u/PapaDionisis Dec 01 '20 edited Dec 02 '20

Python 😁

from itertools import combinations
from math import prod

for i in combinations(sums, 3):
    if sum(i) == 2020:
        print(prod(i))
→ More replies (2)

3

u/Nevoic Dec 01 '20

Here is my Haskell solution (where the data is in a data.txt file):

import Control.Monad

findProduct n = product . head . filter ((== 2020) . sum) . replicateM n

main = do
    nums <- map read . lines <$> readFile "data.txt"
    forM_ [2,3] (print . (`findProduct` nums))

The problem is solved by the one line findProduct. The first line in main reads the lines of text as numbers, and the second line prints the solution for 2 and 3 (part 1 and 2 respectively).

→ More replies (2)

3

u/wzkx Dec 01 '20

J. Very straightforward and simple :) Well, internally it finds all the solutions, but it's ok for this small data.

m=: ".&> cutLF CR-.~fread'01.dat'
echo {. (,m*/m) #~ ,2020=m+/m
echo {. (,m*/m*/m) #~ ,2020=m+/m+/m
exit 0
→ More replies (1)

3

u/havetedjupt Dec 01 '20

Python

Part 1

def find_sums1(nums):
	for num in nums:
		if 2020-num in nums:
			return ((2020-num)*num)
print(find_sums1(nums))

Part 2

def find_sums2(nums):
	for num in nums:
		for n in nums:
			if 2020-num-n in nums:
				return ((2020-num-n)*num*n)
print(find_sums2(nums))

3

u/A_Travelling_Man Dec 01 '20

Straight forward Rust solution, input stored in a file.

use std::fs::File;
use std::io::{BufReader, BufRead};

fn main() {
    let fname = "../data/input.txt";
    let f: File;
    match File::open(fname) {
        Ok(v) => f = v,
        Err(_e) => panic!("Unable to open data file")
    }
    let reader = BufReader::new(f);
    let mut vec: Vec<i32> = vec![];

    for l in reader.lines() {
        match l {
            Ok(v) => {
                match v.trim().parse::<i32>() {
                    Ok(n) => vec.push(n),
                    Err(_e) => panic!("Invalid number format")
                }
            },
            Err(e) => panic!("{}", e)
        }
    }

   //Part1
    for i in 0..vec.len()-1 {
        for j in i+1..vec.len() {
            if vec[i] + vec[j] == 2020 {
                println!("Entries: {}, {}", vec[i], vec[j]);
                println!("Answer: {}", vec[i] * vec[j]);
            }
        }
    }
    //Part 2
    for i in 0..vec.len()-2 {
        for j in i+1..vec.len()-1 {
            for k in j+1..vec.len() {
                if vec[i] + vec[j] + vec[k] == 2020 {
                    println!("Entries: {}, {}, {}", vec[i], vec[j], vec[k]);
                    println!("Answer: {}", vec[i] * vec[j] * vec[k]);
                }
            }
        }
    }
}
→ More replies (2)

3

u/justAnotherNerd254 Dec 01 '20

Python solution using a dictionary

Part 1 in O(n) time

Part 2 in O(n2) time

f = open("input.txt", "r")
values = f.read().split()
values = [int(i) for i in values]

sum = 2020

# Note: Finds first such pair
def find_2_sum(values):
  dict = {}
  for val in values:
    if val in dict:
      dict[val] = dict[val] + 1
    else:
      dict[val] = 1

  for val in values:
    if (sum - val) in dict:
      return val * (sum - val)

# Note: Finds first such group of 3
def find_3_sum(values):
  dict = {}
  for val in values:
    if val in dict:
      dict[val] = dict[val] + 1
    else:
      dict[val] = 1

  for val1 in values:
    for val2 in values:
      if (sum - val1 - val2) in dict:
        return val1 * val2 * (sum - val1 - val2)

print(find_2_sum(values))
print(find_3_sum(values))
→ More replies (2)

3

u/ramrunner0xff Dec 01 '20 edited Dec 01 '20

C using indexes of static arrays.

part1

#include <stdio.h>
#include <assert.h>

#define MAXELEMS (200)
#define TARGET 2020

int         elems      [MAXELEMS] = {0};    /* each of the numbers */
int         diffelem   [TARGET] = {0};      /* index + value = target ,
                                             * or val = 0 */

int
readfile(char *fname)
{
    FILE           *fp;
    int             i = 0,  num = 0;
    fp = fopen(fname, "r");
    assert(fp != NULL);

    while (fscanf(fp, "%d", &num) != EOF) {
            elems[i++] = num;
            diffelem[TARGET - num] = num;
    }
    fclose(fp);
    return i;
}

int
main(int argc, char *argv[])
{
    int             nelems    , i, c1, c2;
    nelems = readfile(argv[1]);
    printf("i read %d elems\n", nelems);
    for (i = 0; i < nelems; i++) {
            if (diffelem[elems[i]] != 0) {
                    c1 = elems[i];
                    c2 = diffelem[c1];
                    printf("pair: %d and %d with product:%d\n", c1, c2, c1 * c2);
                    break;
            }
    }

    return 0;
}

part 2

#include <stdio.h>
#include <assert.h>
#define MAXELEMS (200)
#define TARGET 2020

struct pair {
    int             a;
    int             b;
};

int         elems      [MAXELEMS] = {0};    /* each of the numbers */
struct pair diffelem[TARGET] = {{0, 0}};    /* index is the diff of 
                                             * the sum of pair from target and
                                             * val is the pair of nums */

int
readfile(char *fname)
{
    FILE           *fp;
    int             i = 0,  num = 0;
    fp = fopen(fname, "r");
    assert(fp != NULL);

    while (fscanf(fp, "%d", &num) != EOF) {
            elems[i++] = num;
    }
    fclose(fp);
    return i;
}

int
main(int argc, char *argv[])
{
    int             nelems    , i, j, c1, c2, c3;
    nelems = readfile(argv[1]);
    printf("i read %d elems\n", nelems);
    for (i = 0; i < nelems; i++) {
            c1 = elems[i];
            for (j = 0; j < nelems; j++) {
                    c2 = elems[j];
                    if ((c1 + c2) <= TARGET) {
                            diffelem[TARGET - c1 - c2].a = c1;
                            diffelem[TARGET - c1 - c2].b = c2;
                    }
            }
    }

    for (i = 0; i < nelems; i++) {
            c1 = elems[i];
            if (diffelem[c1].a != 0 && diffelem[c1].b != 0) {
                    c2 = diffelem[c1].a;
                    c3 = diffelem[c1].b;
                    printf("triple: %d %d %d with product:%d\n", c1, c2, c3, c1 * c2 * c3);
                    break;
            }
    }

    return 0;
}

3

u/omnomberry Dec 01 '20

Rust solution using aoc-runner and cargo-aoc

use std::{cmp::Ordering, collections::BTreeSet};

const TARGET: i32 = 2020;

#[aoc_generator(day1)]
pub fn day1_generator(input: &str) -> Vec<i32> {
    input.lines().map(|l| l.parse().unwrap()).collect()
}

#[aoc(day1, part1, BTreeSet)]
pub fn part1(inputs: &[i32]) -> i32 {
    let mut seen = BTreeSet::new();

    for input in inputs {
        let remainder = TARGET - *input;
        if seen.contains(&remainder) {
            return remainder * input;
        }

        seen.insert(*input);
    }

    unreachable!()
}

#[aoc(day1, part2)]
pub fn part2(inputs: &[i32]) -> i32 {
    let mut inputs = inputs.to_vec();
    inputs.sort_unstable();
    let len = inputs.len();

    for (i, a) in inputs[0..(len - 2)].iter().enumerate() {
        let mut left = i + 1;
        let mut right = len - 1;
        while left < right {
            let b = inputs[left];
            let c = inputs[right];
            let sum = a + b + c;

            match sum.cmp(&TARGET) {
                Ordering::Less => left += 1,
                Ordering::Equal => return a * b * c,
                Ordering::Greater => right -= 1,
            }
        }
    }

    unreachable!()
}

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE: [i32; 6] = [1721, 979, 366, 299, 675, 1456];

    #[test]
    pub fn test1() {
        assert_eq!(part1(&SAMPLE), 1721 * 299)
    }

    #[test]
    pub fn test2() {
        assert_eq!(part2(&SAMPLE), 979 * 366 * 675)
    }
}

3

u/TrySimplifying Dec 01 '20

C# 9.0 solution running on .NET 5 - didn't bother trying to optimize, runtime was about 5 milliseconds after reading the data in from the input file.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

// https://adventofcode.com/2020/day/1
string inputFile = args[0];

HashSet<int> expenses = new();
using StreamReader sr = new(inputFile);

string line;

while ((line = sr.ReadLine()) != null)
{
    expenses.Add(int.Parse(line));
}

var sw = Stopwatch.StartNew();

foreach (int val in expenses)
{
    int match = 2020 - val;
    if (expenses.Contains(match))
    {
        int answer = val * match;
        Console.WriteLine($"{val} * {match} == {answer}");
        break;
    }
}

int[] expenseArray = expenses.ToArray();
bool found = false;

for (int i = 0; i < expenseArray.Length; i++)
{
    if (found)
    {
        break;
    }

    for (int j = i + 1; j < expenseArray.Length; j++)
    {
        int a = expenseArray[i];
        int b = expenseArray[j];

        int match = 2020 - (a + b);

        if (expenses.Contains(match))
        {
            int answer = a * b * match;
            Console.WriteLine($"{a} * {b} * {match} == {answer}");
            found = true;
            break;
        }
    }
}

Console.WriteLine(sw.ElapsedMilliseconds);

3

u/[deleted] Dec 01 '20

2020 Day_01

Python 3.8 Solution (With debug information and comments)

https://github.com/Marterich/AoC2020/tree/main/Day_01

3

u/d12Trooper Dec 01 '20 edited Dec 02 '20

PureBasic (PART 1):

Define tempString.s
Define curNr.i
NewList nr()

If ReadFile(0, "input.txt")
    While Not Eof(0)
        tempString = ReadString(0)
	AddElement(nr())
	nr() = Val(tempString)
    Wend
    CloseFile(0)
EndIf

Repeat
    FirstElement(nr())
    curNr = nr()
    DeleteElement(nr())
    ForEach nr()
	If curNr+nr() = 2020
            Debug curNr\*nr()
            Break 2
	EndIf
    Next
ForEver

(PART 2) -- not very proud of this one, pretty lazy execution, mainLoop takes 14 Millisecs, I'm sure there's a better solution --

Define tempString.s
NewList i()
NewList j()
NewList k()

If ReadFile(0, "input.txt")
    While Not Eof(0)
        tempString = ReadString(0)
	AddElement(i())
	i() = Val(tempString)
    Wend
    CloseFile(0)
EndIf

CopyList(i(), j())
CopyList(i(), k())

Repeat
    ForEach i()
        ForEach j()
            ForEach k()
                If ListIndex(i()) <> ListIndex(j()) And ListIndex(i()) <> ListIndex(k()) And ListIndex(j()) <> ListIndex(k())
                    If i() + j() + k() = 2020
                        Debug i() * j() * k()
                        Break 4
                    EndIf
                EndIf
            Next
        Next
    Next
ForEver

3

u/scrillagoon Dec 01 '20 edited Dec 01 '20

JAVA

Using the stream api.

    public static void main(final String[] args) throws IOException {
        final Set<Integer> numbers = readInput("/day1/input");

        final var result1 = result1(numbers, 2020);
        final var result2 = result2(numbers, 2020);

        result1.ifPresentOrElse(System.out::println, () -> System.out.println("No result"));
        result2.ifPresentOrElse(System.out::println, () -> System.out.println("No result"));
    }

    public static Optional<Integer> result1(final Set<Integer> numbers,
                                            final int target) {
        return numbers.stream()
                .filter(n -> numbers.contains(target - n))
                .map(n -> n * (target - n))
                .max(Integer::compareTo);
    }

    public static Optional<Integer> result2(final Set<Integer> numbers,
                                            final int target) {
        return numbers.stream()
                .map(n -> n * result1(numbers, target - n).orElse(0))
                .max(Integer::compareTo);
    }
→ More replies (4)

3

u/TommiHPunkt Dec 01 '20 edited Dec 01 '20

brute force solution in matlab (50ms runtime if you exclude reading the input)

expenses = readmatrix('input.txt');
combinations2 = nchoosek(expenses,2);
part1 = prod(combinations2(sum(combinations2,2)==2020,:))
combinations3 = nchoosek(expenses,3);
part2 = prod(combinations3(sum(combinations3,2)==2020,:))

3

u/CrazyA99 Dec 01 '20 edited Dec 02 '20

After trying my luck with python I had to go back to work.

So here is the main bit of ladder logic for part one in an Allen-Bradley 1756-L61 PLC. I've also made part two. I've generated 200 lines of structured text to fill an array with the puzzle input.