Results 1 to 1 of 1

Thread: Advent of Code, 2024 - Day 01

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,153
    Rep Power
    736

    Lightbulb Advent of Code, 2024 - Day 01

    Hi,

    there is a nice algorithmic challenge each year, called Advent of Code.

    Link for this year is:
    https://adventofcode.com/2024

    I attach my naive solutions, we can think if and how they can be improved, if you want.

    SPOILERS BELOW

    Link to the task assignment: https://adventofcode.com/2024/day/1

    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
    
      ' First, let's load each column individually
      long leftList(entryCount)
      long rightList(entryCount)
    
      long columns(2)
      for i as long = 1 to entryCount
        split(lines(i), "   ", columns)
        leftList(i)  = columns(1)
        rightList(i) = columns(2)
      next
    
      ' ...for easy individual sort
      array sort leftList(), ascend
      array sort rightList(), ascend
    
      ' ...to calculate distance score
      long distance
      for i as long = 1 to entryCount
        distance += abs(rightList(i) - leftList(i))
      next
    
      printl distance, "is the distance" in 14
    
      waitkey
    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
      
      ' In theory, I would not need to load whole file to memory in this exercise, but it is small
      string inputContent = file_load(inputFile)
    
      string lines()
      long entryCount = parse(inputContent, lines, $LF) - 1 ' Last line left empty
    
      ' I will store key-value pairs of number & its occurence count for each column
      dword totalColumnNumberCounts(2)  = hash_new(entryCount), hash_new(entryCount)
      long rowValue(2)
      
      ' Let's find number of value occurences in each column
      long idOccurrence(2)
      for i as long = 1 to entryCount
        split(lines(i), "   ", rowValue)
      
        IncreaseHashValue(totalColumnNumberCounts(1), rowValue(1), 1)
        IncreaseHashValue(totalColumnNumberCounts(2), rowValue(2), 1)
      next
    
      ' First, we need to find unique keys in the left column
      string idsCommaSeparated = Hash_GetKeys(totalColumnNumberCounts(1), ",")
      string id()
      parse(idsCommaSeparated, id, ",")
      
      long similarity
      
      ' Second, we calculate the similarity score
      for i as long = 1 to countof(id)
        idOccurrence(1) = hash_get(totalColumnNumberCounts(1), id(i))
        idOccurrence(2) = hash_get(totalColumnNumberCounts(2), id(i))
        
        similarity += val(id(i)) * idOccurrence(1) * idOccurrence(2)
      next
      
      hash_free(totalColumnNumberCounts(1))
      hash_free(totalColumnNumberCounts(2))
    
      printl similarity, "is the similarity score" in 14
    
      waitkey
    end function
    
    function IncreaseHashValue(hashHandle as dword, hashValue as long, amount as long)
      long currentCount = val(hash_get(hashHandle, hashValue)) + amount
    
      hash_set(hashHandle, hashValue, currentCount)
    end function
    
    Last edited by Petr Schreiber; 19-12-2024 at 21:30.
    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. thinbasic 2024 BUG HUNT
    By ReneMiner in forum thinBasic Beta testing
    Replies: 2
    Last Post: 15-08-2024, 10:41
  2. Advent of Code, 2017
    By Petr Schreiber in forum Challenge
    Replies: 4
    Last Post: 13-12-2017, 08:50
  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: 2

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
  •