Editorial for LCC '24 Contest 3 J3 - Fish++


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: AndyMan68

This problem was designed purely to test implementation. This editorial will go through some tips, but if you're curious to see what the intended solution looked like, a C++ and Python solution is also present.

Although there are many ways to implement a solution (which can range from concise to incredibly long), there are a few key tips to reduce implementation:

  • Store variables as strings

    • If a variable's integer form ever needs to be accessed, simply cast it temporarily
    • This makes the convert function pretty simple to deal with
  • Use a getter and setter method

    • The getter and setter method should try to match the string token with either a, b, or c
    • If the string is matched with a variable, the getter method will just return the variable's value, otherwise it just returns the string
    • If none of them match for the getter method, then just return the string itself
    • Call the getter method every single time you have something that could potentially be a variable
    • Call the setter method any time any variable needs to be set, as the setter also matches the string token to the variable
  • Read in each line as a whole

    • Simply just reading in each line (which conveniently always ends with a semicolon) makes it easier to process each line
    • For each line, simply keep appending the next token of the line to a list/Arraylist/vector until a semicolon is reached
    • Just by looking at the length or placement of certain tokens of the list/Arraylist/vector of strings representing the operation, you can tell almost all types of operations apart
    • Once the type of operation is identified, accessing individual tokens can just be done by accessing an index, which can be processed through a getter method to check if it's a variable

Each line can be handled with about 1 line each, with use of getter and setter methods as such:

Python 3

# intended solution
import sys
def getVar(char):
    if char == 'a': return a
    if char == 'b': return b
    if char == 'c': return c
    return char

def setVar(char, val):
    global a, b, c
    if char == 'a': a = val
    if char == 'b': b = val
    if char == 'c': c = val


line = input()
a, b, c = 0, 0, 0
if line != "include fish.io ;":
    print("Exception in program: no such method \"fish.print\" is found")
    sys.exit()
while line[0] != "abyss":
    line = input().split()
    if line[1] == "=":
        if len(line) == 6: setVar(line[0], int(getVar(line[2])) + int(getVar(line[4])))
        else: setVar(line[0], getVar(line[2]))
    elif line[0] == "fish.print": print(getVar(line[1]))
    elif line[0] == "convert": setVar(line[1], getVar(line[5]))

C++

// intended solution
#include <bits/stdc++.h>
using namespace std;
string a = "0";
string b = "0";
string c = "0";
vector<string> line;

string getVar(string s){
    if (s == "a"){return a;}
    if (s == "b"){return b;}
    if (s == "c"){return c;}
    return s;
}

void setVar(string s, string newVal){
    if (s == "a"){a = newVal;}
    if (s == "b"){b = newVal;}
    if (s == "c"){c = newVal;}
}

void readline(){
    line.clear();
    string token = "";
    while (token != ";"){cin >> token; line.push_back(token);};
}

int main(){

    readline();
    if (line[0] != "include"){ cout << "Exception in program: no such method \"fish.print\" is found"; return 0;}
    while (line[0] != "abyss"){
        readline();
        if (line[1] == "="){
            if (line.size() == 6){setVar(line[0], to_string(stoi(getVar(line[2])) + stoi(getVar(line[4]))));}
            else {setVar(line[0], getVar(line[2]));}
        }
        else if (line[0] == "fish.print"){cout << getVar(line[1]) << "\n";}
        else if (line[0] == "convert"){setVar(line[1], getVar(line[5]));}
    }


    return 0;

}

Comments

There are no comments at the moment.