OrbitalReign's career change

The Partridge Family were neither partridges nor a family. Discuss.
OrbitalReign
Posts: 19
Joined: July 17th, 2017, 1:24 pm

Re: OrbitalReign's career change

Post by OrbitalReign » September 3rd, 2017, 12:55 am

So now the solutions out to the code challenge i guess ill share my now apparent vain attempt.
i had to jump ahead in the tutorials to figure out how to load the file but definitely should have learnt the rest.
My code basically had to process each number for each path and then adjust its path using a binary path from the bottom up which seemed like a terrible approach and once i got it working it showed because it was taking forever to calculate.
something like 2.7 ^126 possible paths and the binary switching number would overload well before it got there so i tried a different approach by looking back over the pathing methods on red blob games but couldn't come up with a solution with what i know.
All in all though it was to to see how not to do it.
and props to the people who got it.
Spoiler:

Code: Select all

#include <fstream>
#include <bitset>
#include <minmax.h>
#include <iostream>



int main()
{
	int Row_num = 0;
	int Row_pos = 0; 
	int maxpath = 0;
	int maxcompare = 0;
	int end = 420; // calculate at compile <<<<<<<
	const int numarr = 2;
	char num[numarr]{ 0 };
	int numberPos = 1;
	int new_row_base = 1;

	std::bitset<500> binaryswitch;

	std::ifstream in("pyramid.txt");
	if (!in)
	{
		// do thing
		return -1;
	}
	int switching_num = 1;
	int bignumber = 1000000;
	char c;

	while (!binaryswitch.test(420))  // loops all paths
	{
	in.seekg(0, std::ios::beg);
	c = in.std::ios::beg;
	maxcompare = max(maxpath, maxcompare);
	Row_pos = 0;
	Row_num = 1;
    binaryswitch = switching_num++;  // increments after its previous number is stored
	new_row_base = 1;
	maxpath = 0;
	numberPos = 1;
	
	std::cout << maxcompare << " " << switching_num << '\r';
	
	

		while ( Row_num <= end )//loops one path
		{
			if (Row_num > 419)
			{
				int x =1;
			}
			Row_pos = end - Row_num; // how far row num is from the last row

			while (c < 48 || c > 57) // excludes non numbers
			{
				c = in.get();
			}

			if (numberPos == new_row_base) // gets the number from the right row position and row number
			{
				int nextnum = 0;
				int lastnum = 0;

				while (c > 47 && c < 58) //make sure its a number
				{
					num[nextnum] = c - 48;
					nextnum++;  // can overload array!!! <<<
					c = in.get();
					if (c > 47 && c < 58) // if next pos is also a number multiply the last number by 10
					{
						num[lastnum] *= 10;
						num[nextnum] = c - 48;
						lastnum++;  // can overload array!!! <<<
					}
					else
					{
						for (int i = 0; i < numarr; i++)
						{
							maxpath += num[i];   // adds up number
							num[i] = 0; // clears num array
						}
						numberPos++;  // increases number count so it finds the next row somehow                        
						new_row_base = Row_num + new_row_base; // last row_pos + last row _num = new row pos.
						Row_num++;
						if (binaryswitch.test(Row_pos))
						{
							new_row_base += 1;   // if binary pos is true move the row pos over by 1.
						}
					}
				}
			}
			else // skips numbers not needed
			{
				while (c > 47 && c < 58) //make sure its a number
				{
					c = in.get();
					if (c > 47 && c < 58)
					{
						continue; // stops incrementing number pos till it ends current number
					}
					else
					{
						numberPos++;  // increases number count so it finds the next row somehow
					}
				}
			}

		}

	}


	return 0;
}

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: OrbitalReign's career change

Post by chili » September 3rd, 2017, 4:42 am

Props for giving it the old college try, and I hope the solution was enlightening.

Pathing stuff like A* or Dijkstra would have worked as well. You would just have to assume a priori knowledge of the range of possible values (1-99).
Chili

Post Reply