[Community Puzzle] What is your garden worth?

WHAT :blossom: IS :tulip: YOUR :cherry_blossom: GARDEN :bouquet: WORTH?

Send your feedback or ask for help here!

1 Like

I’ve noticed a strange behaviour with javascript.
With a testcase like this :

1
$3 = 🍁
10
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁
🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁🍁

If i just print the row number and the gardenRow in the console.
With an input gardenHeight of 10 it prints :

0 ??????????????????????
1 ??????????????????????
2 ??????????????????????
3 ??????????????????????
4 ??????????????????????
5 ??????????????????????
6 ??????????????????????
7 ??????????????????????
8 ??????????????????????
9 ??????????????????????

But with an input gardenHeight of 8 (for example) there’s a bug :

0 ??????????????????????
1 ??????????????????????
2 ??????????????????????
3 ??????????????????????
4 ??????????????????????
5 ????????οΏ½οΏ½οΏ½οΏ½????????????
6 ??????????????????????
7 ??????????????????????

The same kind of bugs appears for the big garden test cases.
With java there’s no bug.

Javascript is a dynamic and weak typing language. When faced with a stream of data, without further guidance from the programmer, js will try its best to guess the data type and convert data into Numbers, Strings or some other odd data (NaN?).

Most of the time it collects 2 bytes from input and displays it as a character in a string. When the console does not have the graphic representation of that 2-byte character it displays ?

However, an emoji character has a length of 4 bytes. Your program was trying to cut an emoji into halves to display it as two characters. Some of the half-emoji bytes could have been misinterpreted by js and produced the odd output.

Java is more consistent because it is strongly-typed.

But either way, breaking a 4-byte char into two parts to display is meaningless in js or java or any other languages. It is a programming error more than a language error.

Though what I said above, breaking a 4-byte character into 4 bytes to calculate is meaningful.

You do not need to see the displayed char. You just need to know its numeric value to process.

The display problem is caused by a different numeric value. Using the code :

    s = ""
    for (c of gardenRow) {
        s += c.codePointAt(0) + "-"      
    }
    console.error(i + " " + s)

With an input gardenHeight of 10 it prints :

0 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
1 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
2 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
3 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
4 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
5 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
6 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
7 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
8 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
9 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-

With an input gardenHeight of 8 it prints :

0 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
1 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
2 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
3 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
4 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
5 127809-127809-127809-127809-65533-65533-65533-65533-127809-127809-127809-127809-
127809-127809-
6 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-
7 127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-127809-

My guess is it’s due to the readline implementation (which isn’t standard in JS). It looks like it must be reading in blocks and not handling cases where the block ends in the middle of a unicode character - you can get different numbers of invalid characters by adding more characters before the big unicode block which seems to be down to the alignment of the block start with the unicode characters. When the height has 2 digits (or 6) the alignment is correct.

3 Likes

Just checking the JS readline() CG is using, it seems that you are right.

PS: this readline() implementation on the CG runner image can be found at /codemachine/lib/javascript/internal/readline.js

IIRC, this is precisely the reason why puzzles are often (always?) ASCII-only…

I have the same problem, it is not working with some tests…So can we do it in javascript ? A solution someone?

1 Like

Maybe with replacing CG’s readline() implementation with a custom but fully working one.
It was userland code anyway…