Advertisement
musifter

AoC 2024, day 1 (smalltalk)

Dec 1st, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.56 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend      [ value: arg  [^arg perform: self] ]
  4. Collection extend  [ sum [ ^self inject: 0 into: [:a :b | a + b] ] ]
  5.  
  6. "
  7. | Mainline
  8. "
  9. list1 := Bag new.
  10. list2 := Bag new.
  11.  
  12. stdin linesDo: [ :line |
  13.     items := line subStrings collect: #asNumber.
  14.     list1 add: items first.
  15.     list2 add: items second.
  16. ].
  17.  
  18. part1 := (list1 sorted with: list2 sorted collect: [:a :b | (a - b) abs]) sum.
  19. part2 := (list1 collect: [:a | a * (list2 occurrencesOf: a)]) sum.
  20.  
  21. ('Part 1: %1' % {part1}) displayNl.
  22. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement