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