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
uselessxp2y 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
uselessxp2y 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
uselessxp2y ago
the only explanation is that the whole function is called more times
ero
ero2y ago
like where that method is being called?
uselessxp
uselessxp2y 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
uselessxp2y 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
uselessxp2y ago
yeah I like this
uselessxp
uselessxp2y ago
one of the first modifies I made to the code consist in this and I didn't want to reverrse it
uselessxp
uselessxp2y ago
is var better than short ?
Angius
Angius2y ago
var is for type inference
uselessxp
uselessxp2y 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
uselessxp2y 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
uselessxp2y 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
uselessxp2y ago
and can't a tuple have a defined type?
uselessxp
uselessxp2y 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
uselessxp2y 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
uselessxp2y ago
interesting
ero
ero2y ago
or (short a, short b) = ...
uselessxp
uselessxp2y 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
uselessxp2y 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
uselessxp2y 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
uselessxp2y 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
uselessxp2y 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
uselessxp2y 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
More Posts
❔ VS 2022 debugger ArgumentNullException not showing ParamName in quick info tooltipThis is mostly a QoL problem. I'm not sure if it's a bug or a problem with my configuration. Visua❔ Problem CanUseCmdhttps://media.discordapp.net/attachments/169726586931773440/1064902725231460443/image.png❔ Need Help with httpclient Async stream readingI do not have much experience with reading data from an stream endpoint (I am sure there is better n❔ Is there a version of matrix of which sqlpackage.exe goes with which version of sql server?Looking for a link that can tell me which goes with which because I need to find sqlpackage.exe for ❔ XML serialization - internal XML elementHow can I get rid of the internal XML of the Value element? Should I add some attribute on field in ❔ Rx.NET issueThis is a minimal reproducible example of request/response model (like HTTP) but over websockets. ❔ trying to detect when the enter key is pressedusing ReadKey() but i dont know what the Enter key is to put in here❔ How to disable an Exception Filter for a specific Action in ASP.NET Core?Hi dear friends, I hope you doing well, I'm working on an `ASP.NET Core 7` project. I created the fo❔ ✅ 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 ❔ EF inheritance, selecting from multiple childrenSo I have this entity ProcessSpec, and 2 classes inherit from it. I'd like to be able to select a li