Last active
December 5, 2024 06:12
-
-
Save jbevain/bca4ba1fe3c59c097d85b2d9c327ae37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Numerics; | |
using System.Runtime.InteropServices; | |
using var reader = new StreamReader("input.txt"); | |
List<int> left = [], right = []; | |
while (reader.ReadLine() is string line) | |
{ | |
var lineSpan = line.AsSpan(); | |
var l = lineSpan[0..line.IndexOf(' ')]; | |
var r = lineSpan[(line.LastIndexOf(' ') + 1)..]; | |
left.Add(int.Parse(l)); | |
right.Add(int.Parse(r)); | |
} | |
left.Sort(); | |
right.Sort(); | |
ReadOnlySpan<int> ls = CollectionsMarshal.AsSpan(left); | |
ReadOnlySpan<int> rs = CollectionsMarshal.AsSpan(right); | |
int distance = 0; | |
for (int i = 0; i < left.Count; i += Vector<int>.Count) | |
{ | |
distance += Vector.Sum( | |
Vector.Abs( | |
Vector.Subtract( | |
new Vector<int>(ls[i..(i + Vector<int>.Count)]), | |
new Vector<int>(rs[i..(i + Vector<int>.Count)])))); | |
} | |
Console.WriteLine(distance); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment