Advertisement
Guest User

Advent of Code 2021 - Day 2

a guest
Dec 2nd, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. ///usr/bin/env jbang "$0" "$@" ; exit $?
  2. //JAVAC_OPTIONS --enable-preview -source 17
  3. //JAVA_OPTIONS --enable-preview
  4.  
  5. import static java.lang.System.*;
  6.  
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.util.function.Function;
  10. import java.util.stream.Stream;
  11.  
  12. public class Day2 {
  13.  
  14.     static record Position(int h, int d, int a) {
  15.         static Position move(Position position, Movement movement) {
  16.             return switch (movement) {
  17.                 case Forward f -> new Position(position.h + f.value, position.d, 0);
  18.                 case Down d -> new Position(position.h, position.d + d.value, 0);
  19.                 case Up u -> new Position(position.h, position.d - u.value, 0);
  20.             };
  21.         }
  22.  
  23.         static Position aim(Position position, Movement movement) {
  24.             return switch (movement) {
  25.                 case Forward f -> new Position(position.h + f.value, position.d + (position.a * f.value), position.a);
  26.                 case Down d -> new Position(position.h, position.d, position.a + d.value);
  27.                 case Up u -> new Position(position.h, position.d, position.a - u.value);
  28.             };
  29.         }
  30.  
  31.         static Position combine(Position l, Position r) {
  32.             return new Position(l.h + r.h, l.d + r.d, l.a + r.a);
  33.         }
  34.    
  35.  
  36.         int multiplied() { return h * d;}
  37.     };
  38.  
  39.  
  40.  
  41.     static sealed interface Movement permits Forward, Down, Up {
  42.         int value();
  43.     }
  44.  
  45.     static record Forward(int value) implements Movement {}
  46.     static record Down(int value) implements Movement {}
  47.     static record Up(int value) implements Movement{}
  48.  
  49.  
  50.     static Stream<Movement> lines() {
  51.         final Function<String, Movement> mapper = (s) -> {
  52.             final String[] ss = s.split(" ");
  53.             final String direction = ss[0];
  54.             final int value = Integer.parseInt(ss[1]);
  55.             return switch(direction) {
  56.                 case "forward" -> new Forward(value);
  57.                 case "down" -> new Down(value);
  58.                 case "up" -> new Up(value);
  59.                 default -> throw new RuntimeException();
  60.             };
  61.         };
  62.         try  {
  63.             return Files.readAllLines(Paths.get("input.txt")).stream().map(mapper);
  64.         } catch (Exception ex) {
  65.             ex.printStackTrace();
  66.         }
  67.         throw new RuntimeException();
  68.     }
  69.    
  70.  
  71.     static void part1() {
  72.         final Position pos = lines().reduce(new Position(0, 0, 0), Position::move, Position::combine);
  73.         out.println(pos.multiplied());
  74.     }
  75.  
  76.     static void part2() {
  77.         final Position pos = lines().reduce(new Position(0, 0, 0), Position::aim, Position::combine);
  78.         out.println(pos.multiplied());
  79.     }
  80.  
  81.     public static void main(String... args) {
  82.         part1();
  83.         part2();
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement