Carl Love

Carl Love

28035 Reputation

25 Badges

12 years, 325 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@segfault There is no command s-convert. It's just the previously defined series s minus all of its terms other than the order term. In other words, the `-` is simply the ordinary subtraction operator, with its second operand being the result of the convert command.

Even easier:

TypeTools:-AddType(gtoint, And(posint, Not(1)));

This avoids two procedure calls every time that the type is checked: a call to `type/satisfies` and a call to the anonymous procedure s-> s > 1

Unlike satisfiesAnd and Not are built-in type constructors, which don't involve procedure calls.

@salim-barzani The fact that there are extra characters in your input is not completely hidden. Look closely at the solve command in your Question and the one in acer's Answer. For the one in your Question, notice that there is extra white space before the A[0] {between the { and the A). Suspicious extra white space in 2D input usually means extra, unseen, characters. Once you get used to it, these extra white spaces "stick out like a sore thumb" (an English idiom meaning "immediately visibly obvious").

@Rouben Rostamian  Maple strings, as we know them today, were introduced in Maple 6 (I think). Before that, there was no syntactic distinction between symbols (a subtype of names) and strings. They were only distinguished by their intended use. The command substring comes from that time.

Note that substring works for strings and symbols. It doesn't work for names that aren't symbols.

@janhardo  You wrote:

  • [T]he moderator could have put your wrong post under the existing question as an example and reminded you again of the correct procedure to ask questions.

No, that's not possible. Unfortunately, the MaplePrimes software doesn't have any option for a Moderator to move an item (posted by another author) to another thread. The only options are 

  • edit the item (including possibly its header info or title) in its current location;
  • delete it;
  • change its overall type, such as changing a Post to a Question or an Answer to a Reply (within fhe same Question);
  • move it within its own thread, such as changing to which Answer a Reply is attached.

The MaplePrimes software ought to be changed to allow a Moderator to move a Question to a followup Reply to a previous Question. Unfortunately, it seems that changes to the software are only made occasionally, every 5 years or so.

A Moderator---or anyone else for that matter---can make a "didactic" (as you say) Reply. The problem with this is that if the OP doesn't move the item, then the primary purpose of Moderation is not achieved. That primary purpose is to reduce the number of extraneous threads, which enhances the readability and searchability of MaplePrimes for all users, for all time.

This was the first Advent of Code problem that I tried. It seemed very easy. Indeed, as you suggested, parsing the text input is harder than doing the calculations.

Dedicated integer solution techniques are not needed. It can be done in each case by finding the rational-number solution of a pair of linear equations and discarding any non-integer solutions. Some extra work might be required if there were cases of multiple solutions, but that didn't occur. My CPU time for all 640 problems (both parsing and computation) was less than 1/2 second.

Advent of Code solution for 2024-Dec-13
Reference: https://adventofcode.com/2024/day/13
Maple solution by Carl Love <carl.j.love@gmail.com>

restart
:

interface(prompt= "")
:

# These operators have nothing specific to do with this particular problem. Rather,
# they are operator constructors that I very frequently use:

`&<`:= eval(curry):  `&>`:= eval(rcurry)
:
ST:= eval(StringTools): LA:= eval(LinearAlgebra): CT:= eval(CodeTools)
:
st:= time()
:
# The raw problem input is entered as a string in this Code Edit Region. For me,
# this is far more convenient and portable than using a text file:

Advent of Code 2024 Day 13 raw input

# After each command, I show the first few entries, just so the reader and I
# can stay oriented:
Raw[..128];

"Button A: X+49, Y+27
Button B: X+35, Y+65
Prize: X=4326, Y=4898

Button A: X+82, Y+64
Button B: X+20, Y+67
Prize: X=6818, Y=1040"

(* Split by paragraph (StringSplit \n\n), then by line (Split \n), obtaining a listlist.
Then use RegSplit to extract the 6 positive integers (as strings) from each paragraph.
Then use 'parse' to convert strings to numbers. Then make each paragraph's numbers in a 3x2
mstrix and transpose (^~%T) it.
*)
use StringTools in  #RegSplit, Split, StringSplit
    Data:= (
        (Matrix &< (3,2) @ parse~ @ RegSplit~ &< "[^0-9]+" @ Split &> "\n")~ @ StringSplit
     )(Raw, "\n\n") ^~%T
end use:
%[..7];

