Created
February 19, 2018 19:13
-
-
Save dkovacevic/37e42ed18345175a02830ad991737e15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.*; | |
| public class Lotto { | |
| static Random random = new Random(); | |
| static class Combination extends ArrayList<Integer> { | |
| public UUID id; | |
| Combination() { | |
| id = UUID.randomUUID(); | |
| for (int i = 0; i < 7; i++) | |
| add(randomNumber()); | |
| } | |
| } | |
| private static int randomNumber() { | |
| return Math.abs(random.nextInt(39) + 1); | |
| } | |
| static class CombinationList extends ArrayList<Combination> { | |
| } | |
| // <CombinationId, hits> | |
| static class Winners extends HashMap<UUID, ArrayList<Integer>> { | |
| } | |
| static class Index extends HashMap<Integer, CombinationList> { | |
| Index(ArrayList<Combination> combinations) { | |
| for (Combination c : combinations) { | |
| put(c); | |
| } | |
| } | |
| void put(Combination c) { | |
| for (Integer number : c) { | |
| CombinationList combinations = super.computeIfAbsent(number, k -> new CombinationList()); | |
| combinations.add(c); | |
| } | |
| } | |
| void printWinners(Collection<Integer> raffle) { | |
| Date s = new Date(); | |
| Winners winners = new Winners(); | |
| for (Integer number : raffle) { | |
| CombinationList combinations = get(number); | |
| for (Combination c : combinations) { | |
| ArrayList<Integer> hits = winners.computeIfAbsent(c.id, k -> new ArrayList<>()); | |
| hits.add(number); | |
| } | |
| } | |
| int seven = 0; | |
| int six = 0; | |
| int five = 0; | |
| for (ArrayList<Integer> hits : winners.values()) { | |
| seven += hits.size() == 7 ? 1 : 0; | |
| six += hits.size() == 6 ? 1 : 0; | |
| five += hits.size() == 5 ? 1 : 0; | |
| } | |
| Date e = new Date(); | |
| long l = e.getTime() - s.getTime(); | |
| System.out.printf("Inverted Dejo:\tSeven hits: %d, Six hits: %d, Five hits: %d in %d ms\n", seven, six, five, l); | |
| } | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| int size = 1 * 1000 * 1000; | |
| System.out.printf("Generating %,d random combinations...\n", size); | |
| ArrayList<Combination> all = generateRandomCombinations(size); | |
| Index index = new Index(all); | |
| Collection<Integer> raffle = raffle(); | |
| System.out.printf("Running ruffle...\n"); | |
| for (Integer number : raffle) { | |
| System.out.printf("%d, ", number); | |
| } | |
| System.out.printf("\nCalculating...\n"); | |
| linear(all, raffle); | |
| index.printWinners(raffle); | |
| Thread.sleep(1000); | |
| } | |
| private static void linear(ArrayList<Combination> all, Collection<Integer> raffle) { | |
| Date s = new Date(); | |
| int seven = 0; | |
| int six = 0; | |
| int five = 0; | |
| for (Combination c : all) { | |
| int hits = 0; | |
| for (Integer number : c) { | |
| if (raffle.contains(number)) | |
| hits++; | |
| } | |
| seven += hits == 7 ? 1 : 0; | |
| six += hits == 6 ? 1 : 0; | |
| five += hits == 5 ? 1 : 0; | |
| } | |
| Date e = new Date(); | |
| long l = e.getTime() - s.getTime(); | |
| System.out.printf("Linear Šime:\tSeven hits: %d, Six hits: %d, Five hits: %d in %d ms\n", seven, six, five, l); | |
| } | |
| private static Collection<Integer> raffle() { | |
| Collection<Integer> raffle = new ArrayList<>(); | |
| for (int i = 0; i < 7; i++) | |
| raffle.add(randomNumber()); | |
| return raffle; | |
| } | |
| private static ArrayList<Combination> generateRandomCombinations(int size) { | |
| ArrayList<Combination> ret = new ArrayList<>(size); | |
| for (int i = 0; i < size; i++) { | |
| ret.add(new Combination()); | |
| } | |
| return ret; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment