Created
          April 23, 2015 17:41 
        
      - 
      
- 
        Save jooyunghan/a11f96303f1fa1b8b18e to your computer and use it in GitHub Desktop. 
    solve euler project #18 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @Test | |
| public void no18_2() throws Exception { | |
| String input = "75\n" + | |
| "95 64\n" + | |
| "17 47 82\n" + | |
| "18 35 87 10\n" + | |
| "20 04 82 47 65\n" + | |
| "19 01 23 75 03 34\n" + | |
| "88 02 77 73 07 63 67\n" + | |
| "99 65 04 28 06 16 70 92\n" + | |
| "41 41 26 56 83 40 80 70 33\n" + | |
| "41 48 72 33 47 32 37 16 94 29\n" + | |
| "53 71 44 65 25 43 91 52 97 51 14\n" + | |
| "70 11 33 28 77 73 17 78 39 68 17 57\n" + | |
| "91 71 52 38 17 14 91 43 58 50 27 29 48\n" + | |
| "63 66 04 68 89 53 67 30 73 16 69 87 40 31\n" + | |
| "04 62 98 27 23 09 70 98 73 93 38 53 60 04 23\n"; | |
| Assert.assertEquals(1074, (int) | |
| _(input.split("\n")) | |
| .map(line -> _(line.split(" ")).map(s -> Integer.parseInt(s))) // parse | |
| .foldr1((a, b) -> a.zipWith(b.zipWith(b.tail(), Math::max), (a1, b1) -> a1 + b1)) // solve | |
| .head()); | |
| } | |
| public interface Fn1<A, B> { | |
| B apply(A a); | |
| } | |
| public interface Fn2<A, B, C> { | |
| C apply(A a, B b); | |
| } | |
| static class Arr<T> { | |
| private final ArrayList<T> values = new ArrayList<>(); | |
| public Arr(T[] split) { | |
| this(asList(split)); | |
| } | |
| public Arr(Collection<T> split) { | |
| values.addAll(split); | |
| } | |
| public <R> Arr<R> map(Fn1<T, R> f) { | |
| ArrayList<R> result = new ArrayList<>(); | |
| for (T value : values) | |
| result.add(f.apply(value)); | |
| return _(result); | |
| } | |
| public T foldr1(Fn2<T, T, T> join) { | |
| T result = last(); | |
| for (int i = length() - 2; i >= 0; i--) { | |
| result = join.apply(get(i), result); | |
| } | |
| return result; | |
| } | |
| public <B, C> Arr<C> zipWith(Arr<B> bs, Fn2<T, B, C> join) { | |
| ArrayList<C> result = new ArrayList<>(); | |
| int len = Math.min(length(), bs.length()); | |
| for (int i = 0; i < len; i++) { | |
| result.add(join.apply(get(i), bs.get(i))); | |
| } | |
| return _(result); | |
| } | |
| public T last() { | |
| return get(length() - 1); | |
| } | |
| public T get(int i) { | |
| return values.get(i); | |
| } | |
| public int length() { | |
| return values.size(); | |
| } | |
| public T head() { | |
| return get(0); | |
| } | |
| public Arr<T> tail() { | |
| return _(values.subList(1, length())); | |
| } | |
| } | |
| public static <T> Arr<T> _(T[] split) { | |
| return new Arr<>(split); | |
| } | |
| public static <T> Arr<T> _(Collection<T> split) { | |
| return new Arr<>(split); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment