Jump to content

How to read this file? c++

MisterWhite

so it's from day 6 of "advent of code" and i came across an issue of reading the file. So i have three different versions:

toggle 461,550 through 564,900turn off 370,39 through 425,839turn on 952,417 through 954,845

if it would be just "toggle" or "turn off" or "turn on" I'd be no problem but how do i read when there are 3 kinds?

I tried fd.get(char1;8); (8 because of the length of "turn of")but it cuts of one number from the first eg.

Ideas?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

How i did it: Since we know there are only three commands, you hardcode them and check with which one the line starts and then remove it from the beginning. Then split the string on " through ", you end up with two comma separated numbers, then split those two on the comma, volia you got the coordinates. Then it's whatever you choose, iterate with them over a 2d array or smth. I personally just put the values in a dictionary with an object of coordinates as the key, this does not bound me to the 1000x1000 grid, but the insertion is slow, I should have used a hash set or something, but it did come in handy later, as I did not have to change much for part 2. Also note that it might be harder to do this way if you are conding with say - C.

Link to comment
Share on other sites

Link to post
Share on other sites

How i did it: Since we know there are only three commands, you hardcode them and check with which one the line starts and then remove it from the beginning. Then split the string on " through ", you end up with two comma separated numbers, then split those two on the comma, volia you got the coordinates. Then it's whatever you choose, iterate with them over a 2d array or smth. I personally just put the values in a dictionary with an object of coordinates as the key, this does not bound me to the 1000x1000 grid, but the insertion is slow, I should have used a hash set or something, but it did come in handy later, as I did not have to change much for part 2. Also note that it might be harder to do this way if you are conding with say - C.

the first part is my struggle (toggle, turn off, turn on), else is not a prob. I just dunno how to. i mean i could do something like this and merge the two:

while(!fd.eof()){    fd>>myString;    if(myString=="turn");    {        fd>>SecondString>>other_input;    }    else    {        fd>>other_input;    }    //Afterwards merge the SecoondString with myString}

but, isn't there a more pretty way?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×