Advent of Code - adventofcode.com

Posted on
Page
of 21
Prev
/ 21
Last Next
  • Messing around with indexes always trips me up, but I've got them both.

  • Was a quick one today.

    https://gist.github.com/dmckennell/bcb6db60d0150bfc877b21492d4f0f78

    ...prints:

    Solving puzzle for Day 2 (Year 2020)
    Loading input from 2020/Day02.txt
    Part 1 took 0.030277581 seconds
    Part 2 took 0.021179534 seconds
    Part 1: 528
    Part 2: 497
    
  • Doing this for the first time this year - in js / node

  • What are you doing for importing the data? I really don't want to go through and edit 1000 lines of strings into an array of objects.

  • using fs.readFile and a separate textfile where I copied the input.
    Did debate trying to fetch it directly from the server but then realised everyones input is different so I would need to deal with auth and I cba.

  • Yeah I've given up trying to be clever and just used find and replace to turn it into an array of strings

  • 410 passwords out of 1000 were valid for part 1
    694 passwords out of 1000 were valid for part 2
    0.18user 0.05system 0:00.11elapsed 212%CPU (0avgtext+0avgdata 39420maxresident)k
    0inputs+64outputs (0major+11049minor)pagefaults 0swap

    Code incredibly inelegant, and no tests. MUST.DO.BETTER :-)

  • 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

  • Google Sheets Day 1:
    https://docs.google.com/spreadsheets/d/1joA1oMsoGaDCwX0u8CAK7IUuMeFqyfLEYzzIWzGB1d0/edit#gid=0

    Google Sheets Day 2:
    https://docs.google.com/spreadsheets/d/17LXRC-kEewCl3K1mvA1dzWJE8geZx9bK78o2fClZc0g/edit?ouid=113006542528071116740&usp=sheets_home&ths=true

    There's probably a neater way to count characters in a string than deleting them and comparing the length =len(B2)-len(SUBSTITUTE(B2,C2,"")) but it's not obvious what.

  • My java did that too.
    I toyed with using a regex, but didn't

  • Done both, I got hung on the first one today a bit due to stupid copy and paste errors. Can share vanilla JS answers if anyone needs help

  • Every day's a school day - learned about arrays in bash.

    #!/bin/bash
    start=$SECONDS
    inputfile=$1
    mapfile -t < $inputfile
    
    # Part 1 - 2 numbers
    for i in "${MAPFILE[@]}"; do
        mycount=$(( mycount + 1 ))
        if [[ " ${MAPFILE[@]} " =~ " $(( 2020 - $i )) " ]] ; then
            echo "$i, $(( 2020 -$i)), $(( $i * (2020 - $i)))"
            duration=$(( SECONDS - start ))
            echo "$duration seconds $mycount records"
            break
        fi
    done
    
    
    # Part 2 - 3 numbers
    mycount=0
    start=$seconds
    for i in "${MAPFILE[@]}"; do
        for j in "${MAPFILE[@]}"; do
            mycount=$(( mycount + 1 ))
            if [[ " ${MAPFILE[@]} " =~ " $(( 2020 - $i -$j )) " ]] ; then
                echo "$i, $j, $(( 2020 -$i - $j )), $(( $i * $j * (2020 - $i - $j) ))"
                duration=$(( SECONDS - start ))
                echo "$duration seconds $mycount records"
                exit
            fi
        done
    done
    
     $ ./array_test.sh input1.txt 
    1632, 388, 633216
    0 seconds 82 records
    1607, 196, 217, 68348924
    3 seconds 14573 records
    
  • Anyone else doing this is Haskell? I'm pretty new to it and had to fight my way through the first two days!

  • Another delay to submitting part 2 because I SIMPLY DID NOT READ THE WORDING.

    When will I learn.

  • Not this year (as I want to focus on just getting to the end of the advent this time), but I'd like to see your solutions as you go as I'm curious!

    Have seen a few GitHub projects with some Haskell advent GitHub projects with file reading scaffolding in place, so will likely clone one of those before next year's challenge.

  • The best prep is going back and completing previous years, gives you a good idea of what scaffolding may be useful.

    Reading in maps into arrays (like today) is a common theme.

    (I don't bother with any pre-written code or scaffolding but I will go and copy/paste from previous submissions if I realise there's a useful chunk of time that could be saved.)

    If this year is like any of the previous ones they'll be some form of a pseudo computer that you need to implement that will come back again and again (and slowly be built up), so there will be some reuse built in.

  • First three days in Python

    https://gist.github.com/wence-/7d2a722fba29c94149a75beebfd5333c

    Will try and update as I go. Thought about using the opportunity to teach myself Rust, but that will wait.

  • I'm usually put off by these "visual" challenges but stuck with it this time. Made a few mistakes with the indexing, offsets and getting my rows and columns mixed up but got there in the end.

    Python: https://gitlab.com/-/snippets/2046058

  • TIL about enumerate!

  • 404 on link (private visibility?)

  • I have an idea how to do this in JS but it's not going to be pretty.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Advent of Code - adventofcode.com

Posted by Avatar for Drakien @Drakien

Actions