GridOS 1 quest 3, basic

This time we’re digging one or more ponds deeper. It took a long time for me to think of this solution.

In part 1 it’s 1 layer deeper, in part 2 2 layers, and in part 3 as many layers as there’s room for.

Part 3 might get a little crazy…

I’m using a mechanism, where I can look at 3 characters and write in a 4th position. In part 3 I have a 5th head, keeping track of whether I need to dig deeper. I also write extra characters, to make sure I stop at the end of each line.

All my code .

#ThisWeeksFiddler, 20260612

This week the #puzzle is: Can You Catch the Longest Wave? #geometry #trigonometry #coding #approximation #maximum #average

Semicircle Island is shaped like a perfect semicircle (or semidisk, technically), with a radius of 1 mile. It doesn’t have any permanent residents, but it’s a very popular destination for surfers.
Rumor has it that a big wave is headed toward the island, but no one knows which direction it’s coming from. This thin, straight wall of water never changes speed or direction. It will first make contact with the island at 10 a.m. and it will last be in contact with the island at 10:10 a.m.
What is the longest possible stretch of land that is directly under the wave at 10:05 a.m.?

And for extra credit:

Another wave is approaching the island, but again no one knows which direction it’s coming from—for the moment, all directions are equally likely. For example, it might come in the direction illustrated below:
On average, what is the length of the stretch of land directly under the wave halfway between when the wave first and last makes contact with the island?

Can You Catch the Longest Wave?

Solution, possibly incorrect:

Program Desmos Video

Method 1: Recreate the situation in Desmos and play around to find a maximum value. (The Desmos document includes an animation. So does the video 😉 )

Method 2: Write a program to systematically go through a lot of options and find a maximum value.

The methods share a lot of the same math.

Assuming the island is a circle, but with 0 <= x, it makes sense to only look at some of the possible angles for the wave.

0θπ20\le \theta \le \frac{\pi}{2}

Here the angle is the one between the x axis and a line perpendicular to the wave. The wave is a tangent to the circle, and the line crosses the wave at the tangent point. (This wave could be the incoming wave or the outgoing wave.) This point can now also be described as:

x1=cos(θ),y1=sin(θ)x_1=\cos(\theta), y_1=\sin(\theta)

So now we have a definition of the line representing the wave:

yy1=x1y1(xx1)y-y_{1}=-\frac{x_{1}}{y_{1}}\left(x-x_{1}\right)

The wave on the other side of the island goes through (0,-1) and has the same slope.

y(1)=x1y1(x0)y-\left(-1\right)=-\frac{x_{1}}{y_{1}}\left(x-0\right)

To learn something about the wave in the middle, the one we will try to restrict to the island, learn the length of and maximize, we begin with a single point on that line:

x2=x1+02,y2=y1+(1)2x_{2}=\frac{x_{1}+0}{2},y_{2}=\frac{y_{1}+\left(-1\right)}{2}

And now we can describe the line segment:

yy2=x1y1(xx2){0x1y2}y-y_{2}=-\frac{x_{1}}{y_{1}}\left(x-x_{2}\right)\left\{0\le x\le\sqrt{1-y^{2}}\right\}

To learn more, we need exact descriptions for the end points of the segment. The top point will either be on the y axis or on the circle.

x3a=0x_{3a}=0

x1y1(xx2)+y2=1x2-\frac{x_{1}}{y_{1}}\left(x-x_{2}\right)+y_{2}=\sqrt{1-x^{2}}

Changing this to the standard form:

ax2+bx+c=0ax²+bx+c=0

a=x12y12+1a=\frac{x_{1}^{2}}{y_{1}^{2}}+1

b=2x12x2y122x1y2y1b=-\frac{2x_{1}^{2}x_{2}}{y_{1}^{2}}-\frac{2x_{1}y_{2}}{y_{1}}

c=x12x22y12+y22+2x1x2y2y11c=\frac{x_{1}^{2}x_{2}^{2}}{y_{1}^{2}}+y_{2}^{2}+\frac{2x_{1}x_{2}y_{2}}{y_{1}}-1

x3b=bb24ac2ax_{3b}=\frac{-b-\sqrt{b^{2}-4ac}}{2a}

x3=max(x3a,x3b),y3=x1y1(x3x2)+y2x_{3}=\max\left(x_{3a},x_{3b}\right),y_{3}=-\frac{x_{1}}{y_{1}}\left(x_{3}-x_{2}\right)+y_{2}

And for the bottom point:

