]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 15: not so complicated
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 15 Dec 2023 07:09:57 +0000 (08:09 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 15 Dec 2023 07:09:57 +0000 (08:09 +0100)
2023/29.pl [new file with mode: 0755]
2023/30.pl [new file with mode: 0755]

diff --git a/2023/29.pl b/2023/29.pl
new file mode 100755 (executable)
index 0000000..a605892
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/perl -w
+
+use v5.38;
+
+chomp (my $line = <>);
+
+my $sum;
+for (split /,/, $line) {
+       my $n = 0;
+       for (split //) {
+               $n += ord;
+               $n *= 17;
+               $n %= 256;
+       }
+       $sum += $n;
+}
+say $sum;
+
diff --git a/2023/30.pl b/2023/30.pl
new file mode 100755 (executable)
index 0000000..8e813c6
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/perl -w
+
+use v5.38;
+use experimental 'for_list', 'builtin';
+use builtin 'indexed';
+
+chomp (my $line = <>);
+
+sub hash {
+       $_ = shift;
+       my $n = 0;
+       for (split //) {
+               $n += ord($_);
+               $n *= 17;
+               $n %= 256;
+       }
+       $n;
+}
+
+my @slot;
+
+for (split /,/, $line) {
+       my ($name, $op, $num) = /(\w+)(\W)(\d*)/;
+       my $id = hash($name);
+       my $sl = $slot[$id];
+       if ($op eq '=') {
+               my $found;
+               for my $s (@$sl) {
+                       if ($s->[0] eq $name) {
+                               $found = 1;
+                               $s->[1] = $num;
+                       }
+               }
+               if (!$found) {
+                       push @$sl, [ $name, $num ];
+               }
+       } elsif ($op eq '-') {
+               @$sl = grep { $_->[0] ne $name } @{ $slot[$id] };
+       }
+}
+
+my $sum;
+for my ($i, $s) (indexed @slot) {
+       for my ($j, $p) (indexed @$s) {
+               $sum += ($i+1) * ($j+1) * $p->[1];
+       }
+}
+say $sum;
+