using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Day2Part1
{
class Program
{
static void Main(string[] args)
{
string[] inputTxt = File.ReadAllLines(@"C:\Users\Ben\Desktop\adventOfCode2018\Day 2\input.txt");
List<int> TwoMatchList = new List<int>();// List of id's to find box in the input array
List<int> ThreeMatchList = new List<int>();// list of id's to find box in the input array
foreach (string boxID in inputTxt)
{
int id = Array.IndexOf(inputTxt, boxID);
var distinctLetters = boxID.Select(x => x).Distinct().ToList();
foreach (char letter in distinctLetters)
{
var matches = boxID.Where(x => x == letter).ToList();
switch (matches.Count)
{
case 2:
//if there's exactly 2, and it's not already in the list then add it to the TwoMatchList
if (!TwoMatchList.Contains(id))
{
TwoMatchList.Add(id);
}
break;
case 3:
//if there's exactly 3, and it's not already in the list then add it to the ThreeMatchList
if (!ThreeMatchList.Contains(id))
{
ThreeMatchList.Add(id);
}
break;
default:
//don't add to any list
break;
}//end switch
}//end foreach letter
}//end foreach boxID
int checksum = TwoMatchList.Count * ThreeMatchList.Count;
Console.WriteLine("Number of boxes with exactly two matching letters {0}", TwoMatchList.Count);
Console.WriteLine("Number of boxes with exactly three matching letters {0}", ThreeMatchList.Count);
Console.WriteLine("Checksum: {0}", checksum);
Console.WriteLine();
Console.WriteLine("Now for part two!");
//empty Dictionary to hold any boxes we find and the index of the letter that's different
Dictionary<string, int> savedIDsAndIndexes = new Dictionary<string, int>();
//empty string to hold the answer
string answer = "";
//Loop through to test and find correct boxes ==>> this could be a foreach loop
for (int i = 0; i < inputTxt.Length; i++)
{
string boxID = inputTxt[i];
for (int j = inputTxt.Length - 1; j >= 0; j--)
{
List<int> mismatchIndexes = new List<int>();
string testBoxID = inputTxt[j];
for (int k = 0; k < boxID.Length; k++)
{
if (boxID[k] != testBoxID[k])
{
mismatchIndexes.Add(k);
}
}
if (mismatchIndexes.Count == 1)
{
savedIDsAndIndexes.Add(boxID, mismatchIndexes.First());
}
}
}
Console.WriteLine("number of correct boxes found: {0}", savedIDsAndIndexes.Count);
answer = savedIDsAndIndexes.Keys.First().Substring(0, savedIDsAndIndexes.Values.First())
+ savedIDsAndIndexes.Keys.First().Substring(savedIDsAndIndexes.Values.First() + 1);
Console.WriteLine("here are the ids:");
foreach (var id in savedIDsAndIndexes)
{
Console.WriteLine(id.Key);
}
Console.WriteLine("The common characters are:\n{0}", answer);
Console.WriteLine("saved index: {0}", savedIDsAndIndexes.Values.First());
}
}
}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's my C# .Net solution: