AoC Day 10 – Adapter Array

This is a solution to Advent of Code 2020 day 10, written in Raku.

https://adventofcode.com/2020/day/10

Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt differences multiplied by the number of 3-jolt differences?

Raku

Here I generate two lists, offset by 1, and then use the subtraction hyper operator to create a differential list.

  my @adaptors = '10-input.txt'.IO.lines.map({+$_}).sort;
  my @uppers = @adaptors;

  @adaptors.unshift(0);
  @uppers.append(@uppers.tail + 3);

  my @jolts = @uppers <<-<< @adaptors;

  my $ones = +@jolts.grep(* == 1);
  my $threes = +@jolts.grep(* == 3);
  say "{$ones} * {$threes} = {$ones * $threes}";
71 * 27 = 1917

You glance back down at your bag and try to remember why you brought so many adapters; there must be more than a trillion valid ways to arrange them! Surely, there must be an efficient way to count the arrangements.

What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device?

Raku

For part two, I realised an efficient soltution would be to match run-lengths of 1 separated by 3 and then multiply those combinations together.

  my @adaptors = '10-input.txt'.IO.lines.map({+$_}).sort;
  my @uppers = @adaptors;

  @adaptors.unshift(0);
  @uppers.append(@uppers.tail + 3);
  my @jolts = @uppers <<-<< @adaptors;

  my @combinations = @jolts.join.split(/'3'+/, :skip-empty).map({
  when '1111' { 7 }
  when '111' { 4 }
  when '11' { 2 }
  when '1' { 1 }
  });

  say @combinations;
  say [*] @combinations;
[2 7 7 7 4 7 2 4 4 7 7 7 7 7 4 2 7 1 7 1 4 7]
113387824750592
raku 
comments powered by Disqus