Created
December 8, 2015 15:21
-
-
Save johnboker/989a848ea884c4a1be7a 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
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