-
-
Save arodland/b433602c13527dc6baf391e295e0374a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $sum = 0; | |
for "input.txt".IO.lines -> $line { | |
my ($left, $right) = $line.split(/'|'/); | |
my @w = $right.words; | |
my @wlen = @w».chars; | |
$sum += (@wlen.grep: { $_ == any(2, 3, 4, 7) }); | |
} | |
say $sum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $sum = 0; | |
for "input.txt".IO.lines -> $line { | |
my ($left, $right) = $line.split(/'|'/); | |
my @sets = $left.words.map: { Set($_.comb) }; | |
@sets = @sets.unique.cache; | |
my @digits; | |
# Digits with unique numbers of segments | |
@digits[1] = @sets.first: { $_.elems == 2 }; | |
@digits[4] = @sets.first: { $_.elems == 4 }; | |
@digits[7] = @sets.first: { $_.elems == 3 }; | |
@digits[8] = @sets.first: { $_.elems == 7 }; | |
# (0, 6, 9) all have six segments lit | |
my @sixes = @sets.grep: { $_.elems == 6 }; | |
# 9 has all of the same segments as 4 | |
@digits[9] = @sixes.first: { $_ (&) @digits[4] == @digits[4] }; | |
@sixes = @sixes.grep: none @digits[9]; | |
# Of the remaining, 0 is the only one with both right segments | |
@digits[0] = @sixes.first: { $_ (&) @digits[1] == @digits[1] }; | |
# And 6 is the one that's left | |
@digits[6] = @sixes.first: none @digits[0]; | |
# The upper right segment is the one that 6 doesn't have, and | |
# the lower left is the one that 9 doesn't have. | |
my $upper_right = @digits[8] (-) @digits[6]; | |
my $lower_left = @digits[8] (-) @digits[9]; | |
# (2, 3, 5) all have five segments lit | |
my @fives = @sets.grep: { $_.elems == 5 }; | |
# 2 is the only one of them that has the lower left segment | |
@digits[2] = @fives.first: { $_ (&) $lower_left }; | |
@fives = @fives.grep: none @digits[2]; | |
# 3 has the upper right (so does 2, but we did that one first)... | |
@digits[3] = @fives.first: { $_ (&) $upper_right }; | |
# And 5 doesn't. | |
@digits[5] = @fives.first: none @digits[3]; | |
say @digits; | |
# Reverse the array to a map | |
my %set_to_digit{Set} = @digits.antipairs; | |
# Finally, look at the right side... | |
my @r = $right.words.map: { Set($_.comb) }; | |
# Do the decoding... | |
my $num = (@r.map: { %set_to_digit{$_} }).join.Num; | |
say $num; | |
# Add to the sum... | |
$sum += $num; | |
} | |
# And win. | |
say $sum; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment