C
C#2y ago
uselessxp

✅ mistery -foreach cycle executed three times for no apparently reason

Here is the code. I checked more and more times for errors, I always putted some breakpoint in order to see into variables, and I just seeing that when the cycle arrive at the last char contained in input (that's a string), it just rest all index to 0 and start again for a total of 3 executions. Am I committing error somewhere? 🧐
41 Replies
ero
ero2y ago
that's not possible
uselessxp
uselessxpOP2y ago
yeah I know, usually stupid errors are committing by beginners in cycle statement, but in this case I'm really going crazy, I'm pretty sure all it's ok
uselessxp
uselessxpOP2y ago
it's like it re-execute those rows again, out of the cycle 🧐
ero
ero2y ago
can you show more of your code?
uselessxp
uselessxpOP2y ago
the only explanation is that the whole function is called more times
ero
ero2y ago
like where that method is being called?
uselessxp
uselessxpOP2y ago
I think it's so <:picard_facepalm:616692703685509130>
using System.Collections;
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();
if (GiftDelivery(input).Item2 == 1337) Console.WriteLine($"Final floor is {GiftDelivery(input).Item1} and Santa never entered the basement");
else Console.WriteLine($"Final floor is {GiftDelivery(input).Item1} and the first time when Santa enter the basement is {GiftDelivery(input).Item2}");
}

static (short, short) GiftDelivery(string input)
{
short floor = 0;
const char floorUpChar = '(';
const char floorDownChar = ')';
short x = 0; //for the cycle
short y = 0; //for the basement check
List<short> basement = new();

foreach (char c in input)
{
if (floor == -1)
{
basement.Add(y);
Console.WriteLine("asd " + basement[x]);
x++;
}
if (c == floorUpChar)
{
floor++;
y++;
}
else if (c == floorDownChar)
{
floor--;
y++;
}
}

if (basement.Count == 0) return (floor, 1337);
else return (floor, basement[0]);
}
}
}
using System.Collections;
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();
if (GiftDelivery(input).Item2 == 1337) Console.WriteLine($"Final floor is {GiftDelivery(input).Item1} and Santa never entered the basement");
else Console.WriteLine($"Final floor is {GiftDelivery(input).Item1} and the first time when Santa enter the basement is {GiftDelivery(input).Item2}");
}

static (short, short) GiftDelivery(string input)
{
short floor = 0;
const char floorUpChar = '(';
const char floorDownChar = ')';
short x = 0; //for the cycle
short y = 0; //for the basement check
List<short> basement = new();

foreach (char c in input)
{
if (floor == -1)
{
basement.Add(y);
Console.WriteLine("asd " + basement[x]);
x++;
}
if (c == floorUpChar)
{
floor++;
y++;
}
else if (c == floorDownChar)
{
floor--;
y++;
}
}

if (basement.Count == 0) return (floor, 1337);
else return (floor, basement[0]);
}
}
}
ero
ero2y ago
you literally call that method 3 times yourself
uselessxp
uselessxpOP2y ago
damn
ero
ero2y ago
4 times even Console.SetIn(new StreamReader(Console.OpenStandardInput(), Encoding.UTF8, false, 8192)); is absolutely unnecessary (and a memory leak)
Angius
Angius2y ago
Call it once, save the result in the variable, reference that variable Since you're using an unnamed tuple, you could even use tuple reconstruction to make everything nice and named
var (foo, bar) = GiftDelivery(...);
var (foo, bar) = GiftDelivery(...);
and .Item1 will be in foo and .Item2 will be in bar
uselessxp
uselessxpOP2y ago
yeah I like this
uselessxp
uselessxpOP2y ago
one of the first modifies I made to the code consist in this and I didn't want to reverrse it
uselessxp
uselessxpOP2y ago
is var better than short ?
Angius
Angius2y ago
var is for type inference
uselessxp
uselessxpOP2y ago
oh ok I have to use it mandatory for avoid calling twice the method
Angius
Angius2y ago
It is short if you assign a short to it
uselessxp
uselessxpOP2y ago
yeah but I also noticed that
var (item1, item2) = GiftDelivery(input);
var (item1, item2) = GiftDelivery(input);
does not works with short also if I use short everywhere
Angius
Angius2y ago
Wym "doesn't work"?
uselessxp
uselessxpOP2y ago
what do you think about converting rows like this:
input = Console.ReadLine();
var (item1, item2) = GiftDelivery(input);
input = Console.ReadLine();
var (item1, item2) = GiftDelivery(input);
to this:
var (item1, item2) = GiftDelivery(Console.ReadLine());
var (item1, item2) = GiftDelivery(Console.ReadLine());
could it be a good practice or it's stupid? it doesn't seems to accept it, now I try again
Angius
Angius2y ago
static (short, short) GiftDelivery(string input)
static (short, short) GiftDelivery(string input)
the method has to actually return a tuple
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
(short, short) Foo() {
return (6, 8);
}

