-
Yep, that makes a lot of sense. Found the same thing in a different language looking at the Reddit solution thread so at least I did the Python implementation myself. Will no doubt be tough going, but that's what I'm doing it for.
expense_list = [int(line) for line in open('day1_input.txt', 'r')] solved1 = False solved2 = False for e in expense_list: lookup1 = 2020 - e if lookup1 in expense_list: print(e * lookup1) solved1 = True for f in expense_list: lookup2 = lookup1 - f if lookup2 in expense_list: print(e * f * lookup2) solved2 = True if solved1 & solved2: finishtime = datetime.now() print(finishtime) exit()
I suppose this is also the moment to familiarize myself with Git so I'm no longer making a mess with codeblocks in this thread.
-
And now it's my python version too.
Congratulations - you have now become my python tutor!
I did this in bash - because box of hammers, and I didn't want to suffer the ignominy of doing it in excel or vba.
#!/bin/bash # Advent of Code - Day 1, Puzzle 1 inputfile=$1 # numbercount=$(cat $1 | wc -l) # Edit because numbers numbercount=$(( $(cat $1 | wc -l) + 1 )) start=$SECONDS for ((i = 1 ; i < $numbercount ; i++)); do for ((j = $i + 1 ; j < $numbercount ; j++)); do ivalue=$(awk "NR == $i" $inputfile) jvalue=$(awk "NR == $j" $inputfile) if [[ $(( $ivalue + $jvalue )) -eq 2020 ]] ; then echo "$ivalue + $jvalue = $(( $ivalue + $jvalue )), $ivalue * $jvalue = $(( $ivalue * $jvalue ))" fi done done duration=$(( SECONDS - start )) echo $duration
:: tiswas@laptop2:/home/tiswas/Temp :: $ ./sum_to_2020.sh input1.txt 1632 + 388 = 2020, 1632 * 388 = 633216 76 seconds 19900 records
Seeing working python code for this is useful though, as I'm learning that from scratch at the moment.
[Edit] because missing a number in the loop
FWIW here's my python version:-
https://gist.github.com/alexgreenbank/55daaa31c84974e1971af2e40e207784