Advertisement
Guest User

Day 2 Solution

a guest
Dec 1st, 2016
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cctype>
  5.  
  6. using namespace std;
  7.  
  8. struct Coord {
  9.     int x, y;
  10.  
  11.     Coord(int xIn = 0, int yIn = 0) : x(xIn), y(yIn) {}
  12.  
  13.     Coord operator+(const Coord &other) {
  14.         return Coord(x + other.x, y + other.y);
  15.     }
  16.  
  17.     Coord operator=(const Coord &other) {
  18.         x = other.x;
  19.         y = other.y;
  20.  
  21.         return *this;
  22.     }
  23.  
  24.     bool legal() {
  25.         return (x >= 0 && x < 3 && y >= 0 && y < 3);
  26.     }
  27.  
  28. };
  29.  
  30. int main() {
  31.     fstream in("input.txt");
  32.     string line;
  33.  
  34.     // const int BUTTON[3][3] = {
  35.     //     {1, 2, 3},
  36.     //     {4, 5, 6},
  37.     //     {7, 8, 9}
  38.     // };
  39.     //
  40.     const char BUTTON[5][5] = {
  41.         {'?', '?', '1', '?', '?'},
  42.         {'?', '2', '3', '4', '?'},
  43.         {'5', '6', '7', '8', '9'},
  44.         {'?', 'A', 'B', 'C', '?'},
  45.         {'?', '?', 'D', '?', '?'}
  46.     };
  47.  
  48.     // Coord location(1, 1);
  49.     Coord location(2, 2);
  50.     Coord movement;
  51.     while(in >> line) {
  52.         // cout << line << endl;
  53.         for(int i = 0; i < line.size(); i++) {
  54.             if(line.at(i) == 'U') {
  55.                 movement = Coord(0, -1);
  56.             }
  57.             else if(line.at(i) == 'R') {
  58.                 movement = Coord(1, 0);
  59.             }
  60.             else if(line.at(i) == 'D') {
  61.                 movement = Coord(0, 1);
  62.             }
  63.             else if(line.at(i) == 'L') {
  64.                 movement = Coord(-1, 0);
  65.             }
  66.             Coord tempLocation = location + movement;
  67.             char button = BUTTON[tempLocation.y][tempLocation.x];
  68.             // if(tempLocation.legal()) {
  69.             if(isalnum(button)) {
  70.                 location = tempLocation;
  71.             }
  72.         }
  73.         cout << BUTTON[location.y][location.x];
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement