Matt23

PythonChallenge

Jul 17th, 2015
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. //num = 82682 - problem
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class Main {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         int num = 12345;
  15.         String res = "";
  16.         while (num != -1){
  17.             try {
  18.                 String a = String.format("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%d", num);
  19.                 URL url = new URL(a);
  20.                 URLConnection conn = url.openConnection();
  21.                 BufferedReader br = new BufferedReader(
  22.                         new InputStreamReader(conn.getInputStream()));
  23.                 res = br.readLine();
  24.  
  25.                 if (res.contains("Divide")){
  26.                     num /= 2;
  27.                 }
  28.                 else {
  29.                     Pattern p = Pattern.compile("\\d+$");
  30.                     Matcher m = p.matcher(res);
  31.                     if (m.find()) num = Integer.parseInt(m.group(0));
  32.                 }
  33.                 System.out.println(num);
  34.             }
  35.             catch (NumberFormatException e){
  36.                 System.out.println(res);
  37.                 break;
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment