From fdcd179eb02bbabcc8bce89ec2981fca9d22ce13 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 6 Dec 2017 11:50:31 -0500 Subject: [PATCH] day06 --- 06/1.pl | 20 ++++++++++++++++++++ 06/2.pl | 29 +++++++++++++++++++++++++++++ 06/example | 1 + 06/input.txt | 1 + 4 files changed, 51 insertions(+) create mode 100644 06/1.pl create mode 100644 06/2.pl create mode 100644 06/example create mode 100644 06/input.txt diff --git a/06/1.pl b/06/1.pl new file mode 100644 index 0000000..dac01f0 --- /dev/null +++ b/06/1.pl @@ -0,0 +1,20 @@ +use 5.20.0; +use experimental 'postderef'; + +#use List::SomeUtils qw/ min_by /; +use List::UtilsBy qw/ max_by/; + +my @stacks = split ' ', <>; + +my %seen; + +until( $seen{ join ':', @stacks }++ ) { + warn "@stacks\n"; + my $col = max_by { $stacks[$_] } 0..$#stacks; + my $v = $stacks[$col]; + $stacks[$col] = 0; + $stacks[ ++$col % @stacks ]++ while $v--; +} + +say scalar keys %seen; + diff --git a/06/2.pl b/06/2.pl new file mode 100644 index 0000000..866a37b --- /dev/null +++ b/06/2.pl @@ -0,0 +1,29 @@ +use 5.20.0; +use experimental 'postderef'; + +use List::UtilsBy qw/ max_by/; + +my @stacks = split ' ', <>; + +my %seen; + +until( $seen{ join ':', @stacks }++ ) { + redistribute(); +} + +my $target = join ":", @stacks; + +my $i = 0; + +redistribute($i++) until $i and $target eq join ':', @stacks; + +say $i; + +sub redistribute { + warn "@stacks\n"; + my $col = max_by { $stacks[$_] } 0..$#stacks; + my $v = $stacks[$col]; + $stacks[$col] = 0; + $stacks[ ++$col % @stacks ]++ while $v--; +} + diff --git a/06/example b/06/example new file mode 100644 index 0000000..fd81a1c --- /dev/null +++ b/06/example @@ -0,0 +1 @@ +0 2 7 0 diff --git a/06/input.txt b/06/input.txt new file mode 100644 index 0000000..177ae3d --- /dev/null +++ b/06/input.txt @@ -0,0 +1 @@ +5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6