Untitled
unknown
plain_text
4 months ago
3.5 kB
3
Indexable
using System.Collections; using System.Diagnostics.Contracts; using System.Text.RegularExpressions; using System.Xml.Schema; class Day4 { private static String inputPath = @"..\..\..\input_4.txt"; private static Regex xmasPattern = new Regex("XMAS"); private static Regex samxPattern = new Regex("SAMX"); private static char[][] parseInput() { char [][] ?grid = null; int? dimension = null; using (StreamReader sr = File.OpenText(inputPath)) { String line; int lineIdx = 0; while ((line = sr.ReadLine()) != null) { dimension ??= line.Length; grid ??= new char[(int)dimension][]; grid[lineIdx] = line.ToCharArray(); lineIdx ++; } } return grid; } public static void printGrid(char[][] grid) { for (int row = 0; row < grid.Length; row++) { Console.WriteLine(new String(grid[row])); } } public static IEnumerable<String> iterateDiagonal(char [][] grid, bool alternative = false) { var result = ""; for (int row = 0; row < grid.Length; row++) { var r = row; var c = 0; while (r >= 0) { result += grid[r][alternative ? grid.Length - c - 1: c]; r --; c ++; } yield return result; result = ""; } result = ""; var row2 = grid.Length - 1; var col = 1; while (col < grid[0].Length) { var r = row2; var c = col; while (c < grid[0].Length) { result += grid[r][alternative ? grid.Length - c - 1: c]; r --; c ++; } col++; yield return result; result = ""; } } public static IEnumerable<String> iterateHorizontal(char [][] grid) { for (int row = 0; row < grid.Length; row ++) { yield return new string(grid[row]); } } public static IEnumerable<String> iterateVertical(char [][] grid) { var columnStr = ""; for (int col = 0; col < grid[0].Length; col ++) { for (int row = 0; row < grid.Length; row ++) { columnStr += grid[row][col]; } yield return columnStr; columnStr = ""; } } public static int part1() { var result = 0; var grid = parseInput(); foreach(var line in iterateVertical(grid)) result += countXMASOcurrences(line); foreach(var line in iterateHorizontal(grid)) result += countXMASOcurrences(line); foreach(var line in iterateDiagonal(grid)) result += countXMASOcurrences(line); foreach(var line in iterateDiagonal(grid, true)) result += countXMASOcurrences(line); return result; } public static int countXMASOcurrences(String input) { return xmasPattern.Matches(input).Count + samxPattern.Matches(input).Count; } }
Editor is loading...
Leave a Comment