Results 1 to 1 of 1

Thread: Advent of Code, 2024 - Day 03

Threaded View

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

    Advent of Code, 2024 - Day 03

    Hi,

    I would like to share a possible solution for day 3 of Advent of Code 2024.

    SPOILERS BELOW

    The assignment can be found at https://adventofcode.com/2024/day/3
    Please note that to reach the assignment you need to complete the assignment 2 first.

    Please make sure you download your input.txt file from the page above and that you pass it as parameter to the scripts below (for example in thinAir, you enter input.txt to Script/Command line... if you stored the file side by side with the solutions)

    Part 1

    This is the first assignment when correct pattern matching is required, in this case extracting valid mul(number1, number2) sequences again with some limitations. Regex can be used in such cases nicely.
    uses "console", "file"
    
    
    function tbmain()
      string inputFile = app.ArgV(2)
      
      if not file_exists(inputFile) then
        printl inputFile
        printl "Please specify valid input file as first parameter of the script"
        waitkey
        return 1
      end if
      
      string inputContent = file_load(inputFile)
    
      long result = CalculateValidMuls(inputContent)
      
      printl
      printl result, "is the result" in 14
      
      waitkey
    end function
    
    function CalculateValidMuls(inputText as string) as long
      long startPos = 1
      long lastLen = 0
      long result
      string textFound
      
      while startPos > 0
        textFound = regexpr$("mul[(][0-9]+[,][0-9]+[)]", inputText, startPos, startPos, lastLen)
        
        startPos = startPos + lastLen
        
        if startPos > 0 then
          result += val(grab$(textFound, "(", ",")) * val(grab$(textFound, ",", ")"))
          printl textFound
        end if
      wend
      
      return result
    end function
    
    Part 2

    Second part complicates the assignment by introduction of do() and don't() directives, which serve as flags enabling and disabling the validity of mul statements.
    uses "console", "file"
    
    
    function tbmain()
      string inputFile = app.ArgV(2)
      
      if not file_exists(inputFile) then
        printl inputFile
        printl "Please specify valid input file as first parameter of the script"
        waitkey
        return 1
      end if
      
      string inputContent = file_load(inputFile)
      
      long result = CalculateValidMulsFromEnabledSections(inputContent)
      
      printl
      printl result, "is the result" in 14
      
      waitkey
    end function
    
    function CalculateValidMulsFromEnabledSections(inputText as string) as long
      inputText = "do()" + inputText
      long result
      
      long pos = 1
      long nextDo, nextDont
      
      Repeat
        nextDo   = instr(pos, inputText, "do()")
        nextDont = instr(nextDo, inputText, "don't()")
        
        result += CalculateValidMuls(mid$(inputText, nextDo, iif(nextDont = 0, len(inputText), nextDont)-nextDo))
    
        pos = nextDont
      until pos = 0 
      
      return result
    end function
    
    function CalculateValidMuls(inputText as string) as long
      long startPos = 1
      long lastLen = 0
      long result
      string textFound
      
      while startPos > 0
        textFound = regexpr$("mul[(][0-9]+[,][0-9]+[)]", inputText, startPos, startPos, lastLen)
        
        startPos = startPos + lastLen
        
        if startPos > 0 then
          result += val(grab$(textFound, "(", ",")) * val(grab$(textFound, ",", ")"))
          printl textFound
        end if
      wend
      
      return result
    end function
    
    Petr
    Last edited by Petr Schreiber; 23-12-2024 at 15:37.
    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

Similar Threads

  1. Advent of Code, 2024 - Day 02
    By Petr Schreiber in forum Challenge
    Replies: 4
    Last Post: 30-12-2024, 16:24
  2. Advent of Code, 2024 - Day 01
    By Petr Schreiber in forum Challenge
    Replies: 0
    Last Post: 09-12-2024, 12:10
  3. thinbasic 2024 BUG HUNT
    By ReneMiner in forum thinBasic Beta testing
    Replies: 2
    Last Post: 15-08-2024, 10:41
  4. Advent of Code, 2017
    By Petr Schreiber in forum Challenge
    Replies: 4
    Last Post: 13-12-2017, 08:50
  5. 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: 3

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
  •