ace

joined 2 years ago
[–] ace@lemmy.ananace.dev 1 points 1 year ago (1 children)

I tried to think of some clever LINQ to do this one, but was blanking entirely.

So naΓ―ve search it is.

C#

string wordsearch = "";
int width;
int height;

public void Input(IEnumerable<string> lines)
{
  wordsearch = string.Join("", lines);
  height = lines.Count();
  width = lines.First().Length;
}

public void Part1()
{
  int words = 0;
  for (int y = 0; y < height; y++)
    for (int x = 0; x < width; x++)
      words += SearchFrom(x, y);

  Console.WriteLine($"Words: {words}");
}
public void Part2()
{
  int words = 0;
  for (int y = 1; y < height - 1; y++)
    for (int x = 1; x < width - 1; x++)
      words += SearchCross(x, y);

  Console.WriteLine($"Crosses: {words}");
}

public int SearchFrom(int x, int y)
{
  char at = wordsearch[y * width + x];
  if (at != 'X')
    return 0;

  int words = 0;
  for (int ydir = -1; ydir <= 1; ++ydir)
    for (int xdir = -1; xdir <= 1; ++xdir)
    {
      if (xdir == 0 && ydir == 0)
        continue;

      if (SearchWord(x, y, xdir, ydir))
        words++;
    }

  return words;
}

private readonly string word = "XMAS";
public bool SearchWord(int x, int y, int xdir, int ydir)
{
  int wordit = 0;
  while (true)
  {
    char at = wordsearch[y * width + x];
    if (at != word[wordit])
      return false;

    if (wordit == word.Length - 1)
      return true;

    wordit++;

    x += xdir;
    y += ydir;

    if (x < 0 || y < 0 || x >= width || y >= height)
      return false;
  }
}

public int SearchCross(int x, int y)
{
  if (x == 0 || y == 0 || x == width - 1 || y == width - 1)
    return 0;

  char at = wordsearch[y * width + x];
  if (at != 'A')
    return 0;

  int found = 0;
  for (int ydir = -1; ydir <= 1; ++ydir)
    for (int xdir = -1; xdir <= 1; ++xdir)
    {
      if (xdir == 0 || ydir == 0)
        continue;

      if (wordsearch[(y + ydir) * width + (x + xdir)] != 'M')
        continue;
      if (wordsearch[(y - ydir) * width + (x - xdir)] != 'S')
        continue;

      found++;
    }

  if (found == 2)
    return 1;

  return 0;
}

[–] ace@lemmy.ananace.dev 5 points 1 year ago

I started poking at doing a proper lexer/parser, but then I thought about how early in AoC it is and how low the chance is that the second part will require proper parsing.

So therefore; hello regex my old friend, I've come to talk with you again.

C#

List<string> instructions = new List<string>();

public void Input(IEnumerable<string> lines)
{
  foreach (var line in lines)
    instructions.AddRange(Regex.Matches(line, @"mul\(\d+,\d+\)|do\(\)|don't\(\)").Select(m => m.Value));
}

public void Part1()
{
  var sum = instructions.Select(mul => Regex.Match(mul, @"(\d+),(\d+)").Groups.Values.Skip(1).Select(g => int.Parse(g.Value))).Select(cc => cc.Aggregate(1, (acc, val) => acc * val)).Sum();
  Console.WriteLine($"Sum: {sum}");
}
public void Part2()
{
  bool enabled = true;
  long sum = 0;
  foreach(var inst in instructions)
  {
    if (inst.StartsWith("don't"))
      enabled = false;
    else if (inst.StartsWith("do"))
      enabled = true;
    else if (enabled)
      sum += Regex.Match(inst, @"(\d+),(\d+)").Groups.Values.Skip(1).Select(g => int.Parse(g.Value)).Aggregate(1, (acc, val) => acc * val);
  }
  Console.WriteLine($"Sum: {sum}");
}

[–] ace@lemmy.ananace.dev 3 points 1 year ago

Of course I ended up with a off-by-one error for the second part, so things took a bit longer than they really should've.

But either way, behold, messy C#:

C#

int[][] reports = new int[0][];

public void Input(IEnumerable<string> lines)
{
  reports = lines.Select(l => l.Split(' ').Select(p => int.Parse(p)).ToArray()).ToArray();
}

