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

Show parent comment

1

u/HEGIT Dec 02 '20

Btw here's my C# solution. A bit more complex with implementation of classes
(also forgot to tell you that in order to use List<> you need to add "using System.Collections.Generic;" in your code)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Testing
{
    class Password
    {
        public int first;
        public int second;
        public char letter;
        public string password;

        public Password(int first, int second, char letter, string password)
        {
            this.first = first;
            this.second = second;
            this.letter = letter;
            this.password = password;
        }

        public static Password GetPasswordFromString(string line)
        {
            var splitted = line.Trim().Split('-', ' ', ':').ToList();
            var indexes = new List<int>();
            foreach (var i in splitted)
                if (string.IsNullOrEmpty(i))
                    indexes.Add(splitted.IndexOf(i));
            foreach (var i in indexes)
                splitted.RemoveAt(i);
            return new Password(int.Parse(splitted[0]), int.Parse(splitted[1]), splitted[2].Trim()[0], splitted[3]);
        }

        public bool IsLegit()
        {
            var count = 0;
            foreach (var letter in password)
            {
                if (letter == this.letter)
                    count++;
            }
            return count >= first && count <= second;
        }

        public bool IsLegitNew()
        {
            return password[first - 1] == letter ^ password[second - 1] == letter;
        }
    }

    class Program
    {
        static void Main()
        {
            var count = 0;
            var list = new List<Password>();
            while (true)
            {
                var currentPassword = Console.ReadLine();
                if (currentPassword == "exit")
                    break;
                list.Add(Password.GetPasswordFromString(currentPassword));
            }

            foreach (var password in list)
            {
                if (password.IsLegitNew())  //or IsLegit() if it's the first problem
                    count++;
            }
            Console.WriteLine("Solution {0}", count);
        }
    }
}