Advertisement
musifter

AoC 2023 day 18, part 2 (Smalltalk)

Dec 18th, 2023 (edited)
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.75 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. String extend [ asRadix: rad [^Number readFrom: (ReadStream on: self) radix: rad] ]
  4.  
  5. "
  6. | Mainline
  7. "
  8. area   := 0.
  9. pos    := (0 @ 0).
  10. trench := 0.
  11.  
  12. stdin linesDo: [ :line |
  13.     parts := line subStrings.
  14.  
  15.     delta := (parts third atAll: (3 to: 7)) asRadix: 16.
  16.     dir   := (parts third at: 8) digitValue.
  17.  
  18.     vert := (dir \\ 2) = 1.                         " U and D "
  19.     (dir >= 2) ifTrue: [ delta := delta * -1 ].     " U and L "
  20.  
  21.     vert ifTrue:  [ pos y: (pos y + delta) ]
  22.          ifFalse: [ pos x: (pos x + delta) ].
  23.  
  24.     area := area + (delta * (vert ifTrue: [pos x] ifFalse: [-1 * pos y])).
  25.     trench := trench + delta abs.
  26. ].
  27.  
  28. part2 := (area abs + trench) / 2 + 1.
  29. ('Part 2: %1' % {part2}) displayNl.
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement