01020304050607080910111213141516171819202122232425

Advent of Code

2020/2

Password Philosophy

in C#

by encse

Your flight departs in a few days from the coastal airport; the easiest way down to the coast from here is via toboggan.

The shopkeeper at the North Pole Toboggan Rental Shop is having a bad day. "Something's wrong with our computers; we can't log in!" You ask if you can take a look.

Read the full puzzle.

using System;
using System.Linq;

namespace AdventOfCode.Y2020.Day02;

record PasswordEntry(int a, int b, char ch, string password);

[ProblemName("Password Philosophy")]
class Solution : Solver {

    public object PartOne(string input) => ValidCount(input, (PasswordEntry pe) => {
        var count = pe.password.Count(ch => ch == pe.ch);
        return pe.a <= count && count <= pe.b;
    });

    public object PartTwo(string input) => ValidCount(input, (PasswordEntry pe) => {
        return (pe.password[pe.a - 1] == pe.ch) ^ (pe.password[pe.b - 1] == pe.ch);
    });

    int ValidCount(string input, Func<PasswordEntry, bool> isValid) =>
        input
            .Split("\n")
            .Select(line => {
                var parts = line.Split(' ');
                var range = parts[0].Split('-').Select(int.Parse).ToArray();
                var ch = parts[1][0];
                return new PasswordEntry(range[0], range[1], ch, parts[2]);
            })
            .Count(isValid);
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator