Day 3 cleanup

This commit is contained in:
Beatrice Szilvasy 2024-12-03 08:47:06 +00:00
parent a0a507b89c
commit ce14d7ac5c

View file

@ -1,24 +1,23 @@
Object subclass: VM [ Object subclass: VM [
| enabled total | | enabled total mulOnly |
initialize [ enabled := true. total := 0. ] initialize [ enabled := true. total := 0. ]
total [ ^total ] mulOnly: it [ mulOnly := it ]
interpret: insns do: doOrDont [ mulOnly ifFalse: [ enabled := doOrDont ] ]
[ insns do: [ do [ self do: true ] dont [ self do: false ]
:it |
(it scanf: 'mul(%d, %d)' perform: insn
with: [ [ insn = 'do()' ifTrue: [ ^self do ].
:l :r | insn = 'don''t()' ifTrue: [ ^self dont ].
enabled ifTrue: [ enabled ifTrue: [
total := total + (l * r) insn scanf: 'mul(%d,%d)'
] with: [ :l :r | total := total + (l * r) ] ] ]
])
ifNil: [ enabled := (it = 'do()') ]. ]. interpret: insns [ insns do: [ :insn | self perform: insn ]. ^total ]
^total ]
] ]
AOC input: [ stdin contents ]; AOC input: [ stdin contents allOccurrencesOfRegex:
part1: 'mul\([0-9]{1,3},[0-9]{1,3}\)'; 'mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don''t\(\)' ];
part2: 'mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don''t\(\)'; part1: true; part2: false;
result: [ :inp :re | VM new interpret: (inp chain allOccurrencesOfRegex: re) ]; result: [ :insns :part | VM new mulOnly: part; interpret: insns ];
finish finish