public void Part1()
{
  int safeCount = reports.Where(report => CheckReport(report)).Count();
  Console.WriteLine($"Safe: {safeCount}");
}
public void Part2()
{
  int safeCount = reports.Where(report => {
    if (CheckReport(report))
      return true;

    for (int i = 0; i < report.Length; ++i)
      if (CheckReport(report.Where((_, j) => j != i)))
        return true;

    return false;
  }).Count();

  Console.WriteLine($"Safe: {safeCount}");
}

bool CheckReport(IEnumerable<int> report)
{
  var diffs = report.SkipLast(1).Zip(report.Skip(1)).Select(v => v.Second - v.First);
  return diffs.All(v => Math.Abs(v) <= 3) && (diffs.All(v => v > 0) || diffs.All(v => v < 0));
}

[–] ace@lemmy.ananace.dev 3 points 1 year ago

Not going to push hard on these first days (fever being a reason), so I slept in quite a bit before looking at the problem.

C#

List<int> _LeftList = new List<int>();
List<int> _RightList = new List<int>();

// Fed via File.ReadLines(...).Select(l => l.Trim())
public void Input(IEnumerable<string> lines)
{
  foreach (var line in lines)
  {
    var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s));
    _LeftList.Add(split.First());
    _RightList.Add(split.Last());
  }
}

public void Part1()
{
  Console.WriteLine($"Sum: {_LeftList.Order().Zip(_RightList.Order()).Select(v => Math.Abs(v.First - v.Second)).Sum()}");
}
public void Part2()
{
  Console.WriteLine($"Sum: {_LeftList.Select(l => _RightList.Where(i => i == l).Count() * l).Sum()}");
}

[–] ace@lemmy.ananace.dev 9 points 1 year ago

Remember to join the !advent_of_code@programming.dev community while you're at it

[–] ace@lemmy.ananace.dev 2 points 1 year ago* (last edited 1 year ago)

I just do the Swedish accent thing and pronounce it forge-yo (like in yo-yo, not the greeting proclamation)

[–] ace@lemmy.ananace.dev 0 points 1 year ago

Well, this has certainly caused quite a bit of drama from all sides.

I'm curious about the earlier audit of libolm which happened many years back (and by a reputable company), it feels like it should've found any potentially exploitable issues after all - including timing attacks.

[–] ace@lemmy.ananace.dev 5 points 1 year ago

That goddamn Doctor Benny's box gets me every time, the fact that they even remixed the theme to match is just glorious.

[–] ace@lemmy.ananace.dev 10 points 1 year ago

GitLab has been working on support for ActivityPub/ForgeFed federation as well, currently only implemented for releases though.

[–] ace@lemmy.ananace.dev 1 points 1 year ago* (last edited 1 year ago)

And it's still entirely unrelated to my point, since SUSE will remain the trademark in question regardless of what's actually contained in OpenSUSE.

But yes, the free/open-source spins of things tend to have somewhat differing content compared to the commercial offering, usually for licensing or support reasons.
E.g. CentOS (when it still was a real thing)/AlmaLinux/etc supporting hardware that regular RHEL has dropped support for, while also not distributing core RedHat components like the subscription manager.

[–] ace@lemmy.ananace.dev 10 points 1 year ago (2 children)

Not at all what my point was. There's indeed plenty of Open-something (or Libre-something) projects under the sun, but no free/open spins of commercial projects named simply "Open<Trademarked company name / commercial offering>".

[–] ace@lemmy.ananace.dev 8 points 1 year ago (9 children)

To be fair, OpenSUSE is the only project with a name like that, so it makes some sense that they'd want it changed.
There's no OpenRedHat, no OpenNovell, no OpenLinspire, etc.

394
Warp NaCLs (lemmy.ananace.dev)
 

I will not be taking any questions.

 

I just love the very Factorio way to get rid of surplus - just toss it over the side.

 

Just continuing with all those quality of life improvements, absolutely loving what I'm seeing.

 

Looks like it's v2 time.

The btrfs-progs -side patch is here.

105
Audio Horror (va.media.tumblr.com)
 

Looks like the Factorio devs are hard in on getting as many improvements into the game as they can in time for the DLC release.

 
 

More rail options sounds like it's going to improve the game tremendously as well, definitely looking like there'll be quite the QoL update alongside the upcoming DLC.

23
Courtesy of my neighbors. (ace-things.rgw.ctrl-c.liu.se)
 

Some more interesting takes on optimization actually, the upcoming expansion / patch continues to look really interesting.

 

All I can say is; Oh dear.

The addictive optimization game adds even more methods of optimization to play with.

view more: β€Ή prev next β€Ί