]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 10: off-by-ones
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 10 Dec 2022 05:44:19 +0000 (06:44 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 10 Dec 2022 05:44:19 +0000 (06:44 +0100)
2022/19.pl [new file with mode: 0755]
2022/20.pl [new file with mode: 0755]

diff --git a/2022/19.pl b/2022/19.pl
new file mode 100755 (executable)
index 0000000..bb8db33
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+use experimental 'multidimensional';
+
+my $tsc = 1;
+my $x = 1;
+my $prev_gen = 0;
+my $sum = 0;
+while (<>) {
+       my ($op, $num) = /^(\w+)(?: (-?\d+))?/;
+       # say "$tsc $x  $op $num";
+       $tsc++;
+       my $gen = int(($tsc+20)/40);
+       if ($gen != $prev_gen) {
+               $prev_gen = $gen;
+               my $str = $x * ((40*$gen)-20);
+               $sum += $str;
+               # say "$tsc x $x = ", $str;
+       }
+       if ($op eq 'addx') {
+               $x += $num;
+               $tsc++;
+       }
+}
+
+say $sum;
+
diff --git a/2022/20.pl b/2022/20.pl
new file mode 100755 (executable)
index 0000000..9c2a59d
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+use experimental 'multidimensional';
+
+my $tsc = 1;
+my $prev_tsc = 1;
+my $x = 1;
+my $prev_gen = 0;
+while (<>) {
+       my ($op, $num) = /^(\w+)(?: (-?\d+))?/;
+
+       $tsc++;
+       while ($prev_tsc <= $tsc) {
+               # say "$prev_tsc ok" if $x +2 <= $prev_tsc;
+               my $off = $prev_tsc % 40;
+               print(($x <= $off && $x + 2 >= $off) ? '#' : '.');
+               print "\n" if $prev_tsc % 40 == 0;
+               $prev_tsc++;
+       }
+       if ($op eq 'addx') {
+               $x += $num;
+               $tsc++;
+       }
+}
+print "\n";
+