Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
3.0 kB
7
Indexable
Never
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Day2
{
    public class MoveBase
    {
        public Move Move { get; set; }
        public int Value { get; set; }
        public Move WinTo { get; set; }
        public Move LoseTo { get; set; }
    }
    public enum Move
    {
        Rock,
        Paper,
        Scissor
    }
    internal class Program
    {
        public static List<string> Input => File.ReadAllLines(@"input.txt").ToList();
        static void Main(string[] args)
        {
            var BaseList = new[]
            {
                new { Name = "Rock", value = 1 },
                new { Name = "Paper", value = 2 },
                new { Name = "Scissor", value = 3 },
            };

            var Part1 = 0;
            var Part2 = 0;

            foreach (var item in Input)
            {
                var opponentMove = GetMove(item[0].ToString());
                var YourMove = GetMove(item[2].ToString());

                if (opponentMove.WinTo == YourMove.Move)
                {
                    Part1 += YourMove.Value;
                }
                else if (opponentMove.LoseTo == YourMove.Move)
                {
                    Part1 += YourMove.Value + 6;
                }
                else
                {
                    Part1 += YourMove.Value + 3;
                }

                if (YourMove.Move == Move.Rock)
                {
                    Part2 += BaseList.FirstOrDefault(x => x.Name.Equals(opponentMove.WinTo.ToString())).value;
                }
                else if (YourMove.Move == Move.Paper)
                {
                    Part2 += opponentMove.Value + 3;
                }
                else 
                {
                    Part2 += BaseList.FirstOrDefault(x => x.Name.Equals(opponentMove.LoseTo.ToString())).value + 6;
                }
            }
            Console.WriteLine(Part1);
            Console.WriteLine(Part2);
        }
        public static MoveBase GetMove(string c)
        {
            if (c == "A" || c == "X")
            {
                return new MoveBase()
                {
                    Move = Move.Rock,
                    Value = 1,
                    WinTo = Move.Scissor,
                    LoseTo = Move.Paper
                };
            }
            else if (c == "B" || c == "Y")
            {
                return new MoveBase()
                {
                    Move = Move.Paper,
                    Value = 2,
                    WinTo = Move.Rock,
                    LoseTo = Move.Scissor
                };
            }
            else
            {
                return new MoveBase()
                {
                    Move = Move.Scissor,
                    Value = 3,
                    WinTo = Move.Paper,
                    LoseTo = Move.Rock
                };
            }
        }
    }
}