You are reading a single comment by @rhowe and its replies. Click here to read the full conversation.
  • 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
    
About

Avatar for rhowe @rhowe started