Code of the Rings - Optimization - Puzzle discussion

Please read the discussion above - there is explanation with examples.

Can I ask how to reduce number of chars to only 6.5k ?
My number of chars is more than 11k

This is also covered in the discussion above.

hi,
I don’t understand my problem.
I even tried to cut the loop in 2 parts thinking that there was an icrement of ‘i’ going wrong and I have the same result. On some tests I pass all 31 letters and on others I don’t.
I don’t see where it’s wrong.
if anyone can help me, thx


Bonjour,
J’ai un souci pour passer certains tests, surtout aux alentours de la 30eme lettre (Ă©videmment).
Certains tests passent, d’autres non.
j’ai essayĂ© en dĂ©coupant mon programme en pensant que c’était un problĂšme avec une boucle for mais ça me fait la mĂȘme chose. j’ai placĂ© des espions qui ,il me semble, me donne les bonnes valeurs que j’attends, mais le rĂ©sultat
bref
si quelqu’un peut m’aider a comprendre, ce serait sympa. Merci


class Player
{
    static void Main(string[] args)
    {
        /// init variables
string result = null;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
List<int> indexMot = new List<int>();
/// inputs
string magicPhrase = Console.ReadLine();
/// pre conditions
foreach (var item in magicPhrase)
{
    indexMot.Add(alpha.IndexOf(item) + 1);   // for 'A'=1, 'Z'=26, ' '=27
}
//Console.Error.WriteLine(magicPhrase[28]+" "+indexMot[28]);
//// conditions
for (int i = 0; i < indexMot.Count; i++)
{
    if (i<=29)
    {
        if (indexMot[i]<13)
        {
            for (int j = 0; j < indexMot[i]; j++)
            {
                result += "+";
            }
        }else
        {
            for (int j = 0; j < 27-indexMot[i]; j++)
            {
                result += "-";
            }
        }

    result += ".>";    
    }
    
}
for (int i = 0; i < indexMot.Count; i++)
{
    if (i>29)
    {
        //Console.Error.WriteLine(magicPhrase[1]+" "+indexMot[1]+" "+magicPhrase[30]+" "+indexMot[30]);
        if (indexMot[i]-indexMot[i-30]>0)
        {
            for (int j = 0; j < indexMot[i-30]-indexMot[i]; j++)
            {
            result += "+";
            }
        }
        if (indexMot[i]-indexMot[i-30]<0)
        {
            for (int j = 0; j <27-indexMot[i-30]+indexMot[i]; j++)
            {
            result += "+";
            }
        }
    result += ".>";
    }
}

J’ai trouvĂ©, il faut changer de rune le moins possible.
je suis OK jusqu’au test 23 maintenant. :thinking:

I used a similar approach but got 1368th. I will try a proper loop approach when I have some time.

Hi,
I have a problem with this puzzle.
In my code, I have a loop with [>] to find the next space rune. I tried several solutions (having a try, an if and even a while there is no space, create space) to avoid having an infinite loop when you have no spaces on your runes.
However, whatever I tried, I end up in an infinite loop, and I can’t understand why.
Here is a link to the replay: Coding Games and Programming Challenges to Code Better
and here is my code since I didn’t find a way to attach it in antoher way.

Thanks in advance for your help!

import sys

import math

import string

import pandas as pd

import numpy as np

Auto-generated code below aims at helping you parse

the standard input according to the problem statement.

magic_phrase = input()

print(f"{magic_phrase}", file=sys.stderr, flush=True)

print(f"{len(magic_phrase)}", file=sys.stderr, flush=True)

Write an action using print

To debug: print(“Debug messages
”, file=sys.stderr, flush=True)

Initialisations

Variables

rune_df = pd.DataFrame(index=range(30), columns=[‘letter’])

rune_df[‘letter’] = ’ ’

answer = ‘’

alphabet = list(string.ascii_uppercase)

alphabet.insert(0, ’ ')

alphabet_df = pd.DataFrame(columns=[‘letter’])

alphabet_df[‘letter’] = alphabet

position = 0

Functions

def FasterToGo(letter, rune_letter):

time_to_write = abs(letter.position - rune_letter.position)

letter_positions = rune_df[rune_df['letter'] == letter.name].index.tolist()

if len(letter_positions) > 3:

    return True, letter_positions[0] - position

else:

    possible_times = [x-position for x in letter_positions]

    time_to_go = np.min(possible_times)

    if abs(time_to_go) <= time_to_write : return True, time_to_go

    else : return False, time_to_go

def letter_position(letter):

    return alphabet_df[alphabet_df['letter'] == letter].index[0]

Class

class _letter:

def __init__(self, name):

    self.name = name

    self.position = letter_position(self.name)

Getting instructions

for character in magic_phrase:

letter = _letter(character)

rune_letter = _letter(rune_df.loc[position, 'letter'])

free_rune = _letter(' ')

#print(f"standing on {position, rune_letter.name}, looking for {letter.name}", file=sys.stderr, flush=True)

# If we already activate a rune with the letter

if letter.name in rune_df['letter'].unique().tolist():

    # Find if it's better to write a new rune or go an old one

    isFaster, path = FasterToGo(letter, rune_letter)

    if isFaster:

        # Find where it is and go there in the shortest path

        if path > 0:

            for i in range(path):

                answer += '>'

                position += 1

        elif path < 0:

            for i in range(abs(path)):

                answer += '<'

                position -= 1

        #print(f"Moving {path} on {position}", file=sys.stderr, flush=True)

        # Then activate the rune

        answer += '.'

    else:

        # Inscribe the letter in place of the old one

        #print(f"writing over", file=sys.stderr, flush=True)

        letter_diff = letter.position - rune_letter.position

        if letter_diff > 13:

            letter_diff = letter_diff - 27

        if letter_diff > 0:

            for i in range(letter_diff):

                answer += '+'

        else:

            for i in range(abs(letter_diff)):

                answer += '-'

        answer += '.'

        rune_df.loc[position, 'letter'] = letter.name

# If the letter has not been written yet

else:

    # Go where we have a free rune

    if len(rune_df[rune_df['letter'] == ' ']) > 0:

        position = rune_df[rune_df['letter'] == ' '].index[0]

        answer += '[>]'

    #print(f"writing on new at position {position}", file=sys.stderr, flush=True)

    # Inscribe the letter in it

    letter_diff = letter.position - free_rune.position

    if letter_diff > 13:

        letter_diff = letter_diff - 27

    if letter_diff > 0:

        for i in range(letter_diff):

            answer += '+'

    if letter_diff < 0:

        for i in range(abs(letter_diff)):

            answer += '-'

    answer += '.'

    rune_df.loc[position, 'letter'] = letter.name

Answering

print(f"{len(answer)}", file=sys.stderr, flush=True)

print(f"{answer}")

Please use </> in the formatting toolbar to format your code properly in this forum.

Great puzzle.
Loops are not needed to get under 6500.

I get under 6500 without loops.

Salut, je comprend pas, j’ai essayĂ© toute les lettres possibles entre l’alphabet pour l’“alphabet sĂ©parĂ© par 1 lettre” et tout fonctionne bien.

Mais quand je soumet ma rĂ©ponse ça ne fonctionne pas au validateur numĂ©ro 16 “Entire alphabet x11 separated by one letter”

Ca marche avec mon vieux code sans boucle mais ca prend 600 caractĂšre et lĂ  une trentaine avec les boucles.
Vous avez une idée du soucis ?
en gros je fait >+++++++++++[<+[.+]+.[+]>-]


Hi, I don’t understand, I tried all possible letters between the alphabet for the “alphabet separated by 1 letter” and everything works fine in the IDE.

But when I submit my answer it doesn’t work at validator number 16 “Entire alphabet x11 separated by one letter”

It works with my old code without loops but it takes 600 characters and there about thirty with the loops.
Do you have an idea of the problem?
basically I do >+++++++++++[<+[.+]+.[+]>-]

The test and the validator are not the same, to avoid hardcoding. It is the alphabet with a letter 
 but maybe not the same letter.

I know the validators are different but I tried several combinations.
Alphabet+letter+alphabet+


By writing I think with find why it does not work, my code takes into account only 1 letter, the same everywhere but it must not be a single letter which separates in the validator.

Thanks for the help :slight_smile:

EDIT :that’s not it, I stop for today !!

No idea ?
I break down my output to explain my approach:
-counter >++++++++++++
">" + * number of full alphabet found
-loop [<+[.+]+.[+]>-]
"<" return to the starting rune
"+[.+]" enumerates the alphabet (when found in the sentence)
"+." if letter found between the alphabets (here A in the IDE, if it was B it would be “++.”, etc)
"[+]" resets the rune to space
">-" decrements the counter

With my code everything works in the IDE even when I try with another letter or different letters between the alphabets with a letter or not at the end, at the beginning, etc.

I hope you have understood my problem better, do you have any idea where my problem comes from?


Aucune idée ?
je décompose ma sortie pour vous expliquer ma démarche:
-compteur >+++++++++++
">" + * nombre d’alphabet complet trouvĂ©
-boucle [<+[.+]+.[+]>-]
"<" reviens sur la rune de départ
"+[.+]" Ă©numĂšre l’alphabet (quand trouvĂ© dans la phrase)
"+." si lettre trouvĂ© entre les alphabets (ici A dans l’IDE, si c’était B ça serait “++.”, etc)
"[+]" remet la rune sur espace
">-" décrémente le compteur

Avec mon code tout marche dans l’IDE mĂȘme quand j’essai avec une autre lettre ou des lettres diffĂ©rentes entre les alphabets avec une lettre ou non Ă  la fin, au dĂ©but, etc

J’espĂšre que vous avez mieux compris mon souci, si vous avez une idĂ©e d’ou viens mon problĂšme ?

While trying to understand using the loops I implemented a check that replaces the output with a shortened loop version. However, this does not bring down my instruction count. Am I misunderstanding something?

Example:

        #two letter word x20
        if self.path == '-------------.>------------.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.<.>.':
            self.path = '------------->------------>-------[<<.>.>-]'

Hello, i’m trying to resolve this (anwsome) puzzle, with less than 6500 letters (i’m a beginner) but i block on the last test
 i have a basic answer with 3k instructions which works and another “optimised” with 2200 instructions that doesn’t works, because of "too many instruction (infinity loop ?) so i thought that my code had an error, however, when i only print the instructions ( the brut instruction, without calling my function) i have the same message, but i don’t understand why
 I would be glad to give you any information if needed and hope you’ll have a nice day ^^

The statement says:

You lose if:
Blub performs 4000 moves or more in the forest.

Maybe your 2200 instructions cause Blub to perform 4000 moves or more?