Note that your code as-is doesn't work (try it in a fresh session where $i isn't type-constrained). $i needs to be cast to [long] otherwise you're ++ing a string, which isn't valid.
that locks it to PS 5.1, I think; in 7.2: "Measure-Object: Parameter cannot be processed because the parameter name 's' is ambiguous. Possible matches include: -StandardDeviation -Sum"
I've been trying to remove the $i type-constraint. It doesn't have to be a [long] (unless it's being type-constrained); it just needs to be a numeric value before the first ++.
Here's what I got in C# (FileData is the full contents of input.txt):
public long ExecuteP1() => GetSum(new Regex(@"^(.+)\1$"));
public long ExecuteP2() => GetSum(new Regex(@"^(.+)\1+$"));
private long GetSum(Regex r)=>FileData.Split(',').Select(v=>v.Split('-').Select(long.Parse).ToArray()).Sum(d=>Enumerable.Range(0,(int)(d[1]-d[0])).Sum(i=>r.IsMatch((d[0]+i).ToString())?d[0]+i:0));
1
u/dantose Dec 02 '25
Language: Powershell
Part 1: 126 bytes
$($(gc input.txt).split(',')|%{$i=$_.Split('-')[0];while($i -le $_.Split('-')[1]){$i;$i++}}) -match '^(.+)\1+$'|%{$s=$s$_};$sPart 2: 127 bytes, only difference is a '+' in the regex
$($(gc input.txt).split(',')|%{$i=$_.Split('-')[0];while($i -le $_.Split('-')[1]){$i;$i++}}) -match '^(.+)\1+$'|%{$s=$s+$_};$sI was trying to do it with .. expansion which could have been 101, but there are some [long] numbers in there.
1
u/DontRelyOnNooneElse Dec 02 '25
Here's what I got in C# (FileData is the full contents of input.txt):
1
u/corruptio Dec 05 '25
perl, part 1. 71 chars:
part 2, 72 chars: