Advent Of Code

Everything else that doesn't fall into one of the other PB categories.
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Advent Of Code

Post by Janni »

Anyone heard of this one ?
https://adventofcode.com/
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Sounds like a fun challenge with PureBasic.....
Last edited by Janni on Wed Dec 07, 2022 9:54 pm, edited 1 time in total.
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: adventofcode.com

Post by Janni »

Here is my take on day 1:

Code: Select all

OpenConsole()

Structure Reindeer
  number.s
  calories.l
EndStructure

NewList deerlist.Reindeer()

 ReadFile(0, "input_01.txt", #PB_File_SharedRead)
   While Eof(0) = 0
      linje.l = Val(ReadString(0))
      If linje.l > 0
        sumcal.l = linje.l + sumcal.l
      ElseIf linje.l = 0
        count.l = count.l + 1
      AddElement(deerlist())
      deerlist()\number = Str(count.l)
      deerlist()\calories = sumcal.l
      sumcal.l = 0  
      EndIf
   Wend 
   
   ; First star of magical energy:
    SortStructuredList(deerlist(), #PB_Sort_Descending, OffsetOf(Reindeer\calories), TypeOf(Reindeer\calories))
    FirstElement(deerlist())
    PrintN("First star of magical energy: "+deerlist()\calories)
    
    ; Second star of magical energy:
    For x = 0 To 2
      totcal.l = totcal.l + deerlist()\calories
      NextElement(deerlist())
    Next
    PrintN("Second star of magical energy: "+totcal.l)
    
    Input()
  CloseFile(0) 
CloseConsole()
End 
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: adventofcode.com

Post by Janni »

For day 2 I decided to do some hard-coding since it was only nine possible outcomes. Perhaps not very sophisticated, but sometimes I guess it can be faster and more reliable.

Code: Select all

OpenConsole()

NewList rockpaperscissor.s()

 ReadFile(0, "input_02.txt", #PB_File_SharedRead)
   While Eof(0) = 0
      linje.s = ReadString(0)
      AddElement(rockpaperscissor())
      rockpaperscissor()= linje 
    Wend 

   ForEach rockpaperscissor()
    Select rockpaperscissor()
      Case "A X"
        count.l = count + 4
      Case "A Y" 
        count.l = count + 8
      Case "A Z"
        count.l = count + 3
      Case "B X"
        count.l = count + 1
      Case "B Y"
        count.l = count + 5
      Case "B Z"
        count.l = count + 9
      Case "C X"
        count.l = count + 7
      Case "C Y"
        count.l = count + 2
      Case "C Z"
        count.l = count + 6     
    EndSelect
    
   Next
  
     PrintN("First star of magical energy: "+ count)
    
    Input()
  CloseFile(0) 
CloseConsole()
End 
Part B

Code: Select all

Case "A X"
        count.l = count + 3 ;
      Case "A Y" 
        count.l = count + 4 ;
      Case "A Z"
        count.l = count + 8 ;
      Case "B X"
        count.l = count + 1 ;
      Case "B Y"
        count.l = count + 5 ;
      Case "B Z"
        count.l = count + 9 ;
      Case "C X"
        count.l = count + 2 ; 
      Case "C Y"
        count.l = count + 6 ;
      Case "C Z"
        count.l = count + 7 ;     
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: adventofcode.com

Post by Janni »

Day 3 part 1:

Code: Select all

OpenConsole()

Structure innhold
  sekk.l
  compa.s
  compb.s
  equal.s
EndStructure

NewList ryggsekk.innhold()

 ReadFile(0, "input_03.txt", #PB_File_SharedRead)
   While Eof(0) = 0
     indeks.l = 1 + indeks
     linje.s = ReadString(0)
     coma.s = Left(linje,Len(linje)/2)
     comb.s = Right(linje,Len(linje)/2)
     
     For loop = 1 To (Len(coma))
        found.l = FindString(comb, Mid(coma, loop, 1),1,#PB_String_CaseSensitive)  
        
        If found >0
          pos.l = found
          equ.s = Mid(comb,found,1)
        EndIf   
      Next
          
      AddElement(ryggsekk())
        ryggsekk()\sekk = indeks
        ryggsekk()\compa = coma
        ryggsekk()\compb = comb
        ryggsekk()\equal = equ
   Wend 
   CloseFile(0)  
    
  NewMap poeng.l()
    For x = 1 To 26
      poeng(Chr(x+96))=x
    Next
    For x = 27 To 52  
      poeng(Chr(x+38))=x
    Next
  
  ForEach ryggsekk()
    totpoeng.l = poeng(ryggsekk()\equal) + totpoeng
  Next
  
  PrintN("First star of magical energy: "+totpoeng)

    Input()
  
CloseConsole()
End 
Part 2:

Code: Select all

OpenConsole()

Structure innhold
  sekk.l
  compa.s
  compb.s
EndStructure

NewList ryggsekk.innhold()

Structure n
  equal.s
EndStructure
NewList nokler.n()

 ReadFile(0, "input_03.txt", #PB_File_SharedRead)
   While Eof(0) = 0
     indeks.l = 1 + indeks
     linje.s = ReadString(0)
     coma.s = Left(linje,Len(linje)/2)
     comb.s = Right(linje,Len(linje)/2)
     
     For loop = 1 To (Len(coma))
        found.l = FindString(comb, Mid(coma, loop, 1),1,#PB_String_CaseSensitive)  
        
        If found >0
          pos.l = found
          equ.s = Mid(comb,found,1)
        EndIf   
      Next
          
      AddElement(ryggsekk())
        ryggsekk()\sekk = indeks
        ryggsekk()\compa = coma+comb
   Wend 
   CloseFile(0)  
      
  NewMap poeng.l()
    For x = 1 To 26
      poeng(Chr(x+96))=x
    Next
    For x = 27 To 52  
      poeng(Chr(x+38))=x
    Next
    
    ; search string from set of three lines and find char
    FirstElement(ryggsekk())
    Repeat  
        oneofthree.s = ryggsekk()\compa
        NextElement(ryggsekk())
          twoofthree.s = ryggsekk()\compa
        NextElement(ryggsekk())
          threeofthree.s = ryggsekk()\compa
          
         For loop = 1 To Len(oneofthree)
           found.l = FindString(twoofthree, Mid(oneofthree, loop, 1),1,#PB_String_CaseSensitive) ;search letter for letter 
            If found >0
              key.s = Mid(twoofthree,found,1)
              found.l = FindString(threeofthree, key, 1)
              If found >0
                keys.s = Mid(threeofthree,found,1)
              EndIf 
            EndIf   
          Next
          
          AddElement(nokler())
          nokler()\equal = keys
          
          curr.l = ListIndex(ryggsekk()) +1
          size.l = ListSize(ryggsekk())
          NextElement(ryggsekk())
      Until curr = size
  
  ForEach nokler()
   totpoeng.l = poeng(nokler()\equal) + totpoeng
  Next
   
  PrintN("Second star of magical energy: "+totpoeng)

    Input()
CloseConsole()
End 
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: adventofcode.com

Post by Janni »

How I solved Day 4 part 1:

Code: Select all

OpenConsole()

Structure innhold
  pair1a.l
  pair1b.l
  pair2a.l
  pair2b.l
  overlap.l
EndStructure

NewList liste.innhold()

 ReadFile(0, "input_04.txt", #PB_File_SharedRead)
   While Eof(0) = 0
     linje.s = ReadString(0)
     tmpleft.s = StringField(linje,1,",")
     tmpright.s = StringField(linje,2,",")
     papir1a.s = StringField(tmpleft,1,"-")
     papir1b.s = StringField(tmpleft,2,"-")
     papir2a.s = StringField(tmpright,1,"-")
     papir2b.s = StringField(tmpright,2,"-")
        
     AddElement(liste())
        liste()\pair1a = Val(papir1a)
        liste()\pair1b = Val(papir1b)
        liste()\pair2a = Val(papir2a)
        liste()\pair2b = Val(papir2b)
   Wend 
   CloseFile(0)  

   poeng.l = 0 
   ForEach liste()
     
     If (liste()\pair1a <= liste()\pair2a And liste()\pair1b >= liste()\pair2b) 
        poeng.l = 1+poeng
      ElseIf (liste()\pair2a <= liste()\pair1a And liste()\pair2b >= liste()\pair1b)
        poeng.l = 1+poeng
      EndIf
  Next
 
  PrintN("First star of magical energy: "+poeng)
    Input()
CloseConsole()
End 
How I solved Day 4 part 2:

Code: Select all

OpenConsole()

Structure innhold
  pair1a.l
  pair1b.l
  pair2a.l
  pair2b.l
  overlap.l
EndStructure

NewList liste.innhold()

 ReadFile(0, "input_04.txt", #PB_File_SharedRead)
   While Eof(0) = 0
     linje.s = ReadString(0)
     tmpleft.s = StringField(linje,1,",")
     tmpright.s = StringField(linje,2,",")
     papir1a.s = StringField(tmpleft,1,"-")
     papir1b.s = StringField(tmpleft,2,"-")
     papir2a.s = StringField(tmpright,1,"-")
     papir2b.s = StringField(tmpright,2,"-")
        
     AddElement(liste())
        liste()\pair1a = Val(papir1a)
        liste()\pair1b = Val(papir1b)
        liste()\pair2a = Val(papir2a)
        liste()\pair2b = Val(papir2b)
   Wend 
   CloseFile(0)  
   
   poeng.l = 0 
   ForEach liste()
     
     If (liste()\pair1a >= liste()\pair2b And liste()\pair1a <= liste()\pair2b) 
        poeng.l = 1+poeng
      ElseIf (liste()\pair1b >= liste()\pair2a And liste()\pair1b <= liste()\pair2b)
        poeng.l = 1+poeng
      ElseIf (liste()\pair2a >= liste()\pair1a And liste()\pair2a <= liste()\pair1b)
        poeng.l = 1+poeng
      ElseIf(liste()\pair2b >= liste()\pair1a And liste()\pair2b <= liste()\pair1b)
        poeng.l = 1+poeng
      EndIf
  Next
    
  PrintN("First star of magical energy: "+poeng)

    Input()
CloseConsole()
End 
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: adventofcode.com

Post by Janni »

How I solved day 6 part 1

Code: Select all

OpenConsole()

ReadFile(0, "input_06.txt", #PB_File_SharedRead)
   While Eof(0) = 0
     input.s = ReadString(0)
   Wend 
   CloseFile(0)  
   
   CreateRegularExpression(0, "(?=^[A-Za-z0-9]+$)(.)+.*\1.*") ;if a string contains only unique characters
   For loop = 0 To Len(input)
    char.s = Mid(input,loop,4)
      If MatchRegularExpression(0, char) = 0
        answer = loop+3
        Break
      EndIf        
    Next
    
    PrintN("First star of magical energy: "+answer)
Input()
CloseConsole()
End 
How I solved day 6 part 2

Code: Select all

OpenConsole()

ReadFile(0, "input_06.txt", #PB_File_SharedRead)
   While Eof(0) = 0
     input.s = ReadString(0)
   Wend 
   CloseFile(0)  
   
   CreateRegularExpression(0, "(?=^[A-Za-z0-9]+$)(.)+.*\1.*") ;if a string contains only unique characters
   For loop = 1 To Len(input)
    char.s = Mid(input,loop,14)
    If MatchRegularExpression(0, char) = 0
      answer = loop+13
      Break
    EndIf
  Next
  
  PrintN("Second star of magical energy: "+answer)

Input()
CloseConsole()
End 
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
Post Reply