using System.Collections.Generic;
public static void Main()
string input = "https://[2001:db8:85a3::8a2e:370:7334]";
Uri uri = new Uri(input);
string output = AdjustIPv6Address(input);
Console.WriteLine(output);
Console.WriteLine(uri.AbsoluteUri);
private static string AdjustIPv6Address(string uriText)
string address = ExtractAddressInSquareBrackets(uriText);
string[] components = GetAddressComponents(address);
string[] adjustedComponents = SquashFirstLongestZeroSequence(components);
string adjustedAddress = string.Join(":", adjustedComponents);
string adjustedUriText = InjectAdjustedAddress(uriText, adjustedAddress);
private static string ExtractAddressInSquareBrackets(string uriText)
string[] parts = uriText.Split('[', ']');
private static string[] GetAddressComponents(string ipv6Address)
string[] components = ipv6Address.Split(':');
string[] adjustedComponents = new string[components.Length];
for (int i = 0; i < components.Length; i++)
adjustedComponents[i] = AdjustAddressComponent(components[i]);
return adjustedComponents;
private static string AdjustAddressComponent(string component)
long number = Convert.ToInt64(component, 16);
string adjusted = number.ToString("x");
private static string InjectAdjustedAddress(string uriText, string adjustedAddress)
string[] parts = uriText.Split('[', ']');
string adjustedUriText = parts[0] + "[" + adjustedAddress + "]" + parts[2];
private static string[] SquashFirstLongestZeroSequence(string[] components)
List<(int, int)> zeroSequences = FindZeroSequences(components);
int lengthOfLongest = GetLengthOfLongestSequence(zeroSequences);
(int, int) firstLongest = SelectFirstLongestSequence(zeroSequences, lengthOfLongest);
string[] adjustedComponents = ReplaceLongestZeroSequenceWithEmptyString(components, firstLongest.Item1, firstLongest.Item2);
return adjustedComponents;
string[] clonedComponents = new string[components.Length];
Array.Copy(components, clonedComponents, components.Length);
private static List<(int, int)> FindZeroSequences(string[] components)
List<(int, int)> sequences = new List<(int, int)>();
for (int i = 0; i < components.Length; i++)
if (components[i] == "0")
sequences.Add((startIndex, zeroCount));
sequences.Add((startIndex, zeroCount));
private static int GetLengthOfLongestSequence(List<(int, int)> sequences)
foreach ((int, int) sequence in sequences)
if (sequence.Item2 > maxLength)
maxLength = sequence.Item2;
private static (int, int) SelectFirstLongestSequence(List<(int, int)> sequences, int maxLength)
foreach ((int, int) sequence in sequences)
if (sequence.Item2 == maxLength)
private static string[] ReplaceLongestZeroSequenceWithEmptyString(string[] components, int startIndex, int zeroCount)
string[] adjustedComponents = new string[components.Length - (zeroCount - 1)];
for (int i = 0; i < startIndex; i++)
adjustedComponents[i] = components[i];
adjustedComponents[startIndex] = string.Empty;
for (int i = startIndex + zeroCount; i < components.Length; i++)
adjustedComponents[i - (zeroCount - 1)] = components[i];
return adjustedComponents;