Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace AdventOfCodeDay8
{
class Program
{
static void Main(string[] args)
{
var sr = new StreamReader("../../input1.txt"); // select part one or two here.
string line;
var total1 = 0;
var total2 = 0;
while ((line = sr.ReadLine()) != null)
{
total1 += ((line.Length - Regex.Unescape(line).Length) + 2);
var part2 = "\"" + Escape(line) + "\"";
total2 += (part2.Length - line.Length);
}
Console.WriteLine($"Part 1 Extra Characters: {total1}");
Console.WriteLine($"Part 2 Extra Characters: {total2}");
}
private static string Escape(string s)
{
var sb = new StringBuilder();
foreach (var c in s)
{
switch (c)
{
case '"':
sb.Append(@"\""");
break;
case '\\':
sb.Append("\\\\");
break;
default:
sb.Append(c);
break;
}
}
return sb.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.