Day 8: Resonant Collinearity

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 3 points 2 years ago (2 children)

Dart

This really does feel like a weekend break this year, maybe Eric and co have begun to realise that family time is more precious than work time :-)

import 'dart:math';
import 'package:more/more.dart';

solve(List<String> lines, int min, int max) {
  var map = ListMultimap<String, Point<int>>();
  for (var r in lines.indices()) {
    for (var ci in lines[r].split('').indexed()) {
      if (ci.value != '.') map[ci.value].add(Point(ci.index, r));
    }
  }
  var anti = <Point<int>>{};
  for (var k in map.keys) {
    for (var p in map[k].combinations(2, repetitions: false)) {
      var diff = p.last - p.first;
      for (var m in min.to(max)) {
        anti.addAll([p.first - diff * m, p.last + diff * m]);
      }
    }
  }

  return anti.count((e) =>
      e.x.between(0, lines.first.length - 1) &&
      e.y.between(0, lines.length - 1));
}

part1(List<String> lines) => solve(lines, 1, 2);

part2(List<String> lines) => solve(lines, 0, 50);
  • source
  • hideshow 2 child comments
  • [โ€“] 4 points 2 years ago* (1 child)

    maybe Eric and co have begun to realise that family time is more precious than work time

    Last year the difficulty was fluctuating from 0 to 100 each day.
    This year all problems so far are suspiciously easy. Maybe the second half of the month will be extra hard?

  • source
  • parent
  • hideshow 1 child comment