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/WarHoliday1813 Dec 02 '20
public class PasswordIssue
    {
        public List<string> Input = ...
    };

        public int Logic1()
        {
            var output = 0;
            foreach (var input in Input)
            {
                var count = 0;
                var split = input.Split(' ');
                var limits = split[0].Split('-');
                var min = Convert.ToInt32(limits[0]);
                var max = Convert.ToInt32(limits[1]);

                var letter = split[1].Trim(':');
                var st = split[2];

                foreach (var character in st)
                {
                    if (character.ToString() == letter)
                    {
                        count++;
                    }
                }

                if (count >= min && count <= max)
                {
                    output++;
                }
            }

            return output;
        }

        public int Logic2()
        {
            var output = 0;
            foreach (var input in Input)
            {
                var count = 0;
                var split = input.Split(' ');
                var limits = split[0].Split('-');
                var position1 = Convert.ToInt32(limits[0]);
                var position2 = Convert.ToInt32(limits[1]);

                var letter = split[1].Trim(':');
                var st = split[2];

                if (position2 < st.Length+1)
                {
                    if (st[position1 - 1].ToString() == letter ^ st[position2 - 1].ToString() == letter)
                    {
                        output++;
                    }
                }
            }

            return output;
        }
    }

1

u/daggerdragon Dec 02 '20

This code is really hard to read on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

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

2

u/[deleted] Dec 02 '20

I don't get why people downvote these, it's really annoying trying to parse codes with the backtick thing, so I tend to just skip things that I may have learned from.

0

u/backtickbot Dec 02 '20

Hello, WarHoliday1813: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.