diff options
author | Christian Segundo | 2024-12-08 11:21:53 +0100 |
---|---|---|
committer | Christian Segundo | 2024-12-08 11:21:53 +0100 |
commit | 293348441811e26b4b885d57608f796b72974bda (patch) | |
tree | 0fdfb21939c53a65e1ca6a1a1db5db9c1d2065b6 /7.pl | |
parent | 25f7a5e5bc80c699d9538aed0d3f31b9406e0a74 (diff) | |
download | advent-of-code-2024-master.tar.gz |
Diffstat (limited to '7.pl')
-rw-r--r-- | 7.pl | 114 |
1 files changed, 114 insertions, 0 deletions
@@ -0,0 +1,114 @@ +#!/usr/bin/perl +use strict; +use warnings; + +# use Data::Dumper; + +my @numberss; +my @results; +my $result_sum = 0; +my $result_sum_part_two = 0; + +open(my $fh, "7-input.txt") or die; +while (<$fh>) { + chomp; + $_ =~ /^(\d+):\s(.*)$/; + push @results, $1; + push @numberss, [split /\s/, $2]; +} +close($fh); + +my $ffffffffffffff = -1; +foreach my $result (@results) { + $ffffffffffffff++; + my $numbers = $numberss[$ffffffffffffff]; + my @possible_operations_part_one = combinations(['+', '*'], scalar @$numbers-1); + my @possible_operations_part_two = combinations(['+', '*', '|'], scalar @$numbers-1); + + my @op_list_base; + foreach (0..scalar @$numbers-1) { + if ($_ == 0) { $op_list_base[$_] = $numbers->[$_]; } + else { $op_list_base[$_+$_] = $numbers->[$_]; } + } + + my @to_calculate_part_one; + foreach my $operation_symbol (@possible_operations_part_one) { + my @op_list = @op_list_base; + my $symbol_idx = 0; + foreach (0..scalar @op_list-1) { + if (not defined $op_list[$_]) { + $op_list[$_] = $operation_symbol->[$symbol_idx]; + $symbol_idx++; + } + } + push @to_calculate_part_one, \@op_list; + } + + my @to_calculate_part_two; + foreach my $operation_symbol (@possible_operations_part_two) { + my @op_list = @op_list_base; + my $symbol_idx = 0; + foreach (0..scalar @op_list-1) { + if (not defined $op_list[$_]) { + $op_list[$_] = $operation_symbol->[$symbol_idx]; + $symbol_idx++; + } + } + # print Dumper(\@op_list); + push @to_calculate_part_two, \@op_list; + } + + foreach (0..scalar@to_calculate_part_one-1) { + if (calculate($to_calculate_part_one[$_]) == $result) { + $result_sum=$result_sum+$result; + print "puzzle 1: $result ok\n"; + last; + } + } + + foreach (0..scalar@to_calculate_part_two-1) { + if (calculate($to_calculate_part_two[$_]) == $result) { + $result_sum_part_two=$result_sum_part_two+$result; + print "puzzle 2: $result ok\n"; + last; + } + } +} + +print "puzzle 1: $result_sum\n"; +print "puzzle 2: $result_sum_part_two\n"; + +sub calculate { + my $expression = shift; + my @local_expression = @$expression; + + while (scalar @local_expression > 1) { + for(my $o=1; $o <= scalar @local_expression-2; $o=$o+2) { + my $r = 0; + if ($local_expression[$o] eq '*') { $r = $local_expression[$o-1] * $local_expression[$o+1]; } + elsif ($local_expression[$o] eq '+') { $r = $local_expression[$o-1] + $local_expression[$o+1]; } + elsif ($local_expression[$o] eq '|') { $r = $local_expression[$o-1] . $local_expression[$o+1]; } + else { die; } + $local_expression[$o-1] = $r; + splice @local_expression,$o,2; + last; + } + } + return $local_expression[0]; +} + +sub combinations { + my ($symbols, $len) = @_; + my @result = (); + my $s_len = scalar @$symbols; + for my $i (0..$s_len**$len - 1) { + my $num = $i; + my @combination; + for my $position (1..$len) { + unshift @combination, $symbols->[$num % $s_len]; + $num = int($num/$s_len); + } + push @result, \@combination; + } + return @result; +} |