Advertisement
Guest User

Untitled

a guest
Dec 28th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3. #include <vector>
  4. #include <tuple>
  5. #include <algorithm>
  6. #include <array>
  7.  
  8. const int arr_sz = 12001;
  9. typedef std::array<int, arr_sz> arr_2k;
  10.  
  11. int F_Day25_Exec(std::vector<std::vector<int>>& instr, int& state, arr_2k& tape, int& pos)
  12. {
  13.     if (tape[pos] == 0)
  14.     {
  15.         tape[pos] = instr[state][0];
  16.         pos += instr[state][1];
  17.         state = instr[state][2];
  18.     }
  19.     else
  20.     {
  21.         tape[pos] = instr[state][3];
  22.         pos += instr[state][4];
  23.         state = instr[state][5];
  24.     }
  25.     return 1;
  26. }
  27.  
  28. // use array instead of map. std::array?
  29.  
  30. int Day25(void)
  31. {
  32.     std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  33.  
  34.     fs::path path("../../_Input/input_Day25.txt");
  35.     //fs::path path("../../_Input/input_Day25_test.txt");
  36.     int res_p1 = 4225;
  37.  
  38.     std::vector<std::vector<std::string>> sinp = F_Split_vString(F_Read_File_To_Array(path), ' ');
  39.     while (sinp.back().size() == 0)
  40.     {
  41.         sinp.pop_back();
  42.     }
  43.     int state = F_Alphabetic_Number(sinp[0][sinp[0].size() - 1][0])-1;
  44.     int steps = std::stoi(sinp[1][sinp[1].size() - 2]);
  45.     sinp.erase(sinp.begin(), sinp.begin() + 3);
  46.  
  47.     int pos = (arr_sz - 1)/6;
  48.     arr_2k tape;
  49.     tape.fill(0);
  50.  
  51.     std::vector<std::vector<int>> instructions;
  52.     for (int i = 0; i < (sinp.size() + 1) / 10; i++)            // 9 line per instruction + one empty line in between. so 9 lines for first instruction, 10 for all others
  53.     {
  54.         int ind = i * 10;
  55.         std::vector<int> t;                         // lines 0 - 8 for each instr
  56.  
  57.         for (int j = 0; j < 2; j++)
  58.         {
  59.             t.push_back(sinp[j * 4 + ind + 2][sinp[j * 4 + ind + 2].size() - 1][0] - '0');      // value to write in [0] and [3]
  60.  
  61.             std::string sdir = sinp[j * 4 + ind + 3][sinp[j * 4 + ind + 3].size() - 1];
  62.             int dir = (sdir == "left." ? -1 : 1);
  63.             t.push_back(dir);                                                                                       // direction to [1] and [4]. right = 1, left = -1
  64.  
  65.             t.push_back(F_Alphabetic_Number(sinp[j * 4 + ind + 4][sinp[j * 4 + ind + 4].size() - 1][0])-1);     // new state in [2] and [5]
  66.         }
  67.         instructions.push_back(t);
  68.         //backup for instructions map: instructions[F_Alphabetic_Number(sinp[ind][sinp[ind].size() - 1][0])] = t;
  69.     }
  70.  
  71.     for (int i = 0; i < steps; i++)
  72.     {
  73.         F_Day25_Exec(instructions, state, tape, pos);
  74.     }
  75.  
  76.     int sum = std::accumulate(tape.begin(), tape.end(), 0);
  77.     cout << "Day25-Part1: " << sum << (sum == res_p1 ? ". Correct." : ". Wrong.") << endl;
  78.     F_Print_Time("Day25 duration: ", t1);
  79.     return 1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement