Advertisement
monkeypaste

Untitled

Dec 8th, 2023 (edited)
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. var lines = (ArrayList<String>) Files.readAllLines(Paths.get(path));
  2. String d = lines.get(0).trim();
  3. var adj = new HashMap<String, String[]>();
  4. var list = new ArrayList<String>();
  5. for(int i = 2; i < lines.size(); i++){
  6.     var s = lines.get(i).split("[^A-Z]+");
  7.     if(s[0].endsWith("A")) list.add(s[0]);
  8.     adj.put(s[0], new String[]{s[1], s[2]});
  9. }
  10.  
  11. list.forEach(curr -> {
  12.     int i = -1, steps = 0;
  13.     while(!curr.endsWith("Z")){
  14.         steps++;
  15.         i = (i + 1) % d.length();
  16.         curr = adj.get(curr)[d.charAt(i) == 'L' ? 0 : 1];
  17.     }
  18.     print("%d", steps);
  19. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement