public static void main(String[] args) {
		System.out.println("Problem #305");
		long t1 = System.currentTimeMillis();
		long result = 0;

		String kPow[] = new String[13];
		int f_k[] = new int[kPow.length];

		int tempOccur[] = new int[kPow.length];

		int cumulativeDigits = 0;
		currentNumber = 1;
		currentDigit = 0;
		currentDigits = "";
		StringBuffer workingArea = new StringBuffer();
		for (int i = 0; i < kPow.length; ++i) {
			int k = i + 1;
			tempOccur[i] = (int) Math.pow(3, k);
			kPow[i] = Integer.toString(tempOccur[i]);
		}

		boolean notDone = true;
		while (notDone) {
			notDone = false;
			++cumulativeDigits;

			char nextDigit = getNextDigit();
			workingArea.append(nextDigit);

			for (int i = 0; i < 9; ++i) {
				if (tempOccur[i] > 0) {
					notDone = true;
					int searchPos = workingArea.length() - kPow[i].length();
					if (searchPos >= 0) {
						int foundPos = workingArea.indexOf(kPow[i], searchPos);
						if (foundPos >= 0) {
							tempOccur[i]--;
							if (tempOccur[i] == 0) {
								f_k[i] = cumulativeDigits
										- (kPow[i].length() - 1);
								System.out.println(i + " " + kPow[i] + ": "
										+ f_k[i]);
							}
						}
					}
				}
			}

			if (workingArea.length() > 10000) {
				workingArea.delete(0, 10);
			}

		}

		for (int i = 0; i < f_k.length; ++i) {
			result += f_k[i];
		}
		long t2 = System.currentTimeMillis();
		long delta = t2 - t1;

		System.out.println("result: " + result + " " + delta + "ms");
	}