x4=b+b24ac2a,y4=x1y1(x4x2)+y2x_{4}=\frac{-b+\sqrt{b^{2}-4ac}}{2a},y_{4}=-\frac{x_{1}}{y_{1}}\left(x_{4}-x_{2}\right)+y_{2}

And now we can calculate the distance between the points:

d=(x4x3)2+(y4y3)2d=\sqrt{\left(x_{4}-x_{3}\right)^{2}+\left(y_{4}-y_{3}\right)^{2}}

Playing around with the original angle, it’s possible to find a max for d. This happens at:

θ=0.339837\theta=0.339837

dmax=1.885618d_{max}=1.885618

The program reaches the same conclusion. 1.885618.

And for extra credit:

Instead of looking for a max, convert the program to add all the distances together, to find an average.

Result: 1.304439.

I’m sure there’s a way to find a function, where d pops out, and then integrate over it for all angles. But, you know.

GridOS 1 quest 2, less steps

Part 1 uses less steps if it comes from both ends at once. Like with quest 1, this requires more states.

Part 2. Oh, part 2. Several things going on here. (1) There are at most 5 lines. So I can do the 2 heads looking at a pair trick for all 5 lines at once with 10 heads. But. (2) Now there are 45 different combinations to account for in the rules. Enter generated code. Once I figured out this was the way to do it, I used a PHP program of 52 lines to write a GridOS program of 1379 lines. (Including comments and blank lines.)

Part 3 is part 2, but more. I still use 5 heads to look at the lines, but then use states to remember the previous characters. Then I use 5 more heads to write @ in certain situations. If I remember correctly, the first 5 heads always write @, when there’s a vertical pair. (So 5 lines are reduced to 4.) This one actually looks kind of pretty:

All my code .

GridOS 1 quest 2, 1 head

Part 1 looks very similar to the basic version. 2 heads is converted into 1 head + 1 state.

The same goes for part 2.

Part 3 is a little more tricky, and I didn’t optimize my solution. My 1 head looks at all 3 characters (this one, the one below it, and the one to the right of it). Vertical pair, write @ above. Horizontal pair, replace this character. Move on. There’s a little extra work, if the position above isn’t empty. Also I write a character at the beginning of the line, so that I can find it again easily later.

Moving up until I find an empty spot:

All my code .

GridOS 1 quest 2, basic

A quest 2, part 1 solution might look like this:

There’s a rule saying, that when a characters appears 2 times in a row, it produces an @. So with 2 heads, I can look at 2 characters at the same time, and I have rules for each of the cases: AA, AB, BA and BB.

For part 2, there can be several lines. I go right on the 1st line, left on the 2nd etc.

For part 3, pairs can occur both horizontally and vertically. I solve this by using 2×2 heads + 1 extra head for writing @. This is because when there are pairs in both directions, I need to write 2 @’s. 1 @ will replace the character appearing in both pairs, the other @ will just be written above the 1st line.

All my code .

GridOS 1 quest 1, less steps

Trying to get a better position in the steps part of the competition, I wrote new programs.

Using more heads, I attack the string from both ends. This requires more rules, because I don’t have to just address the cases A, B and C, but also “A on one end, B on the other” etc., 9 combinations in all. It’s also a little more complicated detecting, that I am done.

I do something similar for part 2:

For part 3, I already had a nice 10 head program, so I didn’t do anything more.

Bonus: I experimented with using less rules for part 2. Because there are 5 heads, a lot of the time I can just write 5 P’s. And I gain a step, because I only use 1 step to spread the heads.

All my code .

GridOS 1 quest 1, more heads

With quest 1 I was still learning the language. After looking at other solutions, I did my own versions with more heads. Part 1:

This is a lot more elegant, and it uses fewer steps. Part 2 has the same nice look:

For part 3 I had to be a little more creative. 10 heads couldn’t quite write 12 P’s, just like that. So I added a little bit, where the last 2 P’s were added after the first 10.

All my code .

GridOS 1 quest 1, 1 head

I tried to write 1 head versions of all programs.

The head travels right. If it encounters an A, delete. If it encounters an B, replace. If it encounters a C, write 3 P’s by going in a little loop. The loop goes either up or down, alternating. That felt very nice and elegant.

Part 2 is very similar, the loop might just be bigger.

Because there might be 2 lines in part 3, instead of a loop, I just go straight up or down. The 1st line produces 0-5 P’s, the 2nd line produces 0-7 P’s. I use E and F to keep track of my state. E means the 1st line produced 0 P’s.

All my code .

GridOS 1 quest 1, basic

A quest 1, part 1 solution might look like this (I will get back this 3 head solution later):

The basic task is: Given a string of A, B and C, use rules to turn these into P’s. E.g., C turns into 3 P’s.

