aoc2024

Advent of Code 2024
Log | Files | Refs | README

day13_pt1.java (3674B)


      1 import java.nio.file.Files;
      2 import java.nio.file.Path;
      3 import java.io.IOException;
      4 import java.util.*;
      5 import java.util.stream.*;
      6 import java.util.regex.*;
      7 
      8 class day13_pt1 {
      9   // A * 94 + B * 22 = 8400
     10   // A * 34 + B * 67 = 5400
     11   // How many solutions does it have?
     12   // Identify the A coefficent 
     13   // Multiply each side by the A coefficient of the other 
     14   // A * 3196 + B * 748 = 285600
     15   // A * 3196 + B * 6298 = 507600
     16   // Subtract one from the other so the A coefficients are cancelled to zero
     17   // B * -5550 = -222000
     18   // Divide the constant side by the remaining B coefficient; INTEGERS ONLY here, if we have a remainder then bail, can't solve.
     19   // B = 40
     20   // Substitute back into the original equation to find A OR repeat the process with the B coefficient to find A
     21   // Multiply each side by the B coefficient of the other
     22   // A * 6298 + B * 1474 = 562800
     23   // A * 748 + B * 1474 = 118800
     24   // A * 5550 = 444000
     25   // A = 80
     26   // So; we have a red herring about minimisation; simultaneous equations only have one solution it seems
     27   // 
     28   // I'm pretty sure this problem lends itself really well to vector / matrix math but I can't remember exactly how to get there from here
     29   // We'll do it simply
     30   static final Pattern A = Pattern.compile("Button A: X\\+(\\d+), Y\\+(\\d+)");
     31   static final Pattern B = Pattern.compile("Button B: X\\+(\\d+), Y\\+(\\d+)");
     32   static final Pattern Z = Pattern.compile("Prize: X=(\\d+), Y=(\\d+)");
     33   public static void main(String[] args) throws IOException {
     34     
     35     var lines = Files.readAllLines(Path.of(args[0]));
     36     var res = 0l;
     37     for (int i=0; i< lines.size(); i+=4) {
     38       var aline = lines.get(i);
     39       var bline = lines.get(i+1);
     40       var zline = lines.get(i+2);
     41       var am = A.matcher(aline);
     42       if (!am.matches()) throw new RuntimeException("line doesn't match [%s]".formatted(aline));
     43       var ax = Long.parseLong(am.group(1));
     44       var ay = Long.parseLong(am.group(2));
     45       
     46       var bm = B.matcher(bline);
     47       if (!bm.matches()) throw new RuntimeException("line doesn't match [%s]".formatted(bline));
     48       var bx = Long.parseLong(bm.group(1));
     49       var by = Long.parseLong(bm.group(2));
     50 
     51       var zm = Z.matcher(zline);
     52       if (!zm.matches()) throw new RuntimeException("line doesn't match [%s]".formatted(zline));
     53 
     54       // PART2
     55       //var zx = Long.parseLong(zm.group(1));
     56       //var zy = Long.parseLong(zm.group(2));
     57       var zx = Long.parseLong(zm.group(1)) + 10000000000000l;
     58       var zy = Long.parseLong(zm.group(2)) + 10000000000000l;
     59       
     60       //System.out.printf("A * %d + B * %d = %d\n", ax, bx, zx);
     61       //System.out.printf("A * %d + B * %d = %d\n", ay, by, zy);
     62       // Solve for A 
     63       {
     64         var c1 = zx * by - zy * bx;
     65         var c2 = ax * by - ay * bx;
     66         
     67         if (c1 % c2 != 0) {
     68           //System.out.println("non-integer result, skipping");
     69           continue;
     70         }
     71         var a = c1 / c2;
     72         //System.out.printf("A = %d\n", a);
     73         //if (a > 100) { // PART2
     74           //System.out.println("A is too big, skipping");
     75           //continue;
     76         //}
     77         res += (3 * a);
     78       }
     79       // Solve for B
     80       {
     81         var c1 = zx * ay - zy * ax;
     82         var c2 = bx * ay - by * ax;
     83         if (c1 % c2 != 0) {
     84           //System.out.println("non-integer result, skipping");
     85           continue;
     86         }
     87         var b = c1 / c2;
     88         //System.out.printf("B = %d\n", b);
     89         //if (b > 100) { // PART2
     90           //System.out.println("B is too big, skipping");
     91           //continue;
     92         //}
     93         res += b;
     94       }
     95       //System.out.println("");
     96     }
     97     System.out.printf("Day 13, pt1 = %d\n", res);
     98   }
     99 }