--- Day 3: Memory Corruption ---

December 3

Listening to: Brian Eno - New Space Music


I did three things to make it easier for me for now:

  1. Implement a settings.json to include login info, day/year info, etc.
  2. Fetch input automatically from the AoC website
  3. Switch to the current day's problem

For 2 I added some basic file handling to check for a local cached copy; otherwise check the AoC website for the current input based on the supplied session information

public static async Task<string[]> GetInput(int year, int day)
{
    string filename = $"{inputDirectory}/{day:D2}.txt";
    if (File.Exists(filename))
    {
        return await File.ReadAllLinesAsync(filename);
    }

    var baseAddress = new Uri("https://adventofcode.com/");
    var cookies = new CookieContainer();

    using var handler = new HttpClientHandler() { CookieContainer = cookies };
    using var client = new HttpClient(handler) { BaseAddress = baseAddress };
    cookies.Add(baseAddress, new Cookie("session", Settings.SessionCookie));

    var inputData = await client.GetStringAsync($"{year}/day/{day}/input");

    string[] result = inputData.Split('\n').Where(s => !string.IsNullOrEmpty(s)).ToArray();
    if (result.Count() > 0)
    {
        Directory.CreateDirectory(exampleDirectory);
        await File.WriteAllLinesAsync(filename, result);
    }

    return result;
}

For 3, I did a little introspection; as long as I name my classes in the format of DayXX and make sure they implement IProblem it should all run smoothly.

var type = Type.GetType($"AOC.Y2024.Day{Settings.Day:D2}");
var dayProblem = (IProblem)Activator.CreateInstance(type);

I'm not sure what else I could add to this to make it easier for me, but maybe it'll come to me for future problems?


This is such obvious regex bait. The wording of it being exact is pretty obvious. Using an online tool it's pretty easy to construct a regex to handle this.

private readonly string mulPattern = @"(?:mul\(\d+\,\d+\))";
var matches = Regex.Matches(memory, mulPattern);
private int ParseMulString(string s)
{
    var pair = s.ToString().Substring("mul(".Length).Replace(")", "").Split(',');

    return int.Parse(pair[0]) * int.Parse(pair[1]);
}

An interesting twist. I should be able to do a regex and simply do some checking on the match itself.

private readonly string mulSwitchPattern = @"(?:mul\(\d+\,\d+\))|(?:do\(\))|(?:don't\(\))";

var matches = Regex.Matches(memory, mulSwitchPattern);

int sum = 0;
bool enabled = true;
foreach (var match in matches)
{
    var matchString = match.ToString() ?? string.Empty;
    switch (matchString)
    {
        case "do()": enabled = true; break;
        case "don't()": enabled = false; break;
        default: if (enabled) { sum += ParseMulString(matchString); } break;
    }
}

Time taken: 25 minutes

My solutions for today's puzzles