package it.simone.google.code.jam2012;

import java.util.HashSet;
import java.util.Set;

public class RecycledNumber implements GoogleCodeExercise {

	int a = 0;
	int b = 0;
	Set<Couple> distinctCouple = null;
	long maxTime = 0;

	private class Couple {
		int x = 0;
		int y = 0;

		public Couple(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Couple other = (Couple) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (x == other.x && y == other.y)
				return true;
			if (x == other.y && y == other.x)
				return true;
			return false;
		}

		private RecycledNumber getOuterType() {
			return RecycledNumber.this;
		}

	}

	@Override
	public String execute(String line) {
		int result = 0;

		String[] data = line.split(" ");
		a = Integer.parseInt(data[0]);
		b = Integer.parseInt(data[1]);
		initialize();

		int n = a;
		//
		while (n <= b) {
			result += analize(n);
			n++;
//			if (n % 1000 == 0)
//				System.out.println(n);
		}

		System.out.println(result);
		return "" + result;
	}

	@Override
	public void initialize() {
		distinctCouple = new HashSet<Couple>();
	}

	private int analize(int number) {
		String numString = "" + number;
		initialize();
		for (int i = 1; i < numString.length(); i++) {
			int m = shift(numString, i);
			if (a <= number && number < m && m <= b) {
				Couple couple=new Couple(number,m);
				if (!distinctCouple.contains(couple))
					distinctCouple.add(couple);
			}
		}
		return distinctCouple.size();
	}

	private int shift(String numString, int posx) {

		String result = null;
		result = "" + numString.substring(posx) + numString.substring(0, posx);

		return Integer.parseInt(result.toString());
	}

}

package fixjava;

import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

public class ParallelWorkQueue<I, O> implements Iterable<Future<O>> {

	public interface CallableFactory<I, O> {
		public Callable<O> newInstance(I input);
	}

	private ExecutorService threadPool;
	private LinkedBlockingQueue<Future<O>> workQueue;
	private Future<O> poisonPill = null;

	public ParallelWorkQueue(final int numThreads, final Iterable<I> inputs, final CallableFactory<I, O> callableFactory) {
		threadPool = Executors.newFixedThreadPool(numThreads);

		// Keep a max of (100x the number of threads) items in the work queue at a time 
		workQueue = new LinkedBlockingQueue<>(numThreads * 100);

		// Create work-producer thread
		new Thread() {
			public void run() {
				try {
					for (final I input : inputs)
						workQueue.put(threadPool.submit(callableFactory.newInstance(input)));

					// poisonPill was null until now, and null can never be put in the queue, so
					// the barrier can't be triggered. Now set poisonPill to another value that it
					// can only attain once, a NOP work unit.
					workQueue.put(poisonPill = threadPool.submit(new Callable<O>() {
						@Override
						public O call() throws Exception {
							return null;
						}
					}));

					// Shut down threads after last callable invoked
					threadPool.shutdown();

				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
			};
		}.start();

	}

	@Override
	public Iterator<Future<O>> iterator() {
		return new Iterator<Future<O>>() {
			Future<O> next = null;
			boolean nextConsumed = true;

			@Override
			public boolean hasNext() {
				if (nextConsumed) {
					try {
						next = workQueue.take();
					} catch (InterruptedException e) {
						throw new RuntimeException(e);
					}
					nextConsumed = false;
				}
				// Check for poison pill
				if (next == poisonPill) {
					try {
						// Get result of poison pill Future so thread exits
						next.get();
					} catch (Exception e) {
						throw new RuntimeException(e);
					}
					return false; // at end
				} else {
					return true;
				}
			}

			@Override
			public Future<O> next() {
				if (nextConsumed)
					throw new RuntimeException("Can't call next() twice without calling hasNext()");
				nextConsumed = true;
				return next;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * Return an Iterable<Integer> that iterates from 0 to n-1 inclusive. Can be
	 * passed into the "inputs" parameter of ParallelWorkQueue.
	 */
	public static Iterable<Integer> makeIntRangeIterable(final int n) {
		return new Iterable<Integer>() {
			private final int max = n;

			@Override
			public Iterator<Integer> iterator() {
				return new Iterator<Integer>() {
					int num = 0;

					@Override
					public boolean hasNext() {
						return num < max;
					}

					@Override
					public Integer next() {
						return num++;
					}

					@Override
					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}
}

