Results 1 to 3 of 3

Thread: Advent of Code, 2024 - Day 02

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

    Advent of Code, 2024 - Day 02

    Hi,

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

    SPOILERS BELOW

    The assignment can be found at https://adventofcode.com/2024/day/2
    Please note that to reach the assignment you need to complete the assignment 1 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
    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)
    
    
      string lines()
      long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
    
    
      
      long i
      long sequenceRating, safeCount
      string description
      for i = 1 to entryCount
        ' Printing the analysed line + extra space to space out later rating
        print lset$(lines(i), 32 using " ")
        
        sequenceRating = GetSequenceRating(lines(i), description)
        
        if sequenceRating = 0 Then
          incr safeCount
        end if
    
    
        ' Optional explanation of sequence rating
        printl description in iif(sequenceRating = 0, 10, 12)
        
      next
      
      printl
      printl safeCount, "safe items" in 14
    
    
      waitkey
    end function
    
    
    function GetSequenceRating(sequenceText as string, byref description as string) as long
      long j, originalDirection, direction, diff
      long values()
      long valueCount = split(sequenceText, " ", values)
      
      
      for j = 2 to valueCount
        diff      = values(j) - values(j-1)
        direction = sgn(diff)
        
        if j = 2 then
          if diff = 0 Then
            description = "UNSAFE - no direction"
            return 1
          end if
        
          originalDirection = direction
        end if
        
        if direction <> originalDirection Then
          description = "UNSAFE - inconsistent direction"
          return 2
        end if
        
        if not between(abs(diff), 1, 3) Then
          description = "UNSAFE - too big gap"
          return 3
        end if
        
        if j = valueCount then
          description = "SAFE"
          return 0
        end if
      next
        
    end function
    
    Part 2
    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)
    
    
      string lines()
      long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
      
      long i, j
      long sequenceRating, safeCount
      string description, alteredSequence
      for i = 1 to entryCount
        ' Printing the analysed line + extra space to space out later rating
        print lset$(lines(i), 32 using " ")
        
        sequenceRating = GetSequenceRating(lines(i), description)
        
        if sequenceRating = 0 Then
          incr safeCount
        else
          ' The original form is not valid, let's alter it by removing specific element
          for j = 1 to parsecount(lines(i), " ")
            alteredSequence = GetSequenceWithoutNthElement(lines(i), j)
            if GetSequenceRating(alteredSequence, description) = 0 Then
              incr safeCount
              exit for
            end if
          next
        end if
        
        ' Optional explanation of sequence rating
        printl description in iif(sequenceRating = 0, 10, 12)
        
      next
      
      printl
      printl safeCount, "safe items" in 14
    
    
      waitkey
    end function
    
    
    function GetSequenceRating(sequenceText as string, byref description as string) as long
      long j, originalDirection, direction, diff
      long values()
      long valueCount = split(sequenceText, " ", values)
      
      
      for j = 2 to valueCount
        diff      = values(j) - values(j-1)
        direction = sgn(diff)
        
        if j = 2 then
          if diff = 0 Then
            description = "UNSAFE - no direction"
            return 1
          end if
        
          originalDirection = direction
        end if
        
        if direction <> originalDirection Then
          description = "UNSAFE - inconsistent direction"
          return 2
        end if
        
        if not between(abs(diff), 1, 3) Then
          description = "UNSAFE - too big gap"
          return 3
        end if
        
        if j = valueCount then
          description = "SAFE"
          return 0
        end if
      next
        
    end function
    
    
    function GetSequenceWithoutNthElement(sequenceText as string, n as long) as string
      return trimfull$(ParseSet$(sequenceText, " ", n, ""))
    end function
    

    Petr
    Last edited by Petr Schreiber; 19-12-2024 at 21:31.
    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
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    52
    Posts
    944
    Rep Power
    111
    Hello Petr doesn't Work Here your both examples or I am too stupid for IT :-)
    "Please specify valid Input File AS First Parameter of the Script" ???
    Regards frank
    you can't always get what you want, but if you try sometimes you might find, you get what you need

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

    thanks for stopping by!

    You need to launch the script with input file as parameter. Each Advent of Code challenge has specific input for each person.

    If you go to:
    - https://adventofcode.com/2024
    - Log In via preffered way

    ...you can start the Advent of Code challenge yourself and download the input from the challenge assignment. It is a simple text file.

    Then, in thinAir, you can go to Script / Command line to supply path to the puzzle input.

    Note: You need to start with day 1 to unlock day 2.


    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

Similar Threads

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

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
  •