Advertisement
Rayk

Snowflake

Feb 28th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Snowflake {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         final Pattern surface = Pattern.compile("^[^A-Za-z0-9]+$");
  12.         final Pattern mantle = Pattern.compile("^[\\d_]+$");
  13.         final Pattern core = Pattern.compile("^[^A-Za-z0-9]+[\\d_]+(?<core>[A-Za-z]+)[\\d_]+[^A-Za-z0-9]+$");
  14.  
  15.         int length = 0;
  16.  
  17.         for (int i = 0; i < 5; i++) {
  18.             String line = reader.readLine();
  19.  
  20.             if (i == 0 || i == 4) {
  21.                 Matcher matcher = surface.matcher(line);
  22.  
  23.                 if (!matcher.find()) {
  24.                     System.out.println("Invalid");
  25.                     return;
  26.                 }
  27.             } else if (i == 1 || i == 3) {
  28.                 Matcher matcher = mantle.matcher(line);
  29.  
  30.                 if (!matcher.find()) {
  31.                     System.out.println("Invalid");
  32.                     return;
  33.                 }
  34.             } else {
  35.                 Matcher matcher = core.matcher(line);
  36.  
  37.                 if (!matcher.find()) {
  38.                     System.out.println("Invalid");
  39.                     return;
  40.                 } else {
  41.                     length = matcher.group("core").length();
  42.                 }
  43.             }
  44.         }
  45.  
  46.         System.out.println("Valid");
  47.         System.out.println(length);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement