ace

joined 2 years ago
MODERATOR OF
[–] [email protected] 1 points 7 months 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;
}

[–] [email protected] 5 points 7 months 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}");
}

[–] [email protected] 4 points 7 months 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));
}

[–] [email protected] 3 points 7 months 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()}");
}

[–] [email protected] 9 points 7 months ago

Remember to join the [email protected] community while you're at it

[–] [email protected] 1 points 7 months ago

I have tried it, but it's never really clicked when I've used it.

[–] [email protected] 4 points 7 months ago

MS Outlook is the joke.

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago)

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

[–] [email protected] 1 points 7 months ago (2 children)

Been doing Ruby the last few years, since it's what I use most at work.
Might try for some C# as well though.

[–] [email protected] 4 points 10 months ago (4 children)

Been enjoying a Logitech MX Master 3S myself, it's definitely a nice mouse to handle, but it's also not something that could be called particularly small.

[–] [email protected] 0 points 10 months 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.

[–] [email protected] 5 points 11 months ago

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

433
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.

123
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.

24
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 β€Ί