From ce14d7ac5c1a9e5fd918ac21ce20a9c519ad6999 Mon Sep 17 00:00:00 2001 From: eutro Date: Tue, 3 Dec 2024 08:47:06 +0000 Subject: [PATCH] Day 3 cleanup --- days/day03.st | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/days/day03.st b/days/day03.st index 14accf9..19b0d9b 100644 --- a/days/day03.st +++ b/days/day03.st @@ -1,24 +1,23 @@ Object subclass: VM [ - | enabled total | + | enabled total mulOnly | initialize [ enabled := true. total := 0. ] - total [ ^total ] + mulOnly: it [ mulOnly := it ] - interpret: insns - [ insns do: [ - :it | - (it scanf: 'mul(%d, %d)' - with: [ - :l :r | - enabled ifTrue: [ - total := total + (l * r) - ] - ]) - ifNil: [ enabled := (it = 'do()') ]. ]. - ^total ] + do: doOrDont [ mulOnly ifFalse: [ enabled := doOrDont ] ] + do [ self do: true ] dont [ self do: false ] + + perform: insn + [ insn = 'do()' ifTrue: [ ^self do ]. + insn = 'don''t()' ifTrue: [ ^self dont ]. + enabled ifTrue: [ + insn scanf: 'mul(%d,%d)' + with: [ :l :r | total := total + (l * r) ] ] ] + + interpret: insns [ insns do: [ :insn | self perform: insn ]. ^total ] ] -AOC input: [ stdin contents ]; - part1: 'mul\([0-9]{1,3},[0-9]{1,3}\)'; - part2: 'mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don''t\(\)'; - result: [ :inp :re | VM new interpret: (inp chain allOccurrencesOfRegex: re) ]; +AOC input: [ stdin contents allOccurrencesOfRegex: + 'mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don''t\(\)' ]; + part1: true; part2: false; + result: [ :insns :part | VM new mulOnly: part; interpret: insns ]; finish