Advertisement
musifter

AoC 2023 day 13 (Smalltalk)

Dec 14th, 2023
1,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.86 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend     [ value: obj     [^obj perform: self] ]
  4.  
  5. Integer extend    [ oneBitSet      [^(self bitAnd: (self - 1)) = 0] ]
  6. Collection extend [ reduceAsBinary [^self inject: 0 into: [:num :dig | num*2 + dig]] ]
  7.  
  8. Array extend [
  9.     " Method for finding index of reflection line in part 1: "
  10.     findMirrorIndex [
  11.         | stack |
  12.         stack := OrderedCollection with: (self first).
  13.         self allButFirst keysAndValuesDo: [:i :val |
  14.             (val = stack last) ifTrue:  [ stack removeLast ]
  15.                                ifFalse: [ stack add: val   ].
  16.  
  17.             (stack isEmpty) ifTrue: [ ^(i+1) / 2 ]
  18.         ].
  19.         ^0
  20.     ]
  21.  
  22.     " Method for finding index of reflection line in part 2: "
  23.     findSmudgedIndex [
  24.         | check pushVal popVal back |
  25.  
  26.         (1 to: self size // 2) do: [:depth |
  27.             back  := depth * 2 + 1.
  28.             check := 0.
  29.             (1 to: depth) do: [:i |
  30.                 pushVal := self at: i.
  31.                 popVal  := self at: back - i.
  32.                 (pushVal ~= popVal) ifTrue: [
  33.                     " add one if 1 bit, 2 if more "
  34.                     check := check + ((pushVal bitXor: popVal) oneBitSet ifTrue:  [1]
  35.                                                                          ifFalse: [2]).
  36.                 ].
  37.             ].
  38.  
  39.             " Check equals 1 only when exactly one 1-bit difference is seen "
  40.             (check = 1) ifTrue: [ ^depth ].
  41.         ].
  42.         ^0
  43.     ]
  44.  
  45.     " Scoring for problem: "
  46.     score  [ ^100 * self first + self second ]
  47.  
  48.     scanForMirrors: symMethod [
  49.         | score |
  50.         " Try backwards mirrors: "
  51.         score := ((self collect: #reverse) collect: [:list | |val|
  52.                      val := list perform: symMethod.
  53.                      (val ~= 0) ifTrue: [ list size - val ] ifFalse: [0].
  54.                  ]) score.
  55.  
  56.         " Try forwards mirrors: "
  57.         score := score + (self collect: symMethod) score.
  58.  
  59.         ^score
  60.     ]
  61. ]
  62.  
  63. "
  64. | Mainline
  65. "
  66. input := stdin contents tokenize: '\n\n'.
  67.  
  68. grids := input collect: [:grid |
  69.              (grid tokenize: '\n') collect: [:line |
  70.                  line asArray collect: [:chr | (chr = $#) ifTrue: [1] ifFalse: [0]].
  71.              ]
  72.          ].
  73.  
  74. part1 := 0.
  75. part2 := 0.
  76.  
  77. grids do: [:grid |
  78.     " dimArrays has two elements: array of rows and array of cols "
  79.     dimArrays := {
  80.         " Parse rows as binary numbers: "
  81.         grid collect: #reduceAsBinary.
  82.  
  83.         " Parse cols as binary numbers: "
  84.         (1 to: grid first size) collect: [:col |
  85.             (grid collect: [:row | row at: col]) reduceAsBinary
  86.         ].
  87.     }.
  88.  
  89.     part1 := part1 + (dimArrays scanForMirrors: #findMirrorIndex).
  90.     part2 := part2 + (dimArrays scanForMirrors: #findSmudgedIndex).
  91. ].
  92.  
  93.  
  94. ('Part 1: %1' % {part1}) displayNl.
  95. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement