• 3 Posts
  • 2 Comments
Joined 7 days ago
cake
Cake day: December 11th, 2025

help-circle


  • C# ( c sharp )

    using System.Collections;
    using System.Collections.Generic;
    
    namespace ConsoleApp1
    {
        public static class Program
        {
            public static void Part1()
            {
                var lines = File.ReadAllLines("C:\\Users\\aman\\RiderProjects\\ConsoleApp1\\ConsoleApp1\\input.txt");
                var dialReading = 50;
                int result = 0;
                foreach (var line in lines)
                {
                    if (dialReading == 0)
                    {
                        result += 1;
                    }
                    char dir = line[0];
                    int rotation =  int.Parse(line.Substring(1));
                    if (dir == 'R')
                    {
                        dialReading += rotation;
                        dialReading %= 100;
                    }
                    else
                    {
                        int diff = dialReading - rotation;
                        if (diff > 0)
                        {
                            dialReading -= rotation;
                            dialReading %= 100;
                        }
                        else
                        {
                            dialReading = dialReading + 100 - rotation;
                            dialReading %= 100;
                        }
                    }
                }
    
                Console.WriteLine(result);
            }
    
            public static void Part2()
            {
                var lines = File.ReadAllLines("C:\\Users\\aman\\RiderProjects\\ConsoleApp1\\ConsoleApp1\\input.txt");
                var dialReading = 50;
                int result = 0;
                foreach (var line in lines)
                {
                    char dir = line[0];
                    int rotation =  int.Parse(line.Substring(1));
                    if (dir == 'R')
                    {
                        while (rotation > 0)
                        {
                            if (dialReading == 0)
                                result += 1;
                            dialReading += 1;
                            dialReading %= 100;
                            rotation -= 1;
                        }
                    }
                    else
                    {
                        while (rotation > 0)
                        {
                            if (dialReading == 0)
                                result += 1;
                            dialReading -= 1;
                            if ( dialReading < 0)
                                dialReading += 100;
                            dialReading %= 100;
                            rotation -= 1;
                        }
                    }
                }
    
                Console.WriteLine(result);
            }
            public static void Main(string[] args)
            {
                Part1();
                Part2();
            }
        }
    }