Advertisement
musifter

AoC day 16 (pt1), Smalltalk

Dec 16th, 2020
2,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. " Split input by sections "
  4. section := stdin contents tokenize: '\n\n'.
  5.  
  6. " First section: rules "
  7. rules := (section first) tokenize: '\n'.
  8.  
  9. " Make set of all valid numbers to test against "
  10. valid := Set new.
  11. rules do: [ :s | (s =~ '(\d+)-(\d+) or (\d+)-(\d+)') ifMatched: [ :r |
  12.                   (r at: 1) asNumber to: (r at: 2) asNumber do: [:n | valid add: n].
  13.                   (r at: 3) asNumber to: (r at: 4) asNumber do: [:n | valid add: n].
  14.               ].
  15.           ].
  16.  
  17. " Third section: other tickets "
  18. " Turn tickets into OrderedCollections of numbers "
  19. others := ((section third) subStrings: Character nl) removeFirst; yourself.
  20. others := (others collect: [:t | (t subStrings: '\n,') collect: [:n | n asNumber]]).
  21.  
  22. " Get invalid numbers from each ticket and sum them "
  23. invalid := others collect: [:o | ((o reject: [:n | valid includes: n])
  24.                                      inject: 0 into: [:a :b | a + b])].
  25.  
  26. " Sum over all tickets to get results "
  27. stdout nextPutAll: ('Part 1: ', (invalid inject: 0 into: [:a :b| a + b]) asString); nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement