[csharp] day2-adventcode
Viewer
- using Day2;
- public static class Program
- {
- public static void Main(string[] args)
- {
- var inputs = File.ReadAllLines($"input.txt");
- var games = GetGames(inputs);
- CalculateScores(games);
- Console.WriteLine("Punkty gracza: " + games.Sum(x => x.ScorePlayer));
- }
- private static List<Game> GetGames(string[] inputs)
- {
- var games = new List<Game>();
- foreach (var input in inputs)
- {
- var splitted = input.Split(" ");
- var game = new Game
- {
- Opponent = splitted[0],
- PlayerMove = splitted[1]
- };
- games.Add(game);
- }
- return games;
- }
- private static void CalculateScores(List<Game> games)
- {
- foreach (var game in games)
- {
- SetPlayerMove(game);
- game.ScorePlayer = GetScoreForGame(game);
- }
- }
- private static void SetPlayerMove(Game game)
- {
- switch (game.PlayerMove)
- {
- case "X": //przegrać
- if (game.Opponent == "A")
- game.Player = "Z";
- else if (game.Opponent == "B")
- game.Player = "X";
- else if (game.Opponent == "C")
- game.Player = "Y";
- break;
- case "Y": //remis
- if (game.Opponent == "A")
- game.Player = "X";
- else if (game.Opponent == "B")
- game.Player = "Y";
- else if (game.Opponent == "C")
- game.Player = "Z";
- break;
- case "Z": //wygrać
- if (game.Opponent == "A")
- game.Player = "Y";
- else if (game.Opponent == "B")
- game.Player = "Z";
- else if (game.Opponent == "C")
- game.Player = "X";
- break;
- default:
- break;
- }
- }
- private static int GetScoreForGame(Game game)
- {
- var score = 0;
- score += GetScoreForOption(game.Player);
- score += GetScoreForBattle(game);
- return score;
- }
- private static int GetScoreForOption(string option)
- {
- switch (option)
- {
- case "A":
- case "X":
- return 1;
- case "B":
- case "Y":
- return 2;
- case "C":
- case "Z":
- return 3;
- default:
- return 0;
- }
- }
- private static int GetScoreForBattle(Game game)
- {
- string player = game.Player;
- string opponent = game.Opponent;
- if (IsDraw(player, opponent))
- return 3;
- else if ((player == "X" && opponent == "C") || (player == "Y" && opponent == "A") || (player == "Z" && opponent == "B"))
- return 6;
- return 0;
- }
- private static bool IsDraw(string player, string opponent)
- {
- if (player == "X" && opponent == "A")
- return true;
- else if (player == "Y" && opponent == "B")
- return true;
- if (player == "Z" && opponent == "C")
- return true;
- return false;
- }
- }
Editor
You can edit this paste and save as new: