Day 1: Secret Entrance

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • ghodawalaaman@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    3 days ago

    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();
            }
        }
    }