Guest User

Untitled

a guest
Dec 11th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4.  
  5. /**
  6.  * @author /u/Philboyd_Studge on 12/9/2015.
  7.  */
  8. public class Advent10 {
  9.  
  10.  
  11.     public static List<Integer> toInt(String in) {
  12.         return in.chars()
  13.                         .map(x -> x - '0')
  14.                         .boxed()
  15.                         .collect(Collectors.toList());
  16.     }
  17.  
  18.     public static List<Integer> lookAndSay(List<Integer> in) {
  19.         List<Integer> out = new ArrayList<>(in.size() * 2);
  20.         int count = 1;
  21.         for(int i=1; i< in.size();i++) {
  22.             if (in.get(i).equals(in.get(i-1))) {
  23.                 count++;
  24.             } else {
  25.                 out.add(count);
  26.                 out.add(in.get(i-1));
  27.                 count = 1;
  28.             }
  29.         }
  30.         out.add(count);
  31.         out.add(in.get(in.size()-1));
  32.         return out;
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.  
  37.         long time = System.currentTimeMillis();
  38.  
  39.         List<Integer> list = toInt("1113122113");
  40.  
  41.         for (int i = 0;i < 50; i++) {
  42.  
  43.             list = lookAndSay(list);
  44.         }
  45.         System.out.println("Length: " + list.size());
  46.         System.out.println("Time: "+(System.currentTimeMillis() - time) + "ms");
  47.  
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment