I examine each nail, and when I encounter the head, I hammer it. This is also a 1 head program. For part 2 I have to find the shortest nails and hammer the rest down to that length. I use 3 heads and some meta data along the edges.
I examine each line from left to right to find the length. Then I examine all the lines from top to bottom. This way I can change state to “1st head found” and “now I just have to delete the rest” easily. This program could easily be optimized. Instead of writing my own @ at the beginning of each line, I could use the #’s already there. And I note which lines doesn’t have to change, so I could actually skip them quickly.
For part 3, the 2 shortest nails are unchanged and the rest are hammered down to the length of the almost shortest.
For part 1, I reduced the number of steps by coming from both ends at once.
For part 2 I used 7 heads instead of 4. This allowed me to build layer 2 of extra pond by looking at the top of the pond directly. (~~~~~ in the top layer means digging in layer 2.)
For part 3 I used a version of the basis program.
Instead of going the full length of each line, I did each pond separately. This saved a lot of steps.
I do a small square, when I detect, that there’s digging to do. In part 2 I add a character to mark the end of line 2, and I go right first, then left.
Part 3 is done in a different way. I wasn’t quite sure how to do this, and then I saw someone mention recursion. Oh yes! It requires going right initially, and when I go down always checking to the left.
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.
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?
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.
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:
So now we have a definition of the line representing the wave:
The wave on the other side of the island goes through (0,-1) and has the same slope.
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:
And now we can describe the line segment:
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.
Changing this to the standard form:
And for the bottom point:
And now we can calculate the distance between the points:
Playing around with the original angle, it’s possible to find a max for d. This happens at:
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.
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:
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.
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.
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.