Advent of Code 2: „Password Philosophy” – Lösungsaustausch

Das Advent of Code Puzzle „ Password Philosophy“ für Heute ist ab jetzt unter verfügbar!

In diesem Thread könnt ihr eure Lösungen posten, oder euch über das Puzzle austauschen.

Quellcode bitte in dieser Syntax posten („python“ durch die verwendete Sprache ersetzen):

```python
print("hello world")
```

Meine nicht ganz so schreckliche Lösung in C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Day2 {
    internal static class Program {
        struct Entry {
            public string Len;
            public string Chr;
            public string Pw;
            public Entry(string len, string chr, string pw) {
                this.Len = len;
                this.Chr = chr;
                this.Pw = pw;
            }
        }
        
        static void Main(string[] args) {
            var inputFile = File.ReadAllLines(args[0]);
            var inputData = new List<Entry>();
            
            foreach (var str in inputFile) {
                var line = str.Split(" ");
                inputData.Add(new Entry(
                        len: line[0],
                        chr: line[1].Replace(":", ""),
                        pw: line[2]
                        ));
            }

            Console.WriteLine($"Solving with input: \n{string.Join("\n", inputFile)}");
            Console.WriteLine($"Part 1 result: {Solve1(inputData.ToArray()).ToString()}");
            Console.WriteLine($"Part 2 result: {Solve2(inputData.ToArray()).ToString()}");
        }

        static int Solve1(Entry[] input) {
            var count = 0;
            foreach (var ent in input) {
                var minMax = ent.Len.Split("-");
                var charCount = Regex.Matches(ent.Pw, ent.Chr).Count();
                if (Convert.ToInt32(minMax[0]) <= charCount && Convert.ToInt32(minMax[1]) >= charCount) {
                    count++;
                }
            }

            return count;
        }

        static int Solve2(Entry[] input) {
            var count = 0;
            foreach (var ent in input) {
                var pos = ent.Len.Split("-");
                var matches = Regex.Matches(ent.Pw, ent.Chr);
                if (matches.Select(x => pos.Contains((x.Index + 1).ToString())).Count(n => n) == 1) {
                    Console.WriteLine($"valid pw found: {ent.Len} {ent.Chr}: {ent.Pw}");
                    count++;
                };
            }

            return count;
        }
    }
}

Python, mit RegEx

#!/usr/bin/env python3

import re


def main():
    input = parse_input("02-input.txt")
    print(f"Policy 1: {check_passwords(input)} passwords valid")
    print(f"Policy 2: {check_passwords_differently(input)} passwords valid")


def parse_input(filename: str) -> list[tuple[int, int, str, str]]:
    parsed = []
    with open(filename, "r") as file:
        for line in file:
            match = re.match(r"(\d+)-(\d+) ([a-z]): (\w+)", line)
            parsed.append(
                (int(match.group(1)), int(match.group(2)), *match.group(3, 4)))
    return parsed


def check_passwords(input: list[tuple[int, int, str, str]]) -> int:
    valid = 0
    for min, max, letter, password in input:
        if re.fullmatch(rf"[^{letter}]*({letter}[^{letter}]*){{{min},{max}}}",
                        password):
            valid += 1
    return valid


def check_passwords_differently(input: list[tuple[int, int, str, str]]) -> int:
    valid = 0
    for pos1, pos2, letter, password in input:

        def check(pos):
            return len(password) >= pos and password[pos - 1] == letter

        if check(pos1) ^ check(pos2):
            valid += 1
    return valid


if __name__ == "__main__":
    main()