Post Reply 
Advents of Code (programming challenges)
12-07-2022, 11:50 PM
Post: #6
RE: Advents of Code (programming challenges)
This first day's project, as all the rest, has a much longer and far more creative description than I'll give here (see the link from the original post in this thread). Each day has a part 1 and part 2 puzzle to solve.

I'll paraphrase the descriptions just so that you can get some idea what the programs do here. This first pair is quite simple, and there's actually more comments than code in the programs I used here. I suspect they choose the first couple problems as "easy winners" to get people interested.

Part 1:

Find the largest sum of the grouped integers given as input. Group boundaries are given as blank lines (or the end of input) for the given input string. See the example input below for the actual format of the data.

Code:
@ %%HP2: T(3)A(R)F(.)M(=)C(R)B(H);
@ Name: 'D1P1'
@ Size: 124.0 bytes, CRC: 03A4h
@ 12/07/22 05:30:00P
@ Libraries used:
@   1423: ListExt Commands

\<<
   @ place input string on stack
   D1dat
   
   @ format input string to create a list of sublists
   { 10 10 } NL\->S "}{" SREPL DROP
   10 CHR " " SREPL DROP
   "{{" SWAP + "}}" +
   
   @ convert re-formatted string to list of sublists
   STR\->
   
   @ create a list of subtotals
   ::LSUM LMAP
   
   @ find the largest subtotal
   LMAX
\>>

The bulk of the above code is in formatting the input as a string in the proper format for a list of lists. Once formatted, the string is converted to an actual list, then each subgroup is summed and the largest sum is the result (left on stack level 1 here).



Part 2:

Using the same data as above, find the top 3 largest sums and add them up for the final result.

Code:
@ %%HP2: T(3)A(R)F(.)M(=)C(R)B(H);
@ Name: 'D1P2'
@ Size: 137.5 bytes, CRC: E22Bh
@ 12/07/22 05:30:00P
@ Libraries used:
@   1423: ListExt Commands

\<<
   @ place input string on stack
   D1dat
   
   @ format input string to create a list of sublists
   { 10 10 } NL\->S "}{" SREPL DROP
   10 CHR " " SREPL DROP
   "{{" SWAP + "}}" +
   
   @ convert re-formatted string to list of sublists
   STR\->
   
   @ create a list of subtotals
   ::LSUM LMAP
   
    @ sort
    @ (use LSORT if you have it -- which you should!)
    SORT
    
    @ take last three ( top 3 quantities )
    -3 LTAKE
    
    @ sum the top 3
    LSUM
\>>

This one is very similar to the first, and the first 4 code sections are exactly the same. At that point, there is a list of group totals on the stack, which is then sorted, and finally the top 3 are isolated and summed. As before, the final result is left in stack level 1.

Here's the sample data provided by the site for testing purposes:

D1dat
@ sample
"1000
2000
3000

4000

5000
6000

7000
8000
9000

10000"


Using the above sample data, the final result for part 1 is 24000, and the final result for part 2 is 45000.

The final data supplied by the site is 2244 lines of numbers and blanks, and I won't include it here. Feel free to go to the site and take a look! Registration may be required, which can be done as "anonymous" if desired.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Advents of Code (programming challenges) - DavidM - 12-07-2022 11:50 PM



User(s) browsing this thread: 1 Guest(s)