[Matrix(2, 3, {(1, 1) = 49, (1, 2) = 35, (1, 3) = 4326, (2, 1) = 27, (2, 2) = 65, (2, 3) = 4898}), Matrix(2, 3, {(1, 1) = 82, (1, 2) = 20, (1, 3) = 6818, (2, 1) = 64, (2, 2) = 67, (2, 3) = 10409}), Matrix(2, 3, {(1, 1) = 75, (1, 2) = 95, (1, 3) = 8360, (2, 1) = 72, (2, 2) = 15, (2, 3) = 4749}), Matrix(2, 3, {(1, 1) = 59, (1, 2) = 15, (1, 3) = 7401, (2, 1) = 26, (2, 2) = 29, (2, 3) = 3032}), Matrix(2, 3, {(1, 1) = 77, (1, 2) = 15, (1, 3) = 13582, (2, 1) = 38, (2, 2) = 46, (2, 3) = 10664}), Matrix(2, 3, {(1, 1) = 39, (1, 2) = 27, (1, 3) = 1367, (2, 1) = 11, (2, 2) = 40, (2, 3) = 2880}), Matrix(2, 3, {(1, 1) = 50, (1, 2) = 20, (1, 3) = 7220, (2, 1) = 28, (2, 2) = 55, (2, 3) = 383})]

#Part 1 solution:
Sol1:= CT:-Usage(
    <3,1> . add(select(type, LA:-LinearSolve~(Data), 'Vector'(nonnegint)))
);

memory used=23.54MiB, alloc change=37.00MiB, cpu time=47.00ms, real time=274.00ms, gc time=15.62ms

36250

#Part 2 extra number:
n:= 10000000000000
:

#Part 2 solution:
UpD:= <0,0,n; 0,0,n>:
Sol2:= CT:-Usage(
    <3,1> . add(select(type, LA:-LinearSolve~(Data +~ 'UpD'), 'Vector'(nonnegint)))
);

memory used=15.09MiB, alloc change=0 bytes, cpu time=62.00ms, real time=214.00ms, gc time=0ns

83232379451012

#CPU time for the whole shebang:
time()-st;

.422

 

Download AoC_2024_Dec_13.mw

@Carl Love Nevermind, I've confirmed that the essential difference is the use of "C""P", and "T" rather than C, P, and T.

@Samir Khan Could you please explain how your CP, which works, is different from the OP's CP(T1(t)), which doesn't work? Is it simply the quotation marks on the C, P, and T that are the crucial difference?

@nm Yes, the only reason that I did it with sprintf instead of concatenation was to get the leading zero(s) in a programmatically general way. The reason to use leading zeros is so that a standard string sort of the filenames will come out in numeric order.

Also, sprintf is a standard command in many languages, and it's much older than Maple.

@dharr I'd like to see your syntax error. This works perfectly for me:

restart;
P:= proc(x) sin(x) end proc:
P2:= subs(sin= evalhf@sin, subsop(3= (op(3,eval(P)), hfloat), eval(P)));
P2(3);

@mehdi jafari Sorry, I don't know how to use these commands or if they work in the current version of Maple, and I don't have the time right now to figure it out. Note that I wrote the above Answer over 6 years ago, and even at that time I suspected that those commands weren't up to date.

Is the SaveAll given by @stefanv above sufficient for your needs?

@dharr Yes, you are correct if we are using the standard definition of cycle, and I was sloppy not to think of that. However, for the purpose of checking transitivity, it is sufficient to consider any walk that returns to its starting vertex to be a "generalized cycle".

Since we need to forbid edges that are the reverse of each other, we could check that A *~ A^+ = 0 as a first step.

Thank you for this Post. I will start looking at these puzzles.

As you noted, checking the transitivity of the rules while ignoring the subsets of the rules that are actually used for the needed sorts is a red herring. However, if one did need to check the acyclicity of a digraph on vertices, a computationally efficient way to do it (just off the top of my head) is to add the first n powers of its adjacency matrix. Calling this sum T, the graph is acyclic iff trace(T) = 0, and any nonzero on the diagonal corresponds to a vertex that is part of a cycle. (For an adjacency matrix A(A^k)[i,j] is the number of walks of length from to j. A cycle containing i is a walk from i to i.) I suspect that this is more computationally efficient (again, just off the top of my head) because it doesn't keep track of what the actual cycles are, only which vertices are part of any cycle.

By the way, why is this the Advent of Code 2025 rather than 2024?

Like most food, pizza has a substantial quantity of water, even after it's baked. If it didn't, it would be extremely crunchy, more so than a cracker. The boiling temperature of water is 212 degrees F (at standard atmospheric pressure). Just because it comes out of a 350-degree oven doesn't mean that its temperature is 350 degrees: Its temperature never equilibrated in the oven. (This also means that the surface temperature is more than the maximum internal temperature, so Newton's Law can't be easily applied.) The standard food-safety recommendation in the USA is to cook things until they reach an internal temperature of 165 degrees.  

@Preben Alsholm Ah, thanks Preben. So the reason that I've never seen this is that I don't use 2D Input.

4 5 6 7 8 9 10 Last Page 6 of 708