❔ ✅ is it possible to ''lock'' a var after the first assignment?
I'm making some base practice with C# with some exercises.
I completed the first one, and it have a second part that ask an additional answer.
Here's the code:
I completed the first one, and it have a second part that ask an additional answer.
Here's the code:
/*
Part 1
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.
*/
/*
Part 2
Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
*/
using System.Text;
namespace AOC_2015_1
{
internal class DayOnePartTwo
{
static void Main(string[] args)
{
string? input;
Console.WriteLine("Where should I delivery the next gift?");
Console.SetIn(new StreamReader(Console.OpenStandardInput(), Encoding.UTF8, false, 8192));
input = Console.ReadLine();
Console.WriteLine($"Final floor is {GiftDelivery(input)}");
}
static short GiftDelivery(string input)
{
short floor = 0;
const char floorUpChar = '(';
const char floorDownChar = ')';
int i = 0;
int x = 0;
foreach (char c in input)
{
if (floor == -1)
{
x = i;
Console.WriteLine("is " + x);
}
if (c == floorUpChar)
{
floor++;
i++;
}
else if (c == floorDownChar)
{
floor--;
i++;
}
}
return floor;
}
}
}/*
Part 1
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.
*/
/*
Part 2
Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
*/
using System.Text;
namespace AOC_2015_1
{
internal class DayOnePartTwo
{
static void Main(string[] args)
{
string? input;
Console.WriteLine("Where should I delivery the next gift?");
Console.SetIn(new StreamReader(Console.OpenStandardInput(), Encoding.UTF8, false, 8192));
input = Console.ReadLine();
Console.WriteLine($"Final floor is {GiftDelivery(input)}");
}
static short GiftDelivery(string input)
{
short floor = 0;
const char floorUpChar = '(';
const char floorDownChar = ')';
int i = 0;
int x = 0;
foreach (char c in input)
{
if (floor == -1)
{
x = i;
Console.WriteLine("is " + x);
}
if (c == floorUpChar)
{
floor++;
i++;
}
else if (c == floorDownChar)
{
floor--;
i++;
}
}
return floor;
}
}
}