Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.8 kB
3
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;

namespace Day_4
{

    public class Program
    {
        public class Board
        {
            public bool Bingo { get; set; }
            public List<List<string>> Rows { get; set; }
            public List<List<string>> Colums { get; set; }
        }
        public static List<string> input => System.IO.File.ReadAllLines(@"input.txt").ToList();
        public static List<string> TestInput => System.IO.File.ReadAllLines(@"TestInput.txt").ToList();


        public static List<string> BingoNumbers => input[0].Split(",").ToList();
        public static List<Board> Boards => GetBoards();

        static void Main(string[] args)
        {
            var NewBingoBoard = Boards.ToList();
            var drawnNumbers = new List<string>();
            var winningSums = new List<int>();

            foreach (var currentBingoNumber in BingoNumbers)
            {
                drawnNumbers.Add(currentBingoNumber);
                foreach (var board in NewBingoBoard.Where(x => !x.Bingo))
                {
                    GetSum(board, board.Colums, drawnNumbers, currentBingoNumber);
                    GetSum(board, board.Rows, drawnNumbers, currentBingoNumber);
                }
            }
            Console.ReadLine();
        }
        public static List<string> GetSum(Board board, List<List<string>> direction, List<string> drawnNumbers, string currentBingoNumber)
        {
            var sumsToReturn = new List<string>();
            foreach (var line in direction)
            {
                if (!line.Except(drawnNumbers).Any())
                {
                    board.Bingo = true;
                    var sumRow = GetUnmarkedSum(board, drawnNumbers) * Convert.ToInt32(currentBingoNumber);
                    if (sumRow != 0)
                    {
                        Console.WriteLine(sumRow.ToString());
                    }
                }
            }
            return null;
        }
        public static List<Board> GetBoards()
        {
            var InputRows = input.Where(x => x != "").Skip(1).Select(x => x.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s)).ToList()).ToList();
            var ListOfBoards = new List<Board>();
            var TempRange = 0;
            for (int i = 0; i < InputRows.Count / 5; i++)
            {
                var Board = new Board()
                {
                    Rows = InputRows.GetRange(TempRange, 5),
                    Colums = GetColums(InputRows, TempRange)
                };
                ListOfBoards.Add(Board);
                TempRange = TempRange + 5;
            }
            return ListOfBoards;
        }
        public static List<List<string>> GetColums(List<List<string>> inputRows, int tempRange)
        {
            var ColumsList = new List<List<string>>();
            for (int i = 0; i < 5; i++)
            {
                ColumsList.Add(inputRows.Skip(tempRange).Select(x => x[i]).Take(5).ToList());
            }
            return ColumsList;
        }
        public static int GetUnmarkedSum(Board board, List<string> drawnNumbers)
        {
            var allNumbers = new List<string>();
            foreach (var row in board.Rows)
            {
                foreach (var number in row)
                {
                    if (!drawnNumbers.Contains(number))
                    {
                        allNumbers.Add(number);
                    }
                }
            }
            var sumOfAllUnmarkedNumbers = allNumbers.Select(int.Parse).Sum();
            return sumOfAllUnmarkedNumbers;

        }
        
    }



}