My first shot actually looks a little boring. 2 heads, the yellow to read the string, the red to write P’s. The red one quickly moves too far right to stay in the frame.

Part 2 added a D, turning into 5 P’s.

And part 3 has 2 strings interacting. This time the green head is writing the P’s. I have rules for every situation: A means 0 P’s, AA means 2 P’s etc. DD means 12 P’s and happens a lot below. I also have rules for counting P’s (as in previous iterations). AA moves unto the state ONE, meaning that state is responsible for writing 1 P and then returning to reading.

I could improve on these programs by writing some of the P’s on top of the strings. I guess my sense of order threw me a little here. It’s nice to have a writing line and a reading line.

The 2 state versions of the programs usually look a lot like the basic versions.

All my code .

#ThisWeeksFiddler, 20260605

This week the #puzzle is: Can You Infer the Color of Your Hat? #logic #permutations

Three game show contestants must work together to win a prize. They are shown a large bag that’s initially empty, and then they see three red hats and two white hats placed into the bag. At this point, the contestants are blindfolded. Each contestant then picks a hat at random from the bag and places it on their head.
One at a time, their blindfolds are removed and they can see the hats on the others’ heads—but not the hat on their own head. If they can, with absolute certainty, identify the color of the hat on their own head, then the game is over and all three contestants win a prize! Otherwise, they skip their turn, at which point the next contestant has their blindfold removed and the process continues. If all three contestants skip their turn, then no prize is won. While blindfolded, contestants can still hear the decisions of other contestants and can identify who said what.
Will the contestants always win the prize? If so, why? If not, why not?

And for extra credit:

Now, there are four game show contestants who must work together. They are shown a large bag that’s initially empty, and then they see R red hats, W white hats, and B blue hats placed into the bag, such that R, W, and B are each less than or equal to 4. Otherwise, the game is played as before.
For some triples (R, W, B), the contestants can always win. Among these, what is the greatest value of R + W + B?

Can You Infer the Color of Your Hat?

Solution, possibly incorrect:

Let’s look at the cases:

  • Contestant 1 sees 2 white hats. Therefore their own hat must be red.
  • Contestant 1 does not see 2 white hats. Inconclusive.
    • Contestant 2 sees contestant 3 wearing a white hat. Therefore their own hat must be red.
    • Contestant 2 does not see contestant 3 wearing a white hat. Inconclusive.
      • Contestant 3 knows, that they have a red hat.

Result: Yes, there will always be a contestant able to figure out the color of their own hat.

Let me also illustrate this with a table showing the different situations. There are 7 of them. XYZ means contestant 1 (C1) is wearing a hat with color X etc. (WWW isn’t possible.)

SituationC1C2C3
RWW 🔴⚪⚪RED!
WRW ⚪🔴⚪?RED!
RRW 🔴🔴⚪?RED!
WWR ⚪⚪🔴??RED!
RWR 🔴⚪🔴??RED!
WRR ⚪🔴🔴??RED!
RRR 🔴🔴🔴??RED!

And for extra credit:

R red hats, W white hats, B blue hats. Each number is at most 4. It is at least 0. (My guess is a maximal solution will actually have each number be at least 1.)

0R,W,B40\le R,W,B\le 4

Each contestant picks a hat. There must be 4 hats.

4R+W+B4\le R+W+B

WLOG:

BWRB\le W \le R

Let’s look at the options written as RWB. So 444 means 4 hats of each color. Each row shows the options with the same sum.

444
443
442433
441432333
440431422332
430421331322
420411330321222
410320311221
400310220211

The fiddler corresponds to 320, and we know the answer to that one.

5max(R+W+B)5\le \max(R+W+B)

My intuition is, that each contestant must be able to reduce the number of cases. Therefore there must be information enough in 3 hats to move forward. This could be ?30 or ?21. Let’s examine 430.

  • C1 sees 3 white hats. Therefore their own hat must be red.
  • C1 doesn’t see 3 white hats.
    • C2 sees C3 and C4 wearing white hats. Therefore their own hat must be red.
    • C2 doesn’t see C3 and C4 wearing white hats.
      • C3 sees C4 wearing a white hat. Therefore their own hat must be red.
      • C3 doesn’t see C4 wearing a white hat.
        • C4 knows their own hat must be red.

So, updated:

7max(R+W+B)127\le \max(R+W+B)\le12

The case with 421 is actually similar. C1 either sees 3 non-blue hats or not, etc.

I’m not sure how to choose a case with 8 hats and test it properly. So my guess is there are 7 hats.