Guest User

advent of code 2025 day 5

a guest
Dec 5th, 2025
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | Source Code | 0 0
  1. public class Day5 extends Solution {
  2.  
  3.     public Day5() {
  4.         part1(this.input);
  5.         part2(this.input);
  6.     }
  7.  
  8.     @Override
  9.     protected void part1(List<String> input) {
  10.         List<Tuple> ranges = new ArrayList<>();
  11.         List<Long> products = new ArrayList<>();
  12.  
  13.         int i = 0;
  14.         while (!input.get(i).isBlank()) {
  15.             String[] split = input.get(i).split("-");
  16.             ranges.add(
  17.                 new Tuple(Long.valueOf(split[0]), Long.valueOf(split[1]))
  18.             );
  19.             i += 1;
  20.         }
  21.  
  22.         i += 1;
  23.         for (; i < input.size(); i++) {
  24.             products.add(Long.valueOf(input.get(i)));
  25.         }
  26.  
  27.         int result = 0;
  28.         for (long product : products) {
  29.             for (Tuple range : ranges) {
  30.                 if (range.inRange(product)) {
  31.                     result += 1;
  32.                     break;
  33.                 }
  34.             }
  35.         }
  36.  
  37.         System.out.println(result);
  38.     }
  39.  
  40.     @Override
  41.     protected void part2(List<String> input) {
  42.         List<Tuple> ranges = new ArrayList<>();
  43.         int i = 0;
  44.         while (!input.get(i).isBlank()) {
  45.             String[] split = input.get(i).split("-");
  46.             ranges.add(
  47.                 new Tuple(Long.valueOf(split[0]), Long.valueOf(split[1]))
  48.             );
  49.             i += 1;
  50.         }
  51.        
  52.         ranges.sort((r1, r2) -> {
  53.             return Long.compare(r1.i, r2.i);
  54.         });
  55.  
  56.         long result = 0;
  57.         Tuple prev = ranges.get(0);
  58.         for (i = 1; i < ranges.size(); i++) {
  59.             Tuple curr = ranges.get(i);
  60.             if (prev.inRange(curr.i)) {
  61.                 Tuple updated = new Tuple(
  62.                     Math.min(prev.i, curr.i),
  63.                     Math.max(prev.j, curr.j)
  64.                 );
  65.                 prev = updated;
  66.             } else {
  67.                 result += prev.j - prev.i + 1;
  68.                 prev = curr;
  69.             }
  70.         }
  71.  
  72.         result += prev.j - prev.i + 1;
  73.         System.out.println(result);
  74.     }
  75.  
  76.     public static void main(String[] args) {
  77.         Day5 day5 = new Day5();
  78.     }
  79. }
  80.  
  81. class Tuple {
  82.  
  83.     public final long i;
  84.     public final long j;
  85.  
  86.     public Tuple(long i, long j) {
  87.         this.i = i;
  88.         this.j = j;
  89.     }
  90.  
  91.     public boolean inRange(long product) {
  92.         return i <= product && product <= j;
  93.     }
  94.  
  95.     @Override
  96.     public String toString() {
  97.         return this.i + " " + this.j;
  98.     }
  99.     // Add equals(), hashCode(), toString() methods manually or via IDE
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment