--
You received this message because you are subscribed to
the "Pick and MultiValue Databases" group.
To post, email to: mvd...@googlegroups.com
To unsubscribe, email to: mvdbms+un...@googlegroups.com
For more options, visit http://groups.google.com/group/mvdbms
---
You received this message because you are subscribed to the Google Groups "Pick and MultiValue Databases" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mvdbms+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mvdbms/3fd39bad-74af-43cb-9a75-df6f8b36ba78n%40googlegroups.com.
I took a different approach using a state machine. I learned this when working with a compiler guy on the formula compiler for CompuSheet+. It makes for a larger program and one that looks a lot more complicated but once it's compiled it can be very efficient. It performs very similar to Ian's code but is a little slower than his.
The basic idea is that you keep track of the current state of your parsing to determine how to process the next character. At the beginning, there's no state. In that state the only characters you care about are "m" and "d" because that's the possible start of mul(xxx,xxx), do() and don't(). Once you hit that character you know the next one needs to be a "u" or "o". If it's not one of those then you reset the state and continue with the next character.
This process continues until you get to the final end parenthesis
at which point you've got either a correct mul(xxx,xxx)
expreression or the do() or don't(). If you get an invalid
character at any point, you reset the state and continue on
looking for the next "m" or "d".
This can seem more complicated but if the rules are clearly
defined it can be very flexible. As a bonus it can help you
determine why an expression is wrong. Like if you wanted to flag
any mul(xxx,xxx) that had alpha characters instead of numbers.
This makes that sort of things easy.
>RUN BP.JG ADVENT.DAY.3
All Answer = 192767529
DO.MUL = 104083373
It took 21 ms
OPEN 'BP.JG' TO F.BP.JG ELSE STOP 201,'BP.JG' READ LINES FROM F.BP.JG, 'ADVENT.DAY3.INPUT' ELSE STOP 202,'ADVENT.DAY3.INPUT' START = NOW() DO.MUL = 1 CHR = '' GOSUB RESET.STATE DO.MUL.ANSWER = 0 ANSWER = 0 LOOP REMOVE LINE FROM LINES SETTING DELIM IF LINE NE '' THEN LN = LEN(LINE) FOR X = 1 TO LN CHR = LINE[X,1] BEGIN CASE CASE STATE = '' IF CHR = 'm' THEN STATE = CHR END ELSE IF CHR = 'd' THEN STATE = CHR END ELSE GOSUB RESET.STATE END END CASE STATE = 'm' IF CHR = 'u'THEN STATE = CHR END ELSE GOSUB RESET.STATE END CASE STATE = 'u' IF CHR = 'l' THEN STATE = 'l' END ELSE GOSUB RESET.STATE END CASE STATE = 'l' IF CHR = '(' THEN STATE = CHR END ELSE GOSUB RESET.STATE END CASE STATE = '(' IF CHR >= '0' AND CHR <= '9' THEN MUL1 := CHR END ELSE IF CHR = ',' THEN STATE = CHR END ELSE GOSUB RESET.STATE END END CASE STATE = ',' IF CHR >= 0 AND CHR <= '9' THEN MUL2 := CHR END ELSE IF CHR = ')' THEN *PRINT EXPRESSION PRODUCT = MUL1*MUL2 *PRINT MUL1:'*':MUL2:'=':PRODUCT ANSWER += PRODUCT IF DO.MUL THEN DO.MUL.ANSWER += PRODUCT END END GOSUB RESET.STATE END CASE STATE = 'd' IF CHR = 'o' THEN STATE = CHR END ELSE GOSUB RESET.STATE END CASE STATE = 'o' IF CHR = '(' THEN STATE = 'o(' END ELSE IF CHR = 'n' THEN STATE = CHR END ELSE GOSUB RESET.STATE END END CASE STATE = 'o(' IF CHR = ')' THEN DO.MUL = 1 END GOSUB RESET.STATE CASE STATE = 'n' IF CHR = "'" THEN STATE = CHR END ELSE GOSUB RESET.STATE END CASE STATE = "'" IF CHR = 't' THEN STATE = CHR END ELSE GOSUB RESET.STATE END CASE STATE = 't' IF CHR = '(' THEN STATE = 't(' END ELSE GOSUB RESET.STATE END CASE STATE = 't(' IF CHR = ')' THEN DO.MUL = 0 END GOSUB RESET.STATE CASE 1 GOSUB RESET.STATE END CASE NEXT END UNTIL DELIM = 0 DO REPEAT MILLISECONDS = NOW() - START PRINT 'All Answer = ':ANSWER PRINT 'DO.MUL = ':DO.MUL.ANSWER PRINT PRINT 'It took ':MILLISECONDS:' ms' STOP RESET.STATE: IF CHR = 'm' OR CHR = 'd' THEN STATE = CHR END ELSE STATE = '' END MUL1 = '' MUL2 = '' RETURN
To view this discussion visit https://groups.google.com/d/msgid/mvdbms/7d92bd11-6d19-452f-9be4-5bef55585021n%40googlegroups.com.
Thanks! I had that idea to help with debugging. It does help
although even on my 43" 4k display the characters are difficult to
read at 134 columns X 134 rows.
I tried part 2 yesterday but haven't gotten a correct answer yet.
I might try working on it again if I have time today.
On Dec 9, 2024, at 12:22 AM, Ian McGowan <ian.m...@gmail.com> wrote:
Everyone should write a shell, at least once :)
--
You received this message because you are subscribed to
the "Pick and MultiValue Databases" group.
To post, email to: mvd...@googlegroups.com
To unsubscribe, email to: mvdbms+un...@googlegroups.com
For more options, visit http://groups.google.com/group/mvdbms
---
You received this message because you are subscribed to the Google Groups "Pick and MultiValue Databases" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mvdbms+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mvdbms/35279abd-b1af-4407-93cd-acbed3591e2dn%40googlegroups.com.
Ian,
When you notice that the following permutation:
+++
++*
+*+
+**
*++
*+*
**+
***
is just 8 binary numbers (replace + with 0 and * with 1), you’ll see that you can use the following code with no permutation preprocessing:
195 M = DCOUNT(TERMS,CHAR(254))
196 MAX = 2^(M-1)
197 FOR I = 0 TO MAX-1 UNTIL SUM = VAL
198 OP = I
199 * USE BINARY NUMBERS TO PERFORM PERMUTATIONS OF +*
200 SUM = TERMS<1>
201 FOR J = 2 TO M UNTIL SUM > VAL
202 X = MOD(OP,2)
203 IF X=0 THEN SUM += TERMS<J> ELSE IF X=1 THEN SUM *= TERMS<J>
204 OP = INT(OP/2)
205 NEXT J
206 NEXT I
And likewise for 3 operations, use base 3 instead of base 2. It’s still brute force to apply these operations, but if there is a way to avoid nested loops, I couldn’t find it.
Tom
To view this discussion visit https://groups.google.com/d/msgid/mvdbms/cc6461d8-dc7c-4530-b2fc-c72e271dd002n%40googlegroups.com.
Don’t be afraid of 10 day. It’s just a DFS and count the number of paths from each 0 to each 9.
Tom
From: mvd...@googlegroups.com <mvd...@googlegroups.com> On Behalf Of Ian McGowan
Sent: Tuesday, December 10, 2024 7:52 AM
To: Pick and MultiValue Databases <mvd...@googlegroups.com>
Subject: Re: [mvdbms] Advent of Code 2024
Yes! I believe Universe had them first. It's magical in two senses - a) it's bringing PICK Basic into the 90's :-), and b) I believe the subroutine or function gets split out to some system generated object and compiled independently. So it's almost a trick of the precompiler, not the basic runtime. It made the debugger a little confused when running from within my STACK program...
To view this discussion visit https://groups.google.com/d/msgid/mvdbms/cc6461d8-dc7c-4530-b2fc-c72e271dd002n%40googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mvdbms/72cc676f-1b3d-44ee-b25a-4dde1e417513%40goldthwaites.com.