]> www.fi.muni.cz Git - aoc.git/blob - 2023/47.pl
Day 24: Maple
[aoc.git] / 2023 / 47.pl
1 #!/usr/bin/perl -w
2
3 use v5.38;
4
5 my @stones = map { chomp; [ /-?\d+/g ] } <>;
6 my ($min, $max) = (200000000000000, 400000000000000);
7 # my ($min, $max) = (7, 27);
8
9 sub from_param {
10         my ($x0, $y0, $z0, $xs, $ys, $zs) = @_;
11         return ($ys/$xs, $y0-($ys*$x0/$xs));
12 }
13
14 sub to_time {
15         my ($x, $xs, $x0) = @_;
16         return ($x-$x0)/$xs;
17 }
18
19 my $sum;
20 for my $s1 (0 .. $#stones-1) {
21         my @st1 = @{ $stones[$s1] };
22         my ($a, $b) = from_param(@st1);
23         say "y=$a*x + $b";
24
25         for my $s2 ($s1+1 .. $#stones) {
26                 my @st2 = @{ $stones[$s2] };
27                 my ($c, $d) = from_param(@st2);
28                 say "  y=$c*x + $d";
29                 if ($a == $c) {
30                         if ($b == $d) {
31                                 die "two same lines";
32                         }
33                         next;
34                 }
35                 my $xi = ($d-$b)/($a-$c);
36                 my $yi = $a*$xi + $b;
37                 my $t0 = to_time($xi, $st1[3], $st1[0]);
38                 my $t1 = to_time($xi, $st2[3], $st2[0]);
39                 say "  intersection at $xi, $yi at $t0 $t1";
40                 if ($xi < $min || $xi > $max || $yi < $min || $yi > $max) {
41                         say "  outside";
42                         next;
43                 }
44                 if ($t0 < 0 || $t1 < 0) {
45                         say "  in the past";
46                         next;
47                 }
48                 $sum++;
49         }
50 }
51
52 say $sum;