Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • Ananace@lemmy.ananace.dev
    link
    fedilink
    arrow-up
    1
    ·
    17 hours ago

    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;
    }
    
    • CameronDevOPM
      link
      fedilink
      arrow-up
      1
      ·
      17 hours ago

      I haven’t quite started yet, and this one does feel like a busy work kinda problem. I was wondering if I could write something to rotate the board and do the search, but I think that might be not worth the effort