Advertisement
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]] ]
- Object subclass: ClawMachine [
- | a b target |
- ClawMachine class >> new: anArray [^super new init: anArray]
- init: array [
- a := (array first @ array second).
- b := (array third @ array fourth).
- target := ((array at: 5) @ array last).
- ^self
- ]
- " Adjust claw target by offset: "
- adjustTarget: offset [target := target + offset]
- " Get cost in tokens to win (0 if no win possible): "
- solveCost [
- | a_num b_num denom |
- a_num := (target x * b y) - (target y * b x).
- b_num := (target y * a x) - (target x * a y).
- denom := (a x * b y) - (a y * b x).
- (a_num \\ denom == 0) & (b_num \\ denom == 0) ifTrue: [
- ^(3 * a_num / denom) + (b_num / denom).
- ].
- ^0
- ]
- ]
- "
- | Mainline
- "
- " Grab input as paragraphs, grab all numbers in each paragraph: "
- para := stdin contents tokenize: '\n\n'.
- input := para collect: [:p | ((p tokenize: '[^0-9]') reject: #isEmpty) collect: #asNumber].
- machines := input collect: [:m | ClawMachine new: m].
- part1 := (machines collect: #solveCost) sum.
- part2 := (machines collect: [:m | (m adjustTarget: 10_000_000_000_000) solveCost]) sum.
- ('Part 1: %1' % {part1}) displayNl.
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement