import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static List<Integer> countRotations(int nCogWheels, List<List<Integer>> connections){
        HashMap<Integer,String> map = new HashMap<>();
        System.err.println("Ajout Wheel 0");
        map.put(0,"clockwise");
        for(List<Integer> wheelList : connections){
            //System.err.println(wheelList);
                if(map.containsKey(wheelList.get(1))){
                    System.err.println("Wheel " + wheelList.get(1) + " est dans la liste");
                    if(wheelList.get(1) != 0){
                        System.err.println("Check rotation wheels...");
                        if(map.get(wheelList.get(0)).equals(map.get(wheelList.get(1)))) {
                            System.err.println("DEADLOCK : configuration invalide");
                            return Arrays.asList(-1, -1);
                        }
                        System.err.println("Configuration valide");
                    }
                }else{
                    System.err.println("Ajout Wheel " + wheelList.get(1));
                    if(map.get(wheelList.get(0)).equals("clockwise"))
                        map.put(wheelList.get(1), "counterclockwise");
                    else
                        map.put(wheelList.get(1),"clockwise");
                }
        }

        int clockwise=0;
        int counterclockwise=0;
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            //System.err.println("wheel: " + entry.getKey() + ", direction: " + entry.getValue());
            if(entry.getValue().equals("clockwise"))
                clockwise+=1;
            else
                counterclockwise+=1;
        }
        return Arrays.asList(clockwise,counterclockwise);
    }

    public static void main(String[] args) {
        System.out.println(countRotations(5,Arrays.asList(Arrays.asList(0,2),
                Arrays.asList(2,4),
                Arrays.asList(2,1),
                Arrays.asList(1,3),
                Arrays.asList(3,4))));

        System.out.println(countRotations(3,Arrays.asList(Arrays.asList(0,1),
                Arrays.asList(1,2),
                Arrays.asList(0,2))));
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: