Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ sum [^self inject: 0 into: [:a :b | a + b]] ]
- Dictionary subclass: Graph [
- <shape: #pointer>
- | memo |
- " Returns list of number of paths through 0, 1, or 2 special (#fft or #dac) nodes "
- pathsFrom: node [
- memo ifNil: [ memo := Dictionary new ].
- ^memo at: node ifAbsentPut: [
- (node == #out)
- ifTrue: [OrderedCollection from: #(1 0 0)]
- ifFalse: [
- | res |
- res := OrderedCollection from: #(0 0 0).
- (self at: node) do: [:child |
- res := (self pathsFrom: child) with: res collect: [:a :b | a + b].
- ].
- ((node == #fft) or: [node == #dac]) ifTrue: [
- res removeLast; addFirst: 0. " rotate list to shift values up "
- ].
- res
- ]
- ]
- ]
- ]
- "
- | Mainline
- "
- graph := Graph from: (stdin lines contents collect: [:line | | words |
- words := line substrings.
- words first allButLast asSymbol -> (words allButFirst collect: #asSymbol)
- ]).
- ('Part 1: %1' % {(graph pathsFrom: #you) sum}) displayNl.
- ('Part 2: %1' % {(graph pathsFrom: #svr) third}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment