Skip to content

Instantly share code, notes, and snippets.

@clarkema
Created December 1, 2017 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkema/d0b6d8d9527854fb6f2388474861db2a to your computer and use it in GitHub Desktop.
Save clarkema/d0b6d8d9527854fb6f2388474861db2a to your computer and use it in GitHub Desktop.
Advent of code, day 1
# The captcha requires you to review a sequence of digits (your puzzle input)
# and find the sum of all digits that match the next digit in the list. The
# list is circular, so the digit after the last digit is the first digit in
# the list.
# For example:
# 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
# 1111 produces 4 because each digit (all 1) matches the next.
# 1234 produces 0 because no digit matches the next.
# 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
my $test = "1122";
sub seq_sum($digits) {
# Seems like there should be a nicer way to do this conversion
my @d = $digits.split('')[1..$digits.chars];
my @n = @d.rotate(1);
(@d Z @n).grep(-> ($a, $b) { $a == $b })
.map({.first})
.sum
.say
}
seq_sum($test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment