Day 7 cleanup
This commit is contained in:
parent
2e5c426dcf
commit
6b558d9330
2 changed files with 42 additions and 10 deletions
|
|
@ -6,7 +6,9 @@ Object subclass: MulOp
|
|||
^tgt quo: n ] ]
|
||||
MulOp subclass: CatOp
|
||||
[ tryDo: n target: tgt
|
||||
[ | p10 | p10 := 10 raisedTo: ((n + 1) log: 10) ceiling.
|
||||
[ | p10 |
|
||||
p10 := (n + 1) ceilingLog: 10.
|
||||
p10 := 10 raisedTo: p10.
|
||||
^super tryDo: p10 target: tgt - n ] ]
|
||||
|
||||
Object subclass: Equation [
|
||||
|
|
@ -17,24 +19,27 @@ Object subclass: Equation [
|
|||
|
||||
printOn: dst [dst << target << ': ' << numbers]
|
||||
|
||||
"Backtracking search right-to-left over the number sequence."
|
||||
canSolveFrom: idx target: tgt with: ops
|
||||
[ | n | n := numbers at: idx.
|
||||
[ | n n1 ok | n := numbers at: idx.
|
||||
idx = 1 ifTrue: [^n = tgt].
|
||||
tgt < n ifTrue: [^false].
|
||||
ops do: [
|
||||
:op |
|
||||
(op tryDo: n target: tgt) ifNotNil:
|
||||
[:it|(self canSolveFrom: idx - 1 target: it with: ops) ifTrue: [^true]]].
|
||||
:op | n1 := op tryDo: n target: tgt.
|
||||
n1 ifNotNil: [
|
||||
ok := self canSolveFrom: idx - 1 target: n1 with: ops.
|
||||
ok ifTrue: [^true] ]].
|
||||
^false ]
|
||||
canSolveWith: ops [^self canSolveFrom: numbers size target: target with: ops]
|
||||
]
|
||||
|
||||
Equation class extend [
|
||||
parse: line
|
||||
[ ^(line tokenize: ': ') letArrayInBlock:
|
||||
[ :target :numbers |
|
||||
[^(line tokenize: ': ') letArrayInBlock: [
|
||||
:target :numbers |
|
||||
Equation new target: target asNumber;
|
||||
numbers: ((numbers tokenize: ' ') collect: [:it|it asNumber]) ] ]
|
||||
numbers: ((numbers tokenize: ' ')
|
||||
collect: [:it|it asNumber])]]
|
||||
]
|
||||
|
||||
AOC input: [ stdin toLines collect: [:line | Equation parse: line] ];
|
||||
|
|
|
|||
|
|
@ -254,3 +254,30 @@ Object subclass: Grid [
|
|||
|
||||
printOn: st [ rows do: [ :r | st << r; nl ] ]
|
||||
]
|
||||
|
||||
Integer extend [
|
||||
ceilingDiv: denom [ ^((self - 1) // denom) + 1 ]
|
||||
|
||||
"The original implementation was incorrect. Here is the correct code:"
|
||||
ceilingLog: radix [
|
||||
"Answer (self log: radix) ceiling. Optimized to answer an integer."
|
||||
|
||||
<category: 'math methods'>
|
||||
| me answer |
|
||||
self < self zero ifTrue:
|
||||
[^self arithmeticError: 'cannot extract logarithm of a negative number'].
|
||||
radix <= radix unity ifTrue:
|
||||
[radix <= radix zero
|
||||
ifTrue: [^self arithmeticError: 'base of a logarithm cannot be negative'].
|
||||
radix = radix unity
|
||||
ifTrue: [^self arithmeticError: 'base of a logarithm cannot be 1'].
|
||||
^(self floorLog: radix reciprocal) negated].
|
||||
radix isInteger ifFalse: [^(radix coerce: self) ceilingLog: radix].
|
||||
me := self.
|
||||
answer := 1.
|
||||
[me > radix] whileTrue:
|
||||
[me := me ceilingDiv: radix. "originally: me // radix."
|
||||
answer := answer + 1].
|
||||
^answer
|
||||
]
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue