[csharp] day2-adventcode

Viewer

copydownloadembedprintName: day2-adventcode
  1. using Day2;
  2.  
  3. public static class Program
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         var inputs = File.ReadAllLines($"input.txt");
  8.         var games = GetGames(inputs);
  9.         CalculateScores(games);
  10.         Console.WriteLine("Punkty gracza: " + games.Sum(=> x.ScorePlayer));
  11.     }
  12.  
  13.     private static List<Game> GetGames(string[] inputs)
  14.     {
  15.         var games = new List<Game>();
  16.         foreach (var input in inputs)
  17.         {
  18.             var splitted = input.Split(" ");
  19.             var game = new Game
  20.             {
  21.                 Opponent = splitted[0],
  22.                 PlayerMove = splitted[1]
  23.             };
  24.             games.Add(game);
  25.         }
  26.         return games;
  27.     }
  28.  
  29.     private static void CalculateScores(List<Game> games)
  30.     {
  31.         foreach (var game in games)
  32.         {
  33.             SetPlayerMove(game);
  34.             game.ScorePlayer = GetScoreForGame(game);
  35.         }
  36.     }
  37.  
  38.     private static void SetPlayerMove(Game game)
  39.     {
  40.         switch (game.PlayerMove)
  41.         {
  42.             case "X": //przegrać
  43.                 if (game.Opponent == "A")
  44.                     game.Player = "Z";
  45.                 else if (game.Opponent == "B")
  46.                     game.Player = "X";
  47.                 else if (game.Opponent == "C")
  48.                     game.Player = "Y";
  49.                 break;
  50.             case "Y": //remis
  51.                 if (game.Opponent == "A")
  52.                     game.Player = "X";
  53.                 else if (game.Opponent == "B")
  54.                     game.Player = "Y";
  55.                 else if (game.Opponent == "C")
  56.                     game.Player = "Z";
  57.                 break;
  58.             case "Z": //wygrać
  59.                 if (game.Opponent == "A")
  60.                     game.Player = "Y";
  61.                 else if (game.Opponent == "B")
  62.                     game.Player = "Z";
  63.                 else if (game.Opponent == "C")
  64.                     game.Player = "X";
  65.                 break;
  66.             default:
  67.                 break;
  68.         }
  69.     }
  70.  
  71.     private static int GetScoreForGame(Game game)
  72.     {
  73.         var score = 0;
  74.         score += GetScoreForOption(game.Player);
  75.         score += GetScoreForBattle(game);
  76.         return score;
  77.     }
  78.  
  79.     private static int GetScoreForOption(string option)
  80.     {
  81.         switch (option)
  82.         {
  83.             case "A":
  84.             case "X":
  85.                 return 1;
  86.             case "B":
  87.             case "Y":
  88.                 return 2;
  89.             case "C":
  90.             case "Z":
  91.                 return 3;
  92.             default:
  93.                 return 0;
  94.         }
  95.     }
  96.  
  97.     private static int GetScoreForBattle(Game game)
  98.     {
  99.         string player = game.Player;
  100.         string opponent = game.Opponent;
  101.  
  102.         if (IsDraw(player, opponent))
  103.             return 3;
  104.         else if ((player == "X" && opponent == "C") || (player == "Y" && opponent == "A") || (player == "Z" && opponent == "B"))
  105.             return 6;
  106.  
  107.         return 0;
  108.     }
  109.  
  110.     private static bool IsDraw(string player, string opponent)
  111.     {
  112.         if (player == "X" && opponent == "A")
  113.             return true;
  114.         else if (player == "Y" && opponent == "B")
  115.             return true;
  116.         if (player == "Z" && opponent == "C")
  117.             return true;
  118.  
  119.         return false;
  120.     }
  121. }

Editor

You can edit this paste and save as new:


File Description
  • day2-adventcode
  • Paste Code
  • 02 Dec-2022
  • 3.3 Kb
You can Share it: