1. Kirill Morarenko
  2. icfp2012-ShallowEndians

Commits

Kirill Morarenko  committed 24ff86e Draft

map reader/writer

  • Participants
  • Parent commits ad52232
  • Branches default

Comments (0)

Files changed (3)

File fastsim/constants.h

View file
  • Ignore whitespace
+const char C_ROBOT = 'R';
+const char C_WALL = '#';
+const char C_ROCK = '*';
+const char C_LAMBDA = '\\';
+const char C_CLOSED_LIFT = 'L';
+const char C_OPEN_LIFT = 'O';
+const char C_EARTH = '.';
+const char C_EMPTY = ' ';

File fastsim/main.cpp

View file
  • Ignore whitespace
+#include <iostream>
+#include <vector>
+
+#include "constants.h"
+
+class Map{
+public:
+  Map(size_t rows, size_t cols) : m_rows(rows), m_cols(cols), m_data(std::vector<char>(rows * cols, C_EMPTY)) {}
+  size_t cols() const {return m_cols;}
+  size_t rows() const {return m_rows;}
+
+  char& get(size_t row, size_t col){
+    return m_data.at(row * m_cols + col); // replace with []
+  }
+  char get(size_t row, size_t col) const{
+    return m_data.at(row * m_cols + col); // replace with []
+  }
+private:
+  size_t m_rows;
+  size_t m_cols;
+  std::vector<char> m_data;
+};
+
+void printMap(const Map& map, std::ostream& stream){
+  for (size_t row = 0; row < map.rows(); ++row){
+    for (size_t col = 0; col < map.cols(); ++col){
+      stream << map.get(row, col);
+    }
+    stream << std::endl;
+  }
+}
+
+Map readMap(const std::istream& stream){
+  std::vector<std::string> lines;
+  std::string line;
+  size_t cols = 0;
+  while (std::getline(std::cin, line).good()){
+    lines.push_back(line);
+    cols = std::max(cols, line.length());
+  }
+  Map map(lines.size(), cols);
+  for (size_t row = 0; row < lines.size(); ++row){
+    for (size_t col = 0; col < std::min(map.cols(), lines[row].size()); ++col){
+      map.get(row, col) = lines[row][col];
+    }
+  }
+  return map;
+}
+
+int main(){
+  Map map = readMap(std::cin);
+  printMap(map, std::cout);
+}

File fastsim/run.sh

View file
  • Ignore whitespace
+g++ -Wall -Werror main.cpp && cat ../maps/contest1.map | ./a.out