A bit of history

Before starting, I think it’s important to go back a little in time to explain why I’m taking on this challenge.

Throughout all my years in the IT field, I never seriously dedicated myself to learning a programming language. I learned a bit of Pascal, C++, and Visual Basic in school, which gave me a basic understanding of programming, but never sparked my interest in learning more, and to be honest, I don’t even remember anything! Maybe because school projects were boring and never motivated me to learn more, or maybe because I was more interested in hardware than software development. A few years later, when I started focusing more on Linux, I learned Bash, which has been quite useful for automating various tasks, including creating several tools for my team in our daily activities in production environments.

However, I’ve been having more and more ideas for different projects (some more foolish than others), and the need to learn a programming language arose! I started learning Python, and my interest grew. I did some small and fun projects, but decided to slow down, change course, and learn Go! Why? Simply because! All the tools I work with daily are written in Go, and after some time thinking, I consider Go to be the most suitable language for the projects I have in mind. Maybe I’m wrong, but… as my grandfather used to say, “Knowledge doesn’t take up space!”

After several days studying the basics of Go, I decided to start solving the problems of Advent of Code and started with the year 2015, which brings us to today’s topic and discovery!

The problem

You can learn more about the challenge here. But, in a very brief summary, the problem consists of calculating the floor of an elevator based on a series of parentheses that indicate if the elevator goes up or down. For example, if the elevator starts on floor 0 and the input is ((( or (()(()(, the elevator will go up to floor 3. If the input is ()), the elevator will go down to floor -1.

Ooh! This is easy, I thought in my innocence!

First, a function called elevator that receives the instructions and returns the floor where the elevator ends up.

func elevator(instructions string) int{
	var floor int
	for _, instruction := range instructions {
		if instruction == '(' {
			floor++
		} else if instruction == ')' {
			floor--
		}
	}

	return  floor
}

And then I just have to ask “Santa Claus” to input the instructions, print the result and it’s done!

func main() {
	var instructions string
	fmt.Print("What floor you want to go?: " )
	fmt.Scan(&instructions)

	lastFloor := elevator(instructions)
	fmt.Println("final Floor: ", lastFloor)
}

I introduce the instructions and, whoa! I’m a machine! Everything works perfectly! (())) returns 3 and ()) returns -1.

I take the challenge input and… Wrong!!! For more experienced programmers, this might be quite obvious, but for little innocent me, it was enough to spend the afternoon banging my head against the wall.

First, I focused on the fact that the result was wrong and decided to add an extra print, hoping to understand what was going on.

func main() {
	var instructions string
	fmt.Print("What floor you want to go?: " )
	fmt.Scan(&instructions)
  fmt.Println("instructions: ", instructions)

	lastFloor := elevator(instructions)
	fmt.Println("final Floor: ", lastFloor)
}

Hey! The output of the instructions is not the same as the input! What’s happening here? And if I read from a text file? Will I have the same problem? I changed the code to read from a text file:

func main() {
	instructions, err := os.ReadFile("input.txt")
	if err != nil {
		panic(err)
	}

	floor := elevator(string(instructions))
	fmt.Println("Final floor:", floor)

This time, the result was different. I entered the result on the website and… Correct! But why? What’s going on here? I got the correct answer for the exercise, but I still couldn’t understand what was happening. So, I decided to compare the result of the input made by the user and the input read from the text file.

My first idea was to check if the strings had the same length and compare the result of both.

func main() {
	var instructionsFromInput string
	fmt.Print("What floor you want to go?: " )
	fmt.Scan(&instructionsFromInput)
	fmt.Println("instructions: ", instructionsFromInput)
	fmt.Println("Length from input: ", len(instructionsFromInput))

	instructionsFromFile, err := os.ReadFile("../01/input.txt")
	if err != nil {
		panic(err)
	}
	fmt.Println("Lenght from file: ", len(instructionsFromFile))
	lastFloorFromInput := elevator(instructionsFromInput)
	lastFloorFromFile := elevator(string(instructionsFromFile))
	fmt.Println("Final Floor From input: ", lastFloorFromInput)
	fmt.Println("Final Floor from file: ", lastFloorFromFile)
}

EUREKA! I’m a noob! Or am I not?

$go run main.go
What floor you want to go?: (((())))()((((((((())()(()))(()((((()(()(((()((()((()(()()()()()))(((()(()((((((((((())(()()((())()(((())))()(()(()((()(()))(()()()()((()((()(((()()(((((((()()())()((((()()(((((()(())()(())((())()()))()(((((((())(()())(()(((())(()))((())))(()((()())))()())((((())))(()(((((()(())(((()()((()((()((((((((((())(()())))))()))())()()((((()()()()()()((((((())())(((()())()((()()(((()()()))(((((()))(((()(()()()(()(()(((())()))(()(((()((())()(()())())))((()()()(()()(((()))(((()((((()(((((()()(()())((()())())(()((((((()(()()))((((()))))())((())()()((()(()))))((((((((()))(()()(((())())(())()((()()()()((()((()((()()(((())))(()((())()((((((((()((()(()()(((())())())))(())())))()((((()))))))())))()()))()())((()())()((()()()))(()()(((()(())((((())())((((((((()()()()())))()()()((((()()))))))()((((()(((()))(()()())))((()()(((()))()()())())(((())((()()(())()()()(((())))))()())((()))()))((())()()())()())()()(()))())))())()))(())((()(())))(()(())(()))))(()(())())(()(())(()(()))))((()())()))()((((()()))))())))()()())((())()((()()()))()(((()(()))))(())()()))(((()())))))))))(((())))()))())()))))()()(((())))))))()(()()(()))((()))))((())))((()((())))())))()()(()))())()(()((()())(()(()()())())(()()))()))))(()())()()))()()()()))(()(()(()))))))()(()))()))()()(()((())(()(())))()(((())(())())))))()(()(()))))()))(()()()(())()(()(())))()))))()()(((((())))))())()())())())()())()))))()))))))))())()()()()()()())))()))((())()))())))()((())()))))()))())))))))())()()()))()()(()((((()(((((((()(())((()())((()()))()))))(())))()()()(())((())()())))(())))(())))(((()()))()(())(((()(()))((())))())()))((((()))())()))))))))()(())())))(()))()(()()))())()()(())())))())()()(()())))()((()())(()(())(())))))))))))))(()))))()))))))()()())(()(((((()(()())))())()))(()))()))(()()))()())(()))())()(())((()()))))))())))())()(((())))(()(()))()()))()(()))))))((()())(()))))))()())))()()))))))))((((((((()()()(()))))))()())))())))()()((())()))((())(())))())())))()()()((()((()(())))())()(())))))))))()())))()()()()()()))()))((())())(()(()))))))(()()))()))(())))()))))))))))))(()))))))))()))))()))()())()))()()))))))()))))((()))))(()))())()(())))(()())((((()())))()))))(()))()(()()(())))))())))))()))))))())))())))))())))())())))())(()))))(())()(())))())()))((()()))))))())))((())))))))())))(())))))()()())))))())))))()))))))()))()()()(()(((()())())())(()))())))))((()(())(()))))))))(())))()()()())())(()))))()()()))()))())())())()(())))()(((()((((())))))))()))))))))))))))))))))((())()())(()))))()()))))))(()()(())())))())))((())))((())))))))))))))()))))()(()))))))())))))()))(()()())(()())))))))))()))))))(())))))()()))()())(((())))()))(()))))))))(())())))())))())())())()()))((())()(())()())()))()())(())(()))))()())))(()(((()))))))()(()())()()()))()))))))))()()()(())()())()(((((()))()())())(()))))()()()(())))())))()((()())))(()))())()(()())())(()))()()))((()()))((()()()()())))(())()))(()(())))((()()))))))))())))))))())()()))))))))))))))))(())()(())(())()())())()))()(()))))())())))))()())()(()))()()(())))(())())))))(()))))))))))))))())())(())(())))(((()))()))))())((())(()))())))))))())))))())))()))()))))))))))))())()))))()))))((()))(())))()(())))(())()))()))())))())))))))()(()())())))()()())))(())))))(()))))))))))))(()))()))()))())))(((()()()(())((()())))()())(((()))(())()))((()()()())))())(())(()))))()(((((())))(()))())())))))))((((()()()))())())()(()(()())))))))))()())())))(())))()())(((()(())())()()))())())))))))((()())((()()(()))(()(())))()))()))(()))(()))()()(()(((())((((()))()(()))((())()(()(()())()(()))()())))))(()))()))())()())))())))(())))((())(()())))))()))(())(()))()())()(()()((()(()))))))()(())(()())(())()))(((())()))(()()(()()()))))(()(())))()))))())))))())(()()()()()()(((())))(()()))()((())(((((()()())))(()))(()))()()))(((())())()(((()()()()))))(()))(())())))()())(()()())())))))))()))))((())))()())(()))(()(()))())))))())(())))))()()())())()))()()(())))(()))(())((((((())(()))(()))())()))(()()(())))()))(()()))()))()(())))(())))((()(()))(())()()())())))(((()()())(())()))))))()(((()(((((()()(((())(())))())()((()))))((()())()(())(((())))(((()((()(()(()))(()()))())(()))(())(())))()))))))((((()))()((((()(()))()))()()))))()(()(()))()(()((()(((()(()()(((()))))()(((()(()(()(((()(()())())()()(()(()())())(()((((())(()))()))(((((()()())(())()((()()())))()()(((()()))()((((((((()(())))())((()))))(())))(()))))((()((((()()(())(((((()))(((((((((((((()())))((((()(((()((())())()))((()))()(()()((()()()()(()()(()(()(((())()(()((((((()((()()((())()((((()((()()(()()())((()()()((()((())()(()(((()((())((((())(()))((()(()))(()())()((((((((()(((((((((((()))(()(((()(()()()((((())((())()())()))(())((())(()))(((()((()(())))(()))))((()()))))((((()(()(()())(()(())((((((((()((((()((()(((((()))())()(()))(()()((()(())(((((()(())()(((((()()))))))()(((())()(()()((((())()((())((()(((())(((()))((()()((((()(())))))((()((((()((()((()(((())((()))(((((((()(((()((((((((())()))((((())(((((()((((((((()(((()((()(((()()(((()((((((()()(()((((((((()()(()(()(())((((()())()))))(((()))((((())((((()())((()(())()((()((((((()((((((()(())))()())(((())())())()(())()(()())((()()((((())((((((())(()(((((()((((())()((((()(()(())(()())(((())()((())((((()))()((((((())(()(((()(((()((((((()(((()))(()()())())((()((()())()((((())(((()(()(((((((((())(())))()((()()()()(())((()))(((((((()(((((((((()(()))))(()((((((((()((((()((()()((((((()()(((((((()(()(())()(())((()()()((()(((((()())()(((((()())()()((()(()())(()()()(((()()(((((()((((((()()((()(()()()((((((((((((()((((((((()()(((()())))()(((()()(())())((((()((((()((((()()()(())(())((()(()(((((((((((((((()(())(())))))()()))((()(((()(())((()(((()(()()((((()()(((()(((()(((((()()((()(()(((()))((((((()((((((((()((()((())(((((()(((())(())())((()()))((((())()()((()(((()(((((()()(((()))(((()(()(((((((((((((()))((((((((()(((()))))())((((((((((((())((())((()())(((())((())(()((((((((((()(((())((()()(()((())(((((((((((()))((((((((((((()(()())((()((()((()(()(((()((((((((()()(()((()(()(((()))((()))(((((((((((((()(())((((((())(((()(())(()(()(()((()()))((((()((((()((((())))())((((()((((()))((((((()((((((()((()(((())))((())(()))(()((()((((()((()(((()()))((((()()()(((((((())(((())(()))())((((()())(((()(((((((((((()(()(()((()(((((((((((((((()()((((()((((((((()(((()()((()((((()))(((()(())((((((()((((())()((((()((()))(())()(()(((()((())())((((((()(()(())())(((())(()(()())(((((()((()((())()())(())))(((()(())))))))(((()(((()))()((()(((()()((()())()()))())))(((()))(()(((()(((((((((()(()(((((()()(((()())()()))))()(((()))(((()(()(()(()(()))()(())()))(()(((())))(()))))))))))(())((()((())((()(())()(())((()()((((()()((()()))((())(((()((()(())(())))()(()(((((()((()))())()(((((()()(((()(()((((((())(()))(())()))((()(()()))(())())()))(((())))(()((()(((())(())())))((()()((((((((((((((()((()(()()(()(((()))())()()((()()()(())(()))(()())(((())((())()(())()()(()()(())))((()(((()))))(((()()(()()))())((()((())()))((((()()()())((())))(((()(())(((((()(((((()((()(()((((()()(((()()()(((()())(((()()((((())(()))(((()))(())())((()))(((()((()))(((()()((())((()(((((()((((()()())((()))()((((()((()(()()()(

Length from input:  4095
Lenght from file:  7001
Final Floor From input:  -633
Final Floor from file:  232

Hmm… interesting. When I pass the elevator instructions through the input in the terminal, the result is different because some instructions are missing! Here everything started to make more sense and, after a little research, I found out that the terminal buffer has a limit of 4096 bytes, and everything beyond that is cut off.

Wow, what a big noob! Usually, when I know that the input is large, I always use a file, especially since I usually get the data from somewhere, save it to a file, and then read and process it. But in this case, since Santa Claus enters an elevator and places instructions for it, I thought it would be nicer to take the input from the website and pass it directly through the terminal, but no!

Part 2

Regarding part 2, it was fairly easy and all the code for this exercise is available on my GitHub repository.