From: Jan "Yenya" Kasprzak Date: Fri, 15 Dec 2023 07:09:57 +0000 (+0100) Subject: Day 15: not so complicated X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=f141c1ae1581135db4bc9f68aa677c7c499f4956;p=aoc.git Day 15: not so complicated --- diff --git a/2023/29.pl b/2023/29.pl new file mode 100755 index 0000000..a605892 --- /dev/null +++ b/2023/29.pl @@ -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 index 0000000..8e813c6 --- /dev/null +++ b/2023/30.pl @@ -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; +