r/adventofcode • • Dec 02 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-

--- Day 2: Password Philosophy ---


Advent of Code 2020: Gettin' Crafty With It


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:02:31, megathread unlocked!

99 Upvotes

1.2k comments sorted by

View all comments

1

u/[deleted] Dec 02 '20
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Advent2_1(@"C:\temp\input2.txt"));
            Console.WriteLine(Advent2_2(@"C:\temp\input2.txt"));
        }

        private static int Advent2_1(string inputPath)
        {
            using var reader = new StreamReader(inputPath);
            var passCounter = 0;

            string inputLine;
            while ((inputLine = reader.ReadLine()) is not null)
            {
                var splittedInput = inputLine.Split(Char.Parse(":"), StringSplitOptions.RemoveEmptyEntries);
                (string startRange, string endRange, string passChar, string password) seperatedLine = (
                    splittedInput[0].Substring(0, splittedInput[0].IndexOf("-")),
                    splittedInput[0].Substring(splittedInput[0].IndexOf("-") +1, splittedInput[0].Length - splittedInput[0].LastIndexOf(" ")), 
                    splittedInput[0].Substring(splittedInput[0].LastIndexOf(" ")).Trim(),
                    splittedInput[1].Trim());

                var tmpCounter = 0;
                for (int i = 0; i < seperatedLine.password.Length; i++)
                {
                    if (seperatedLine.password[i].ToString() == seperatedLine.passChar)
                        tmpCounter++;
                }
                if (tmpCounter >= Convert.ToInt32(seperatedLine.startRange) &&
                    tmpCounter <= Convert.ToInt32(seperatedLine.endRange))
                    passCounter++;
            }
            return passCounter;
        }
        
        private static int Advent2_2(string inputPath)
        {
            using var reader = new StreamReader(inputPath);
            var passCounter = 0;

            string inputLine;
            while ((inputLine = reader.ReadLine()) is not null)
            {
                var splittedInput = inputLine.Split(Char.Parse(":"), StringSplitOptions.RemoveEmptyEntries);
                (string startRange, string endRange, string passChar, string password) seperatedLine = (
                    splittedInput[0].Substring(0, splittedInput[0].IndexOf("-")),
                    splittedInput[0].Substring(splittedInput[0].IndexOf("-") +1, splittedInput[0].Length - splittedInput[0].LastIndexOf(" ")), 
                    splittedInput[0].Substring(splittedInput[0].LastIndexOf(" ")).Trim(),
                    splittedInput[1].Trim());
                
                if (seperatedLine.password[Convert.ToInt32(seperatedLine.startRange)-1].ToString() == seperatedLine.passChar ^ seperatedLine.password[Convert.ToInt32(seperatedLine.endRange)-1].ToString() == seperatedLine.passChar)
                    passCounter++;
            }
            return passCounter;
        }
    }
}

Quick'n'dirty during lunch..

1

u/daggerdragon Dec 02 '20

Please add the language to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

(looks like Linq? C#?)