01020304050607080910111213141516171819202122232425

Advent of Code

2018/2

Inventory Management System

in C#

by encse

You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.

Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"

Read the full puzzle.

using System.Linq;

namespace AdventOfCode.Y2018.Day02;

[ProblemName("Inventory Management System")]
class Solution : Solver {

    public object PartOne(string input) {
        var doubles = (
            from line in input.Split("\n")
            where CheckLine(line, 2)
            select line
        ).Count();
        var tripples = (
            from line in input.Split("\n")
            where CheckLine(line, 3)
            select line
        ).Count();
        return doubles * tripples;
    }

    bool CheckLine(string line, int n) {
        return (from ch in line
                group ch by ch into g
                select g.Count()).Any(cch => cch == n);
    }

    public object PartTwo(string input) {
        var lines = input.Split("\n");
        return (from i in Enumerable.Range(0, lines.Length)
                from j in Enumerable.Range(i + 1, lines.Length - i - 1)
                let line1 = lines[i]
                let line2 = lines[j]
                where Diff(line1, line2) == 1
                select Common(line1, line2)
        ).Single();
    }

    int Diff(string line1, string line2) {
        return line1.Zip(line2, 
            (chA, chB) => chA == chB
        ).Count(x => x == false);
    }

    string Common(string line1, string line2) {
        return string.Join("", line1.Zip(line2, (chA, chB) => chA == chB ? chA.ToString() : ""));
    }
}

Please ☆ my repo if you like it!

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