Results 1 to 5 of 5

Thread: Advent of Code, 2017

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Lightbulb Advent of Code, 2017

    Hello,

    Advent of Code is a much healthier alternative to advent calendar.

    0% chocolate, 100% code.

    You are offered new code challenge each day, till Xmas.
    This is an excellent opportunity to get better in algorithmic thinking as well as in mastering thinBASIC!

    For me, it is also an opportunity to compare thinBASIC to other languages to identify possible weak points we can work on in future.

    It is fun and you will reach Xmas not few kilos heavier (as advent calendar can do), but few IQ points smarter instead. Let's play with it

    Links
    Official Advent of Code challenge page: http://adventofcode.com/2017

    My GitHub (in progress)
    - with thinBASIC solutions:
    https://github.com/petrSchreiber/adv...2017-thinbasic
    - with Python 3 solutions: https://github.com/petrSchreiber/adv...e-2017-python3 (to see what other interpreters offer... and we can steal )


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    Always great ideas from Petr.

    I work for a company that makes 50% of global annual income in 4 months from Sept to Dec so in this period my time is very limited.
    But I will follow with great interest getting ideas to implement thinBasic and if I will have some time I will do my versions of the scripts.

    Ciao
    Eros

    PS: First of all I hate to see py version of script 01 is much shorter than thninBasic one.

    Last edited by ErosOlmi; 12-12-2017 at 11:58.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    In Day 01 what about:
      Dim lInput(len(input)) as string * 1 at strptr(input)
    
    This remove the need of all MID$ functions and also takes advance of the fact that thinBasic automatically convert strings to numbers if destination is a number.

    Using "Load_File" included in Core module instead of "File_Load" included in File module. Both functions are the same so File module is not needed.

    The example could be something like:
    Uses "console"
    
    string input = RTrim$(Load_File("data.txt"), $LF)
    
    printl GetCaptcha(input, 1)
    printl GetCaptcha(input, len(input)/2)
    waitkey
    
    function GetCaptcha(input as string, lookupOffset as long)
      long indexA, indexB, result
    
      Dim lInput(len(input)) as string * 1 at strptr(input)
      
      for indexA = 1 to len(input)
    
        indexB = indexA + lookupOffset
        while indexB > len(input)
          indexB = indexB - len(input)
        wend
    
        if lInput(indexA) = lInput(indexB) then result += lInput(indexA)
    
      next
    
      return result
    
    end function
    
    I didn't get the WHILE/WEND usage. Maybe because string is "circular"?
    Last edited by ErosOlmi; 12-12-2017 at 16:57.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Dear Eros,

    thanks a lot for the deep analysis, very much appreciated

    I did use DIM..AT magic in the 01_forAdvanced.tbasic, yet I used overlay of BYTE array instead of STRING * 1.
    Now, as you reminded me addition of number-like string to numeric value is a correct operation, I like your version better. It reduces the need to subtract 48

    As for magical WHILE, I thought Cycle_Next will help me, but once I go around the upper limit with step bigger than one, it always returns 1, which is a party breaker.
    Yet it inspired me to express it better as:
    if indexB > len(input) then 
      indexB -= len(input) * (indexB \ len(input))
    end if
    
    For those interested, Eros commented this version:
    https://github.com/petrSchreiber/adv...a1b4f6b8fd1386

    I mentioned advanced version here:
    https://github.com/petrSchreiber/adv...7248f44f3ff985

    And the current advanced version, after suggestions of Eros looks like:
    https://github.com/petrSchreiber/adv...dvanced.tbasic

    I am looking forward to the review of 02, once you have a time


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    My version for Day 02: calculate checksum of a data file containing lines of tab delimited integer numbers.
    Checksum is the sum of the difference between the max and min value of each data row.

    I used the "matrix" version of PARSE to load file into a matrix instead of an array of strings to be parsed later.

    uses "console"
    
    #MinVersion 1.10.4
    
    '---Parse a Matrix from a file
    string gMatrix()
    parse(file "data.txt", gMatrix, $lf, $tab)
    
    '---Calc and print CheckSum
    printl CheckSum(gMatrix)
    
    waitkey
    
    function CheckSum(lMatrix() as String) as long
      long row
      long col
      long lMin, lMax
      long total
      
      For row = 1 to UBound(lMatrix, 1)
        lMin = +2147483647  '---Max possible long, see help "Numeric Variables"
        lMax = -lMin        '---Min possible long, see help "Numeric Variables"
        For col = 1 to UBound(lMatrix, 2)
          lMin = Min(lMin, lMatrix(row, col))
          lMax = Max(lMax, lMatrix(row, col))
        Next
        total += lMax - lMin
      Next
      
      function = total
    
    
    end function
    
    Last edited by ErosOlmi; 13-12-2017 at 09:01.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. Petr out 22.3.2017 - 31.3.2017
    By Petr Schreiber in forum TBGL module by Petr Schreiber
    Replies: 9
    Last Post: 10-04-2017, 11:39
  2. Pf 2017
    By Petr Schreiber in forum TBGL Scripts and Projects
    Replies: 2
    Last Post: 01-01-2017, 19:12
  3. Coding challenge: Advent of code
    By Petr Schreiber in forum thinBasic General
    Replies: 10
    Last Post: 08-12-2015, 23:54

Members who have read this thread: 1

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •