From c1d40dd98ed914f3a7c78bef6e52239d6fa95804 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sun, 25 Dec 2022 06:45:33 +0100 Subject: [PATCH] Day 25: symmetric base-5 --- 2022/49.pl | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 2022/49.pl diff --git a/2022/49.pl b/2022/49.pl new file mode 100755 index 0000000..5dc8244 --- /dev/null +++ b/2022/49.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use POSIX qw(floor); + +sub snafu($sn) { + my ($n, $order) = (0, 1); + for my $dig (reverse split(//, $sn)) { + my $x; + $x=1 if $dig eq '1'; + $x=2 if $dig eq '2'; + $x=0 if $dig eq '0'; + $x=-1 if $dig eq '-'; + $x-=2 if $dig eq '='; + $n += $x * $order; + $order *= 5; + } + $n; +} + +my $sum; +while (<>) { + chomp; + + $sum +=snafu($_); +} +say "sum = $sum"; + +my $order = 5**25; +my @digits = qw(= - 0 1 2); +my $ans = ''; +while ($order >= 1) { + my $d = floor($sum/$order); + my $d2 = ($sum - $order*$d)*5/$order; + $d++ if $d2 > 2.5; + $sum -= $order * $d; + $ans .= $digits[$d+2]; + $order /= 5; +} +$ans =~ s/^0*//; +say $ans; -- 2.43.5