Advertisement
Guest User

Advent of Code 2021 - Day 1

a guest
Dec 1st, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. ///usr/bin/env jbang "$0" "$@" ; exit $?
  2.  
  3.  
  4. import static java.lang.System.*;
  5.  
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.Collections;
  10. import java.util.function.BiConsumer;
  11. import java.util.function.Consumer;
  12. import java.util.function.IntConsumer;
  13. import java.util.function.ToLongBiFunction;
  14. import java.util.stream.IntStream;
  15.  
  16. public class Day1 {
  17.  
  18.  
  19.     static class Mapper implements IntStream.IntMapMultiConsumer {
  20.  
  21.         private int last = -1;
  22.  
  23.         @Override
  24.         public void accept(int t, IntConsumer u) {
  25.  
  26.             if (last >= 0 && t > last)
  27.                 u.accept(1);
  28.            
  29.             last = t;
  30.  
  31.         }
  32.        
  33.     }
  34.  
  35.  
  36.     static record Window(int a, int b, int c) {
  37.         int sum() {
  38.             return a + b + c;
  39.         }
  40.  
  41.         boolean isValid() {
  42.             return a >= 0 && b >= 0 && c >= 0;
  43.         }
  44.     }
  45.  
  46.     static class Windower implements BiConsumer<Integer, Consumer<Window>> {
  47.         private int a = -1;
  48.         private int b = -1;
  49.         private int c = -1;
  50.  
  51.         @Override
  52.         public void accept(Integer t, Consumer<Window> u) {
  53.             a = b;
  54.             b = c;
  55.             c = t;
  56.             u.accept(new Window(a, b, c));
  57.         }
  58.        
  59.     }
  60.    
  61.     static IntStream lines() {
  62.         try  {
  63.             return Files.readAllLines(Paths.get("input.txt")).stream().mapToInt(Integer::parseInt);
  64.         } catch (Exception ex) {
  65.             ex.printStackTrace();
  66.         }
  67.         throw new RuntimeException();
  68.     }
  69.  
  70.     static void part1() {
  71.         final Mapper mapper = new Mapper();
  72.         final Integer sum = lines().mapMulti(mapper).sum();
  73.         out.println("Part 1: " + sum);
  74.     }
  75.  
  76.     static void part2() {
  77.         final Mapper mapper = new Mapper();
  78.         final Windower windower = new Windower();
  79.         final Integer sum = lines()
  80.             .boxed()
  81.             .mapMulti(windower)
  82.             .filter(Window::isValid)
  83.             .mapToInt(Window::sum)
  84.             .mapMulti(mapper)
  85.             .sum();
  86.  
  87.         out.println("Part 2: " + sum);
  88.  
  89.     }
  90.     public static void main(String... args) {
  91.         part1();
  92.         part2();
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement