Zad 4.1

mail@pastecode.io avatar
unknown
csharp
7 months ago
3.7 kB
1
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdventTests
{
    public static class Zad4
    {
        public static void Calculate()
        {
            //var test = Data.Zad4;
            var test = @"Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1
Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11";



            int result = 0;

            using (StringReader reader = new StringReader(Data.Zad4))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string game = new string(line.Skip(line.IndexOf(':') + 1).ToArray());
                    string[] winning = game.Split("|")[0].Split(' ').Where(x => !string.IsNullOrEmpty(x)).ToArray();
                    string[] mine = game.Split("|")[1].Split(' ').Where(x => !string.IsNullOrEmpty(x)).ToArray();

                    int cardWorth = 0;
                    foreach (var num1 in mine)
                    {
                        foreach (var num2 in winning)
                        {
                            if (num1.Equals(num2))
                            {
                                if (cardWorth == 0)
                                {
                                    cardWorth = 1;
                                }
                                else
                                {
                                    cardWorth = cardWorth * 2;
                                }
                            }
                        }
                    }

                    if (cardWorth > 0)
                    {
                        result += cardWorth;
                    }
                }

                Console.WriteLine(result);
            }
        }

        public static void Calculate2()
        {
            //var test = Data.Zad4;
            var test = @"Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1
Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11";



            int result = 0;



            using (StringReader reader = new StringReader(Data.Zad4))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string game = new string(line.Skip(line.IndexOf(':') + 1).ToArray());
                    string[] winning = game.Split("|")[0].Split(' ').Where(x => !string.IsNullOrEmpty(x)).ToArray();
                    string[] mine = game.Split("|")[1].Split(' ').Where(x => !string.IsNullOrEmpty(x)).ToArray();

                    int cardWorth = 0;
                    foreach (var num1 in mine)
                    {
                        foreach (var num2 in winning)
                        {
                            if (num1.Equals(num2))
                            {
                                cardWorth ++;
                            }
                        }
                    }


                    //if (cardWorth > 0)
                    //{
                    //    result += cardWorth;
                    //}
                }

                Console.WriteLine(result);
            }
        }
    }
}
Leave a Comment