Tuesday, May 6, 2014

Problem 83 - Zimbu, and no more path sums!

So, problem 83 is just a harder version of the previous 2 problems, 81, and 82. As far as figuring out a solution, my main difficulty was in misreading the problem and not realizing that you have to go from corner to corner (note: with the restriction removed, the answer to this problem is the answer to problem 82!). However, finding a language took a while. By this point, I have used a lot of languages. And while it might be tempting to say that that means that there are not many programming languages left out there, that is simply not true. However, some languages are...difficult to work with. In particular, I found this: http://fll.presidentbeef.com/, which is a list of "fledgling" languages. Many of these are still in development (Kitten, which I really wanted to use because a) it seems like a neat functional concatenative langauge and b) it would have been a nice follow up to Cat, is in this category), or have code that just simply doesn't seem like it will compile (brace was like that), or have incredibly arcane build systems (Lobster, which is set up for OS X or win32, but for Linux would have required downloading more libraries than I wanted to deal with). But, I found one that worked (the developer sayed it is in the proof of concept to beta stage, but when "proof of concept" included the compiler being able to compile itself, things seem pretty safe.

This language was Zimbu (http://www.zimbu.org/). Two things stood out about Zimbu that made me want to use it: a) it was at the bottom of the list, being last in alphabetical order and b) it was written by the author of vim, my favorite command line text editor. So, using Zimbu. It was easy to build, run, yada yada, its a compiled language that compiles to C with pretty C-like syntax, yep, pretty solid in the basics. Neat advantage: it was written by the author of vim: therefore, even though it is crazily uncommon, it has beautiful syntax highlighting / autocorrect / autoindenting in vim. And I kind of like the language in principle, it has some neat little things about it, including bringing us back to the 70s with ALL CAPS reserved words, which, actually, I found to be something of a nice feature, the caps help to make them stand out. However, the one main complaint I had with the language that it is aggressively whitespace sensitive. The language description seems to say this is so that code is easy to read, but this language takes whitespace sensitivity to kind of a pedantic level that enforces an exact style of code. The main issue here is that all operators must be separated by whitespace. So in the below, where I have updated[row - 1][col], it is a syntax error to write updated[row-1][col]. Similarly, there must be a space after the comma in a list. So yeah, just annoying stuff like that...this discussion makes me think that maybe I should at some point try to kind of formally review the languages I use, maybe I will implement something like a grading system, 1 to 5 stars on various categories...we shall see. Anyway, my code is below, runs in about .13s on my machine, and now I can start using languages that don't have easy ways of defining matrices again.
FUNC Main() int
  list<list<int>> matrix = [[4445, 2697, 5115, 718, 2209, ...]]
  list<list<int>> updated = NEW(80, NEW(80, 0))
  FOR row IN 0 TO 79
    updated[row] = NEW(80, 0)
  }
  updated[0][0] = matrix[0][0]
  FOR diag IN 1 TO 159
    int h = diag
    IF h > 79
      h = 79
    }
    int l = diag - 79
    IF h < 0
      h = 0
    }
    FOR col IN l TO h
      int row = diag - col
      int lp = matrix[row][col]
      IF row > 0
        lp += updated[row - 1][col]
      ELSE
        lp += 1000000
      }
      int rp = matrix[row][col]
      IF col > 0
        rp += updated[row][col - 1]
      ELSE
        rp += 1000000
      }
      IF rp < lp
        updated[row][col] = rp
      ELSE
        updated[row][col] = lp
      }
    }
  }
  DO
    bool mchanged = FALSE
    FOR col IN 0 TO 79
      DO
        bool changed = FALSE
        FOR row IN 0 TO 79
          IF row > 0
            IF updated[row - 1][col] < updated[row][col] - matrix[row][col]
              updated[row][col] = matrix[row][col] + updated[row - 1][col]
              changed = TRUE
            }
          }
          IF row < 79
            IF updated[row + 1][col] < updated[row][col] - matrix[row][col]
              updated[row][col] = matrix[row][col] + updated[row + 1][col]
              changed = TRUE
            }
          }
        }
        mchanged = mchanged || changed
      UNTIL !changed
    }
    FOR row IN 0 TO 79
      DO
        bool changed = FALSE
        FOR col IN 0 TO 79
          IF col > 0
            IF updated[row][col - 1] < (updated[row][col] - matrix[row][col])
              updated[row][col] = matrix[row][col] + updated[row][col - 1]
              changed = TRUE
            }
          }
          IF col < 79
            IF updated[row][col + 1] < updated[row][col] - matrix[row][col]
              updated[row][col] = matrix[row][col] + updated[row][col + 1]
              changed = TRUE
            }
          }
        }
        mchanged = mchanged || changed
      UNTIL !changed
    }
  UNTIL !mchanged
  IO.write(updated[79][79])
  IO.write("\n")
  RETURN 0
}

No comments:

Post a Comment