Day 13 cleanup
No inputs are linearly dependent, remove the case
This commit is contained in:
parent
c4bac0aa4f
commit
6aba245667
1 changed files with 10 additions and 33 deletions
|
|
@ -1,51 +1,28 @@
|
||||||
Object subclass: Machine [
|
Object subclass: Machine [
|
||||||
| vecA vecB prize |
|
| vecA vecB prize matrix |
|
||||||
parse: line
|
parse: line
|
||||||
[ line scanf: 'Button A: X+%d, Y+%d Button B: X+%d, Y+%d Prize: X=%d, Y=%d'
|
[ line scanf: 'Button A: X+%d, Y+%d Button B: X+%d, Y+%d Prize: X=%d, Y=%d'
|
||||||
with: [ :aX :aY :bX :bY :pX :pY |
|
with: [ :aX :aY :bX :bY :pX :pY |
|
||||||
vecA := Posn x: aX y: aY.
|
vecA := Posn x: aX y: aY.
|
||||||
vecB := Posn x: bX y: bY.
|
vecB := Posn x: bX y: bY.
|
||||||
prize := Posn x: pX y: pY ] ]
|
prize := Posn x: pX y: pY.
|
||||||
|
matrix := self computeMatrix ] ]
|
||||||
|
|
||||||
printOn: st
|
printOn: st
|
||||||
[ st << '{A: ' << vecA << '; B: ' << vecB << '; P: ' << prize << '}' ]
|
[ st << '{A: ' << vecA << '; B: ' << vecB << '; P: ' << prize << '}' ]
|
||||||
|
|
||||||
priceA [^3] priceB [^1]
|
computeMatrix
|
||||||
|
[ ^(Mat2 col: vecA col: vecB) inverse
|
||||||
|
ifNil: [self error: 'Linearly dependent'] ]
|
||||||
|
|
||||||
solve: offV
|
solve: offV
|
||||||
[ ^(Mat2 col: vecA col: vecB) inverse
|
[ | vec | vec := matrix *> (prize + offV).
|
||||||
ifNotNil: [
|
|
||||||
:inv | | vec |
|
|
||||||
vec := inv *> (prize + offV).
|
|
||||||
(vec x isInteger and: [vec y isInteger])
|
(vec x isInteger and: [vec y isInteger])
|
||||||
ifTrue: [^vec]
|
ifTrue: [^vec] ifFalse: [^nil] ]
|
||||||
ifFalse: [^nil] ]
|
|
||||||
ifNil: [
|
|
||||||
"linearly dependent -- not actually reached"
|
|
||||||
| slvA slvB |
|
|
||||||
'Linearly dependent!' displayNl.
|
|
||||||
slvA := self solveLinDep: vecA.
|
|
||||||
slvB := self solveLinDep: vecB.
|
|
||||||
slvA ifNil: [
|
|
||||||
slvB ifNil: [^nil]
|
|
||||||
ifNotNil: [^Posn x: 0 y: slvB]].
|
|
||||||
slvB ifNil: [^Posn x: slvA y: 0].
|
|
||||||
((slvA * self priceA) <= (slvB * self priceB))
|
|
||||||
ifTrue: [^Posn x: slvA y: 0]
|
|
||||||
ifFalse: [^Posn x: 0 y: slvB]] ]
|
|
||||||
|
|
||||||
tokenCost: soln
|
tokenCost: soln [ soln ifNil: [^0]. ^(soln x * 3) + soln y ]
|
||||||
[ soln ifNil: [^0].
|
|
||||||
^(soln x * self priceA) + (soln y * self priceB) ]
|
|
||||||
|
|
||||||
solveCost: offV [ ^self tokenCost: (self solve: offV) ]
|
solveCost: offV [ ^self tokenCost: (self solve: offV) ]
|
||||||
|
|
||||||
solveLinDep: vec
|
|
||||||
[ | sf |
|
|
||||||
sf := prize x / vec x.
|
|
||||||
(prize y / vec y) = sf ifFalse: [^nil].
|
|
||||||
sf isInteger ifFalse: [^nil].
|
|
||||||
^sf ]
|
|
||||||
]
|
]
|
||||||
|
|
||||||
AOC input: [ stdin chain contents; tokenize: (String with: $<10> with: $<10>);
|
AOC input: [ stdin chain contents; tokenize: (String with: $<10> with: $<10>);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue