From 6b558d93306823c7235cef56065f566ad897e800 Mon Sep 17 00:00:00 2001 From: eutro Date: Sat, 7 Dec 2024 11:38:55 +0000 Subject: [PATCH] Day 7 cleanup --- days/day07.st | 25 +++++++++++++++---------- days/utils.st | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/days/day07.st b/days/day07.st index 26d92b9..3a492a0 100644 --- a/days/day07.st +++ b/days/day07.st @@ -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 | - Equation new target: target asNumber; - numbers: ((numbers tokenize: ' ') collect: [:it|it asNumber]) ] ] + [^(line tokenize: ': ') letArrayInBlock: [ + :target :numbers | + Equation new target: target asNumber; + numbers: ((numbers tokenize: ' ') + collect: [:it|it asNumber])]] ] AOC input: [ stdin toLines collect: [:line | Equation parse: line] ]; @@ -42,6 +47,6 @@ AOC input: [ stdin toLines collect: [:line | Equation parse: line] ]; part2: {CatOp new. MulOp new. AddOp new}; result: [ :eqns :part | eqns chain select: [:eqn | eqn canSolveWith: part]; - collect: [:it|it testValue]; + collect: [:it | it testValue]; sum ]; finish. diff --git a/days/utils.st b/days/utils.st index b5883d2..8112623 100644 --- a/days/utils.st +++ b/days/utils.st @@ -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." + + + | 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 + ] +]