Advertisement
musifter

AoC 2024, day 6, part 1 (smalltalk)

Dec 6th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.83 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend  [ value: arg [^arg perform: self] ]
  4. Integer extend [ % modulus [^self - 1 \\ modulus + 1] ]
  5.  
  6. Object subclass: IntervalGrid [
  7.     | grid dimX dimY start |
  8.  
  9.     IntervalGrid class >> new: mapArray [
  10.         ^super new init: mapArray
  11.     ]
  12.  
  13.     init: mapArray [
  14.         | rows cols |
  15.         dimY := mapArray size.
  16.         dimX := mapArray first size.
  17.  
  18.         rows := (1 to: dimY) collect: [:a | OrderedCollection new ].
  19.         cols := (1 to: dimX) collect: [:a | OrderedCollection new ].
  20.  
  21.         (1 to: dimY) do: [ :row |
  22.            | startBlock |
  23.             startBlock := 1.
  24.             (1 to: dimX) do: [ :col |
  25.                 (((mapArray at: row) at: col) = $#) ifTrue: [
  26.                     (rows at: row) add: (startBlock to: col - 1).
  27.                     startBlock := col + 1.
  28.                 ] ifFalse: [
  29.                     (((mapArray at: row) at: col) = $^) ifTrue: [
  30.                         start := (col @ row).
  31.                     ]
  32.                 ]
  33.             ].
  34.             (rows at: row) add: (startBlock to: dimX).
  35.         ].
  36.  
  37.         (1 to: dimX) do: [ :col |
  38.            | startBlock |
  39.             startBlock := 1.
  40.             (1 to: dimY) do: [ :row |
  41.                 (((mapArray at: row) at: col) = $#) ifTrue: [
  42.                     (cols at: col) add: (startBlock to: row - 1).
  43.                     startBlock := row + 1.
  44.                 ].
  45.             ].
  46.             (cols at: col) add: (startBlock to: dimX).
  47.         ].
  48.  
  49.         grid := {rows. cols}.
  50.         ^self
  51.     ]
  52.  
  53.     rowInterval: pt [
  54.         ^((grid at: 1) at: pt y) detect: [:a | a includes: pt x] ifNone: [nil].
  55.     ]
  56.  
  57.     colInterval: pt [
  58.         ^((grid at: 2) at: pt x) detect: [:a | a includes: pt y] ifNone: [nil].
  59.     ]
  60.  
  61.     dimX        [ ^dimX  ]
  62.     dimY        [ ^dimY  ]
  63.     start       [ ^start ]
  64. ]
  65.  
  66. "
  67. | Mainline
  68. "
  69. grid := IntervalGrid new: stdin lines contents.
  70.  
  71. dirs := { #(#colInterval: #first #y #y:).       " Up "
  72.           #(#rowInterval: #last  #x #x:).       " Right "
  73.           #(#colInterval: #last  #y #y:).       " Down "
  74.           #(#rowInterval: #first #x #x:) }.     " Left "
  75.  
  76. limX  := grid dimX - 1.
  77. limY  := grid dimY - 1.
  78.  
  79. visit := Set new.
  80. pos   := grid start.
  81. face  := 1.
  82.  
  83. [(pos x between: 2 and: limX) and: [pos y between: 2 and: limY]] whileTrue: [
  84.     table := dirs at: face.
  85.  
  86.     interval := grid perform: table first with: pos.
  87.     end      := interval perform: table second.
  88.  
  89.     range := (table second = #first)
  90.                 ifTrue:  [end to: (pos perform: table third)]
  91.                 ifFalse: [(pos perform: table third) to: end].
  92.  
  93.     range do: [:i | visit add: (pos perform: table last with: i) copy].
  94.  
  95.     pos perform: table last with: end.
  96.     face := (face + 1) % 4.
  97. ].
  98.  
  99. ('Part 1: %1' % {visit size}) displayNl.
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement