Created
December 2, 2020 14:12
-
-
Save Naravia/81ca7e5bf0e6a201e4b8006d95191e77 to your computer and use it in GitHub Desktop.
This file contains 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
(int x, int y) Naive(int target, List<int> values) | |
{ | |
for (var i = 0; i < values.Count; ++i) | |
{ | |
for (var j = i + 1; j < values.Count; ++j) | |
{ | |
if (values[i] + values[j] == target) | |
{ | |
return (values[i], values[j]); | |
} | |
} | |
} | |
throw new Exception($"Failed to find 2 numbers that sum to {target}."); | |
} | |
(int x, int y) Optimized(bool preSorted, int target, List<int> values) | |
{ | |
// Sorting is a requirement for this method. | |
if (!preSorted) | |
{ | |
values.Sort(); | |
} | |
int iPtr = 0, jPtr = values.Count - 1; | |
int MoveJPtr() => --jPtr >= 0 ? jPtr : throw new IndexOutOfRangeException($"{nameof(jPtr)} went out of range."); | |
int MoveIPtr() => ++iPtr <= values.Count ? iPtr : throw new IndexOutOfRangeException($"{nameof(iPtr)} went out of range."); | |
while (iPtr < jPtr) | |
{ | |
// If we're too big, get smaller. | |
while (values[iPtr] + values[jPtr] > target) | |
{ | |
MoveJPtr(); | |
} | |
while (values[iPtr] + values[jPtr] < target) | |
{ | |
MoveIPtr(); | |
} | |
if (values[iPtr] + values[jPtr] == target) | |
{ | |
return (values[iPtr], values[jPtr]); | |
} | |
} | |
throw new Exception($"Gave up at {values[iPtr]}, {values[jPtr]} - failed to find 2 numbers that sum to {target}."); | |
} | |
var input = File.ReadAllLines(@"C:\Code\Advent2020Scratchpad\Day1Part1Input.txt") | |
.Select(int.Parse) | |
.ToList(); | |
var target = 2020; | |
try | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var result = Naive(target, input); | |
sw.Stop(); | |
result.Dump($"Naive finished in {sw.ElapsedTicks} ticks"); | |
} | |
catch (Exception ex) | |
{ | |
ex.Message.Dump("Naive failed."); | |
} | |
try | |
{ | |
var oddInput = input.Where(i => i % 2 == 1).ToList(); | |
oddInput.Sort(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var result = Optimized(true, target, oddInput); | |
sw.Stop(); | |
result.Dump($"Odd Optimized finished in {sw.ElapsedTicks} ticks"); | |
} | |
catch (Exception ex) | |
{ | |
ex.Message.Dump("Odd Optimized failed."); | |
} | |
try | |
{ | |
var evenInput = input.Where(i => i % 2 == 0).ToList(); | |
evenInput.Sort(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var result = Optimized(true, target, evenInput); | |
sw.Stop(); | |
result.Dump($"Even Optimized finished in {sw.ElapsedTicks} ticks"); | |
} | |
catch (Exception ex) | |
{ | |
ex.Message.Dump("Even Optimized failed."); | |
} | |
try | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var result = Optimized(false, target, input); | |
sw.Stop(); | |
result.Dump($"Unsorted Optimized finished in {sw.ElapsedTicks} ticks"); | |
} | |
catch (Exception ex) | |
{ | |
ex.Message.Dump("Unsorted Optimized failed."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment