You need to cross a vast firewall. The firewall consists of several layers, each with a security scanner that moves back and forth across the layer. To succeed, you must not be detected by a scanner.
By studying the firewall briefly, you are able to record (in your puzzle input) the depth of each layer and the range of the scanning area for the scanner within it, written as depth: range
. Each layer has a thickness of exactly 1
. A layer at depth 0
begins immediately inside the firewall; a layer at depth 1
would start immediately after that.
Read the full puzzle.
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace AdventOfCode.Y2017.Day13;
class Layers : List<(int depth, int range)> {
public Layers(IEnumerable<(int depth, int range)> layers) : base(layers) {
}
}
[ProblemName("Packet Scanners")]
class Solution : Solver {
public object PartOne(string input) => Severities(Parse(input), 0).Sum();
public object PartTwo(string input) {
var layers = Parse(input);
return Enumerable
.Range(0, int.MaxValue)
.First(n => !Severities(layers, n).Any());
}
Layers Parse(string input) =>
new Layers(
from line in input.Split('\n')
let parts = Regex.Split(line, ": ").Select(int.Parse).ToArray()
select (parts[0], parts[1])
);
IEnumerable<int> Severities(Layers layers, int t) {
var packetPos = 0;
foreach (var layer in layers) {
t += layer.depth - packetPos;
packetPos = layer.depth;
var scannerPos = t % (2 * layer.range - 2);
if (scannerPos == 0) {
yield return layer.depth * layer.range;
}
}
}
}
Please ☆ my repo if you like it!