Advertisement
musifter

AoC 2023 day 6 (Smalltalk)

Dec 6th, 2023
1,761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.03 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend     [ value: arg  [^arg perform: self]                    ]
  4. Collection extend [ product     [^self inject: 1 into: [:a :b | a * b]] ]
  5.  
  6. Array extend [
  7.     countWins [
  8.         | time dist discrim upper lower |
  9.         " ASSUME: size of array is at least 2 "
  10.         time := self first.
  11.         dist := self second + 1.    " Need at least one more for the win "
  12.  
  13.         discrim := (time * time - (4 * dist)) sqrt.
  14.         upper := ((time + discrim) / 2) floor.
  15.         lower := ((time - discrim) / 2) ceiling.
  16.  
  17.         ^upper - lower + 1
  18.     ]
  19. ]
  20.  
  21. "
  22. | Mainline
  23. "
  24. input := stdin lines contents collect: [:line | line subStrings allButFirst].
  25.  
  26. " Parse input into pairs of values for part one "
  27. table := input collect: [:line | line collect: #asNumber].
  28. cols  := (table first) with: (table second).
  29.  
  30. " Join for part 2 "
  31. concat := input collect: [:line | line join asNumber].
  32.  
  33. ('Part 1: %1' % {(cols collect: #countWins) product}) displayNl.
  34. ('Part 2: %1' % {concat countWins}) displayNl.
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement