Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <chrono>
- #include <iostream>
- #include <vector>
- #include <tuple>
- #include <algorithm>
- #include <array>
- const int arr_sz = 12001;
- typedef std::array<int, arr_sz> arr_2k;
- int F_Day25_Exec(std::vector<std::vector<int>>& instr, int& state, arr_2k& tape, int& pos)
- {
- if (tape[pos] == 0)
- {
- tape[pos] = instr[state][0];
- pos += instr[state][1];
- state = instr[state][2];
- }
- else
- {
- tape[pos] = instr[state][3];
- pos += instr[state][4];
- state = instr[state][5];
- }
- return 1;
- }
- // use array instead of map. std::array?
- int Day25(void)
- {
- std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
- fs::path path("../../_Input/input_Day25.txt");
- //fs::path path("../../_Input/input_Day25_test.txt");
- int res_p1 = 4225;
- std::vector<std::vector<std::string>> sinp = F_Split_vString(F_Read_File_To_Array(path), ' ');
- while (sinp.back().size() == 0)
- {
- sinp.pop_back();
- }
- int state = F_Alphabetic_Number(sinp[0][sinp[0].size() - 1][0])-1;
- int steps = std::stoi(sinp[1][sinp[1].size() - 2]);
- sinp.erase(sinp.begin(), sinp.begin() + 3);
- int pos = (arr_sz - 1)/6;
- arr_2k tape;
- tape.fill(0);
- std::vector<std::vector<int>> instructions;
- 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
- {
- int ind = i * 10;
- std::vector<int> t; // lines 0 - 8 for each instr
- for (int j = 0; j < 2; j++)
- {
- t.push_back(sinp[j * 4 + ind + 2][sinp[j * 4 + ind + 2].size() - 1][0] - '0'); // value to write in [0] and [3]
- std::string sdir = sinp[j * 4 + ind + 3][sinp[j * 4 + ind + 3].size() - 1];
- int dir = (sdir == "left." ? -1 : 1);
- t.push_back(dir); // direction to [1] and [4]. right = 1, left = -1
- 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]
- }
- instructions.push_back(t);
- //backup for instructions map: instructions[F_Alphabetic_Number(sinp[ind][sinp[ind].size() - 1][0])] = t;
- }
- for (int i = 0; i < steps; i++)
- {
- F_Day25_Exec(instructions, state, tape, pos);
- }
- int sum = std::accumulate(tape.begin(), tape.end(), 0);
- cout << "Day25-Part1: " << sum << (sum == res_p1 ? ". Correct." : ". Wrong.") << endl;
- F_Print_Time("Day25 duration: ", t1);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement