198 lines
5.1 KiB
C#
198 lines
5.1 KiB
C#
var exParsed = new Day04Parsed("example-input.txt");
|
|
|
|
Console.WriteLine("Example Part01: {0}", exParsed.GetPart01());
|
|
Console.WriteLine("Example Part02: {0}", exParsed.GetPart02());
|
|
|
|
var pzParsed = new Day04Parsed("puzzle-input.txt");
|
|
|
|
Console.WriteLine("Puzzle Part01: {0}", pzParsed.GetPart01());
|
|
Console.WriteLine("Puzzle Part02: {0}", pzParsed.GetPart02());
|
|
|
|
class Day04Parsed : AdventCommon.ParsedInput
|
|
{
|
|
static readonly string WORD_TO_SEARCH = "XMAS";
|
|
|
|
List<string> letterGrid = new List<string>();
|
|
|
|
public Day04Parsed(string fileName) : base(fileName)
|
|
{
|
|
}
|
|
|
|
public override int GetPart01()
|
|
{
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < letterGrid.Count; i++)
|
|
{
|
|
var row = GetRow(i);
|
|
|
|
for (int j = 0; j < row.Length; j++)
|
|
{
|
|
var words = GetWords(i, j);
|
|
count += CountWords(words, WORD_TO_SEARCH);
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
public override int GetPart02()
|
|
{
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < letterGrid.Count; i++)
|
|
{
|
|
var row = GetRow(i);
|
|
for (int j = 0; j < row.Length; j++)
|
|
{
|
|
if (IsMASX(i, j)) { count++; }
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
public bool IsMASX(int i, int j)
|
|
{
|
|
try
|
|
{
|
|
string wordToSearch = "MAS";
|
|
string rwordToSearch = new string(wordToSearch.Reverse().ToArray());
|
|
|
|
if (GetEntry(i,j) != 'A') { return false; }
|
|
|
|
string backwardSlash = String.Format("{0}{1}{2}", GetEntry(i-1,j-1), GetEntry(i,j), GetEntry(i+1,j+1));
|
|
string forwardSlash = String.Format("{0}{1}{2}", GetEntry(i-1,j+1), GetEntry(i,j), GetEntry(i+1,j-1));
|
|
|
|
if ( (String.Compare(backwardSlash, wordToSearch) == 0 || String.Compare(backwardSlash, rwordToSearch) == 0)
|
|
&& (String.Compare(forwardSlash, wordToSearch) == 0 || String.Compare(forwardSlash, rwordToSearch) == 0))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
catch (Exception) { }
|
|
|
|
return false;
|
|
}
|
|
|
|
string GetRow(int i) => letterGrid[i];
|
|
string GetColumn(int i) {
|
|
string toRet = "";
|
|
foreach (var line in letterGrid)
|
|
{
|
|
toRet += line[i];
|
|
}
|
|
return toRet;
|
|
}
|
|
|
|
char GetEntry(int i, int j) => letterGrid[i][j];
|
|
|
|
static int CountWords(List<string> words, string wordToSearch)
|
|
{
|
|
int count = 0;
|
|
|
|
string rwordToSearch = new string(wordToSearch.Reverse().ToArray());
|
|
|
|
foreach (var word in words)
|
|
{
|
|
if (String.Compare(word, wordToSearch) == 0 || String.Compare(word, rwordToSearch) == 0)
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
List<string> GetWords(int i, int j)
|
|
{
|
|
List<string> words = new List<string>();
|
|
|
|
// Scan only with X
|
|
if (GetEntry(i, j) != 'X')
|
|
{
|
|
return words;
|
|
}
|
|
|
|
var row = GetRow(i);
|
|
var col = GetColumn(j);
|
|
|
|
// Get horizontal
|
|
try
|
|
{
|
|
words.Add(row.Substring(j, WORD_TO_SEARCH.Length));
|
|
} catch (Exception) { }
|
|
|
|
// Get backwards
|
|
try
|
|
{
|
|
words.Add(row.Substring(j-(WORD_TO_SEARCH.Length-1), WORD_TO_SEARCH.Length));
|
|
} catch (Exception) { }
|
|
|
|
// Get vertical down
|
|
try
|
|
{
|
|
words.Add(col.Substring(i, WORD_TO_SEARCH.Length));
|
|
} catch (Exception) { }
|
|
|
|
// Get vertical "up"
|
|
try
|
|
{
|
|
words.Add(col.Substring(i-(WORD_TO_SEARCH.Length-1), WORD_TO_SEARCH.Length));
|
|
} catch (Exception) { }
|
|
|
|
// Top Right Diag
|
|
try
|
|
{
|
|
string diag = "";
|
|
for (int x = i, y = j; y < j+WORD_TO_SEARCH.Length; x--, y++)
|
|
{
|
|
diag += GetEntry(x, y);
|
|
}
|
|
words.Add(diag);
|
|
} catch (Exception) { }
|
|
|
|
// Down right Diag
|
|
try
|
|
{
|
|
string diag = "";
|
|
for (int x = i, y = j; y < j + WORD_TO_SEARCH.Length; x++, y++)
|
|
{
|
|
diag += GetEntry(x, y);
|
|
}
|
|
words.Add(diag);
|
|
} catch (Exception) { }
|
|
|
|
// Down left Diag
|
|
try
|
|
{
|
|
string diag = "";
|
|
for (int x = i, y = j; y > j - WORD_TO_SEARCH.Length; x++, y--)
|
|
{
|
|
diag += GetEntry(x, y);
|
|
}
|
|
words.Add(diag);
|
|
} catch (Exception) { }
|
|
|
|
// Up right Diag
|
|
try
|
|
{
|
|
string diag = "";
|
|
for (int x = i, y = j; y > j - WORD_TO_SEARCH.Length; x--, y--)
|
|
{
|
|
diag += GetEntry(x, y);
|
|
}
|
|
words.Add(diag);
|
|
}
|
|
catch (Exception) { }
|
|
|
|
return words;
|
|
}
|
|
|
|
public override bool ParseLine(string line, object? context = null)
|
|
{
|
|
letterGrid.Add(line);
|
|
return true;
|
|
}
|
|
}
|