var (a, b) = Foo();
Console.WriteLine($"a is {a} and b is {b}");
(short, short) Foo() {
return (6, 8);
}

var (a, b) = Foo();
Console.WriteLine($"a is {a} and b is {b}");
Console Output
a is 6 and b is 8
a is 6 and b is 8
Compile: 672.899ms | Execution: 88.116ms | React with ❌ to remove this embed.
uselessxp
uselessxpOP2y ago
and can't a tuple have a defined type?
uselessxp
uselessxpOP2y ago
that's what I tried to do
Angius
Angius2y ago
No Because a tuple can contain a string and an int, for example Tuple deconstruction needs var
uselessxp
uselessxpOP2y ago
ok as I know, in DB a tuple is a row I'm reading that a tuple in C# can contains multiple values, isn't like an array so?
Angius
Angius2y ago
Kinda-sorta Something between an array and an object
uselessxp
uselessxpOP2y ago
interesting
ero
ero2y ago
or (short a, short b) = ...
uselessxp
uselessxpOP2y ago
that's make sense. but I realized I have to use var also if all variables are same type working in this way, but not working if I put short out, doesn't it understand that I want both short maybe
Angius
Angius2y ago
Show the code that doesn't work Because if you're using tuples, tuple deconstruction can't not work
uselessxp
uselessxpOP2y ago
(short item1, short item2) = GiftDelivery(Console.ReadLine()); //works
var (item1, item2) = GiftDelivery(Console.ReadLine()); //works
short (item1, item2) = GiftDelivery(Console.ReadLine()); //does not works
(short item1, short item2) = GiftDelivery(Console.ReadLine()); //works
var (item1, item2) = GiftDelivery(Console.ReadLine()); //works
short (item1, item2) = GiftDelivery(Console.ReadLine()); //does not works
Angius
Angius2y ago
Well, yeah Because it's not a short It's (short, short)
uselessxp
uselessxpOP2y ago
that's ok but why at this point not requiring
(var item1, var item2) = GiftDelivery(Console.ReadLine());
(var item1, var item2) = GiftDelivery(Console.ReadLine());
Angius
Angius2y ago
Sure, you can But why (var foo, var bar) if you can var (foo, bar)
uselessxp
uselessxpOP2y ago
I don't und why the compiler in this case understand that both are var, and it doesn't when I put short
Angius
Angius2y ago
var is not a type var is "go figure out what type it is" It doesn't understand that "both are var"
uselessxp
uselessxpOP2y ago
ok, but is intended to take the place of a type
Angius
Angius2y ago
It understands that it should figure out the types I guess you could say var is special in this case. If you have (string, int, bool) you can't deconstruct it to a variable of any concrete type You need var (a, b, c) or (string a, int b, bool c) or (var a, var b, var c) Sure, in your case all elements of the tuple are of the same type But handling this special case is just not worth it So the compiler wasn't made to handle it Since if you want explicit types you can do (T1 a, T2 b) and if you want the compiler to figure it out you use var (a ,b)
uselessxp
uselessxpOP2y ago
wow when I thought to have learned all bases 🤣 I imagine it was an hard work to write this compiler, or a compiler in general
Angius
Angius2y ago
Probably, yeah We have a few people in the server who are on C#'s compiler team, actually Might ask them about it if you're curious, #roslyn sounds like it might be a good place
Want results from more Discord servers?
Add your server