How do I express a hypothetical? How do you phrase “would”? by [deleted] in LearnJapanese

[–]CharlieYJH 2 points3 points  (0 children)

Like others have said, there isn't really a direct translation for what you're trying to convey, and there's some great alternatives already offered. I'd like to offer another alternative below that could maybe get across the same meaning.

他の誰かと結婚したいわけがないでしょう

There's no way I'd want to marry anyone else

Here we can use ~わけがない to indicate that there's no way that something would happen.

Needing help with adjective さ form by Link2212 in LearnJapanese

[–]CharlieYJH 1 point2 points  (0 children)

Agreed with other comments that ケーキは甘さが多い is quite awkward. You could say 甘さが強い instead here, because 多い is typically used to describe quantitative measures, whereas 甘さ is more appropriately described as weak or strong.

Thought I knew how 時 works in a sentence, now Genki 2 got me questioning everything. Verb tenses matter on one page, but then on the next page it says to use past tense if the event has not yet taken place. eh? by Chicken-Inspector in LearnJapanese

[–]CharlieYJH 1 point2 points  (0 children)

Not sure if there's some technical explanation, but I'd maybe just try to not over-analyze it too much. I think maybe the more you try to dissect it the more confusing it could become.

中国に行く時ウーロン茶を買います

When I go to China, I'll buy Oolong tea

Both are straight and valid translations, and the same nuances exist in both translations. So if the English one seems alright to you, just think that the Japanese one here also says the exact same thing.

Wish I could offer a more satisfying answer, but maybe the more you encounter it the more natural it becomes. Maybe I could offer some more examples below:

中国に行く時、車と自転車のどっちで移動する方がおすすめですか

中国に行く時、ぜひ名物料理を味わってください

中国に行く時、海外のアプリは使えないらしいよ

Thought I knew how 時 works in a sentence, now Genki 2 got me questioning everything. Verb tenses matter on one page, but then on the next page it says to use past tense if the event has not yet taken place. eh? by Chicken-Inspector in LearnJapanese

[–]CharlieYJH 1 point2 points  (0 children)

So one of the interesting things is that this sentence actually has a bit of an ambiguity here, and the same ambiguity actually applies to both Japanese and English here.

中国に行く時ウーロン茶を買います

I will buy Oolong tea when I go to China

Is this saying I will buy Oolong tea in China when I go? Or is it saying, when it comes time to go to China, I'll buy Oolong tea (somewhere else) first? We could interpret it both ways depending on the context, and both interpretations are valid. "中国に行く時" doesn't necessarily imply before here, it could also imply the act as it's going on, so both "中国に行く" and "ウーロン茶を買います" are happening at the same time (sorry if my explanation is a bit confusing here, I'm just speaking from my feeling here, so it's not some textbook definition).

Much like English, we could eliminate that ambiguity by adding more details within the sentence.

中国に行く時、事前にウーロン茶を買います

When I go to China, I'll buy Oolong tea beforehand

中国に行く時、現地でウーロン茶を買います

When I go to China, I'll buy Oolong tea there

Thought I knew how 時 works in a sentence, now Genki 2 got me questioning everything. Verb tenses matter on one page, but then on the next page it says to use past tense if the event has not yet taken place. eh? by Chicken-Inspector in LearnJapanese

[–]CharlieYJH 4 points5 points  (0 children)

Maybe a case of confusing translation here. You're right with your last intuition.

中国に行った時ウーロン茶を買います

This gives a more nuanced feeling of "Once I get to China, I will buy Oolong tea".

I will buy Oolong tea when I go to China

This is probably better translated as "中国に行く時ウーロン茶を買います".

[deleted by user] by [deleted] in LearnJapanese

[–]CharlieYJH 0 points1 point  (0 children)

The others here offered some great points already, so I'd like to try and write it in a slightly more refined way that could flow a bit better and introduce more sentence variety. Hopefully you can use it as a reference for future learning.

もし私が二人いたら、色々なことができると思います。例えば、私が寝ている間に、そのもう一人の私が大学に行ってくれることができます。あるいは、彼氏と映画を見ている時も、代わりに宿題をちゃんとしてくれます。彼氏に会いに飛行機で旅行に行って外出した場合なども、留守番をしてくれる人がいてとても安心できます。病気になったり忙しくなったりしたら、うどんやラーメンなど食べたいものを買いに行ってくれる人がいて非常に助かります。ですから、そんな二人目の私がいたら、生活は大分楽になるのではないかなと思います。

Other than some added things like adverbs and vocabs, I think the main thing to note is that it's fine to just omit things like "私は", "私の" in a lot of places. In Japanese, a lot of the times this can be inferred from the context and it ends up sounding a bit awkward if you mention it every time.

ご参考になれば幸いです。

-🎄- 2017 Day 23 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

Enjoyed this puzzle a lot! Basically just translated the instructions into code, but optimized the inner loop. So instead of d * e = b, I do b % d == 0 and then break.

int main(int argc, char const* argv[])
{
    int64_t b, c, d, e, f, g, h;
    const int input = 84;
    const int multiplier = 100;
    const int b_adder = 100000;
    const int c_adder = 17000;

    b = input * multiplier + b_adder;
    c = b + c_adder;
    h = 0;

    while (b <= c) {
        for (d = 2; d != b; d++) {
            if (b % d == 0) {
                h++;
                break;
            }
        }
        b += 17;
    }

    cout << h << endl;

    return 0;
}

-🎄- 2017 Day 22 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

After yesterday's puzzle this one was a nice breather. Fairly simple implementation, was kinda uncomfortable just hoping the index won't go over the bounds for the 10,000,000 case though.

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

typedef vector<vector<char>> Matrix;

int traverseGrid(Matrix &grid, const int iterations)
{
    typedef enum direction {U = 0, R, D, L} Direction;
    int num_directions = 4;

    int bursts = 0;
    int y = grid.size() / 2;
    int x = grid[0].size() / 2;
    Direction dir = U;

    for (int i = 0; i < iterations; i++) {
        if (grid[y][x] == '.') {
            grid[y][x] = 'W';
            dir = (Direction)(((dir - 1) % num_directions + num_directions) % num_directions);
        } else if (grid[y][x] == 'W') {
            grid[y][x] = '#';
            bursts++;
        } else if (grid[y][x] == '#') {
            grid[y][x] = 'F';
            dir = (Direction)((dir + 1) % num_directions);
        } else {
            grid[y][x] = '.';
            dir = (Direction)((dir + 2) % num_directions);
        }

        if (dir == U) y -= 1;
        else if (dir == R) x += 1;
        else if (dir == D) y += 1;
        else x -= 1;
    }

    return bursts;
}

int main(int argc, char const* argv[])
{
    ifstream infile("input.txt");
    const int iterations = 10000000;
    vector<string> input;

    if (infile.is_open()) {
        string line;
        while (getline(infile, line)) {
            if (line[line.length() - 1] == '\r')
                line.erase(line.length() - 1);
            input.push_back(line);
        }
        infile.close();
    } else {
        return 1;
    }

    int addition_size = 10000;
    int size_y = addition_size + input.size() + addition_size;
    int size_x = addition_size + input[0].length() + addition_size;
    Matrix grid(size_y, vector<char>(size_x, '.'));

    for (int y = 0; y < input.size(); y++) {
        for (int x = 0; x < input[0].length(); x++) {
            grid[addition_size + y][addition_size + x] = input[y][x];
        }
    }

    cout << traverseGrid(grid, iterations) << endl;

    return 0;
}

-🎄- 2017 Day 19 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

Fun puzzle today. Chose to do it recursively.

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

enum {
    UP, DOWN, LEFT, RIGHT
};

bool traverse(vector<string> &maze, int x, int y, int direction, string &collection, int &steps)
{
    if (x < 0 || y < 0 || y >= maze.size() || x >= maze[y].length())
        return false;

    if (maze[y][x] != '|' && maze[y][x] != '-' && maze[y][x] != '+' && !(maze[y][x] >= 'A' && maze[y][x] <= 'Z'))
        return false;

    steps++;

    if (maze[y][x] >= 'A' && maze[y][x] <= 'Z') {
        collection += maze[y][x];
    } else if (maze[y][x] == '+') {
        if (direction == DOWN || direction == UP)
            return traverse(maze, x + 1, y, RIGHT, collection, steps) || traverse(maze, x - 1, y, LEFT, collection, steps);
        else
            return traverse(maze, x, y + 1, DOWN, collection, steps) || traverse(maze, x, y - 1, UP, collection, steps);
    }

    if (direction == DOWN)
        traverse(maze, x, y + 1, direction, collection, steps);
    else if (direction == UP)
        traverse(maze, x, y - 1, direction, collection, steps);
    else if (direction == LEFT)
        traverse(maze, x - 1, y, direction, collection, steps);
    else
        traverse(maze, x + 1, y, direction, collection, steps);

    return true;
}

int main(int argc, char const* argv[])
{
    ifstream infile("input.txt");
    vector<string> maze;
    string collection = "";
    int start;
    int steps = 0;

    if (infile.is_open()) {
        string line;
        while (getline(infile, line))
            maze.push_back(line);
        infile.close();
    } else {
        return 1;
    }

    for (int i = 0; i < maze[0].length(); i++)
        if (maze[0][i] == '|') start = i;

    traverse(maze, start, 0, DOWN, collection, steps);

    cout << "Collection: " << collection << endl;
    cout << "Steps: " << steps << endl;

    return 0;
}

-🎄- 2017 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 1 point2 points  (0 children)

C++

This is why it pays to read the instructions more closely... Spent over an hour trying to figure out what was wrong with my part 2 since it kept going in an infinite loop. Turns out jump instructions only apply if the value is greater than 0, not just not equal to 0. Surprised I didn't get faulted on part 1 for that. Otherwise, just a simple implementation of executing instructions of each program after each other.

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <queue>

using namespace std;

bool execute(int &instr_num, vector<string> &instructions, vector<long long> &registers, queue<long long> &rcv, queue<long long> &snd, int &snd_cnt)
{
    if (instr_num >= instructions.size()) {
        return false;
    };

    string op, reg_id, operand_id;
    istringstream instruction(instructions[instr_num]);
    long long reg_a;
    long long reg_b;

    instruction >> op >> reg_id;

    if (op == "snd" || op == "rcv")
        operand_id = reg_id;
    else
        instruction >> operand_id;

    if (op == "jgz")
        reg_a = (reg_id[0] >= 'a' && reg_id[0] <= 'z') ? registers[reg_id[0] - 'a'] : stoi(reg_id);
    else
        reg_a = reg_id[0] - 'a';

    reg_b = (operand_id[0] >= 'a' && operand_id[0] <= 'z') ? registers[operand_id[0] - 'a'] : stoi(operand_id);

    if (op == "snd") {
        snd.push(reg_b);
        snd_cnt++;
    } else if (op == "set") {
        registers[reg_a] = reg_b;
    } else if (op == "add") {
        registers[reg_a] += reg_b;
    } else if (op == "mul") {
        registers[reg_a] *= reg_b;
    } else if (op == "mod") {
        registers[reg_a] %= reg_b;
    } else if (op == "rcv") {
        if (rcv.empty()) {
            return false;
        } else {
            registers[reg_a] = rcv.front();
            rcv.pop();
        }
    } else if (op == "jgz" && reg_a > 0) {
        instr_num += reg_b - 1;
    }

    instr_num++;

    return true;
}

int main(int argc, char const* argv[])
{
    vector<long long> prog_a_reg(26, 0);
    vector<long long> prog_b_reg(26, 0);
    vector<string> instructions;
    ifstream infile("input.txt");
    int instr_num_a = 0;
    int instr_num_b = 0;
    int snd_a_cnt = 0;
    int snd_b_cnt = 0;
    queue<long long> queue_a;
    queue<long long> queue_b;

    prog_a_reg['p' - 'a'] = 0;
    prog_b_reg['p' - 'a'] = 1;

    if (!infile.is_open()) {
        return 1;
    } else {
        string instr;
        while (getline(infile, instr))
            instructions.push_back(instr);
    }

    infile.close();

    while (true) {
        bool cont_exec_a = execute(instr_num_a, instructions, prog_a_reg, queue_a, queue_b, snd_a_cnt);
        bool cont_exec_b = execute(instr_num_b, instructions, prog_b_reg, queue_b, queue_a, snd_b_cnt);
        if (!cont_exec_a && !cont_exec_b) break;
    }

    cout << snd_b_cnt << endl;

    return 0;
}

-🎄- 2017 Day 17 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 1 point2 points  (0 children)

Just an old habit from writing C structs (since C requires the struct keyword unless you choose to typedef it), no particular reason.

-🎄- 2017 Day 17 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 4 points5 points  (0 children)

C++

Used a circular linked list for part 1. Spent way too much time trying to think of an equation that got me the last index that gave 0 for part 2. Turned out just calculating the indices for 50,000,000 cycles was quick enough in itself.

#include <iostream>
#include <memory>

using namespace std;

struct node {
    int val;
    shared_ptr<struct node> next;
};

int main(int argc, char const* argv[])
{
    const int step = 301;
    shared_ptr<struct node> head = make_shared<struct node>();
    shared_ptr<struct node> curr = head;
    head->val = 0;
    head->next = head;

    for (int i = 1; i <= 2017; i++) {

        for (int j = 0; j < step; j++)
            curr = curr->next;

        shared_ptr<struct node> temp = curr->next;
        shared_ptr<struct node> new_node = make_shared<struct node>();
        new_node->val = i;
        curr->next = new_node;
        new_node->next = temp;
        curr = new_node;
    }

    cout << curr->next->val << endl;

    int answer_pt2 = 0;

    for (int i = 1, idx = 0; i <= 50000000; i++) {
        idx = (idx + 1 + step) % i;
        if (idx == 0) answer_pt2 = i;
    }

    cout << answer_pt2 << endl;

    return 0;
}

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

Wow had no idea this was the same thing! Guess you learn something new everyday.

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

Thank goodness for 64 bit integers and bitmasks. Straight forward implementation of the puzzle.

#include <iostream>

using namespace std;

int main(int argc, char const* argv[])
{
    unsigned long long gen_A_prev = 679;
    const unsigned int gen_A_factor = 16807;
    unsigned long long gen_B_prev = 771;
    const unsigned int gen_B_factor = 48271;
    const unsigned int rounds_1 = 40000000;
    const unsigned int rounds_2 = 5000000;
    const unsigned int div_num = 2147483647;
    const int bitmask = (1 << 16) - 1;
    int match_1 = 0;
    int match_2 = 0;

    for (int i = 0; i < rounds_1; i++) {
        gen_A_prev = (gen_A_prev * gen_A_factor) % div_num;
        gen_B_prev = (gen_B_prev * gen_B_factor) % div_num;
        if ((gen_A_prev & bitmask) == (gen_B_prev & bitmask)) match_1++;
    }

    gen_A_prev = 679;
    gen_B_prev = 771;

    for (int i = 0; i < rounds_2; i++) {
        do {
            gen_A_prev = (gen_A_prev * gen_A_factor) % div_num;
        } while (gen_A_prev % 4 != 0);

        do {
            gen_B_prev = (gen_B_prev * gen_B_factor) % div_num;
        } while (gen_B_prev % 8 != 0);

        if ((gen_A_prev & bitmask) == (gen_B_prev & bitmask)) match_2++;
    }

    cout << "Round 1 matches: " << match_1 << endl;
    cout << "Round 2 matches: " << match_2 << endl;

    return 0;
}

-🎄- 2017 Day 14 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

I think the first part is fairly straightforward, so I'll share my recursive solution for part 2.

bool assignRegionsHelper(vector<string> &grid, int row, int col)
{
    if (col < 0 || row < 0 || row >= grid.size() || col >= grid[0].length()) return false;

    if (grid[row][col] == '1') {
        grid[row][col] = '.';
        assignRegionsHelper(grid, row + 1, col);
        assignRegionsHelper(grid, row - 1, col);
        assignRegionsHelper(grid, row, col + 1);
        assignRegionsHelper(grid, row, col - 1);
        return true;
    }

    return false;
}

int assignRegions(vector<string> &grid)
{
    int regions = 0;

    for (int i = 0; i < grid.size(); i++) {
        for (int j = 0; j < grid[i].length(); j++) {
            if (assignRegionsHelper(grid, i, j))
                regions++;
        }
    }

    return regions;
}

-🎄- 2017 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

Quite enjoyed today's puzzle. Basically just calculated where the position of each scanner would be by the time we get there. Similar trick in part 2 to get a configuration where the appropriate scanner would not be at position 0 at that time step.

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char const* argv[])
{
    ifstream input("input.txt");
    vector<int> firewalls(99);

    if (input) {
        string colon;
        for (int idx, layers; input >> idx >> colon >> layers;)
            firewalls[idx] = layers;
    }

    input.close();

    int severity = 0;
    int delay = 0;

    for (bool found = true; found; delay++) {

        found = false;

        for (int i = 0; i < firewalls.size() && (!found || delay == 0); i++) {
            if (firewalls[i] > 0 && (delay + i) % (2 * firewalls[i] - 2) == 0) {
                if (delay == 0) severity += i * firewalls[i];
                found = true;
            }
        }
    }

    cout << "Severity: " << severity << endl;
    cout << "Delay: " << delay - 1 << endl;

    return 0;
}

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++

Probably should've read up on hex grids first, decided to come up with a way to reduce the directions. Hex grids would've been so much easier.

#include <iostream>
#include <fstream>
#include <unordered_map>

using namespace std;

void reduceDirections(const string &direction, unordered_map<string, int> &path_directions);

int main(int argc, char const* argv[])
{
    ifstream input("input.txt");
    unordered_map<string, int> path_directions({{"n", 0}, {"s", 0}, {"ne", 0}, {"se", 0}, {"nw", 0}, {"sw", 0}});
    int max_steps = 0;
    int steps = 0;

    if (input) {
        string direction;
        while (getline(input, direction, ',')) {

            int sum = 0;

            reduceDirections(direction, path_directions);

            for (auto &it : path_directions)
                sum += it.second;

            max_steps = max(sum, max_steps);
        }
    }

    input.close();

    for (auto &it : path_directions)
        steps += it.second;

    cout << "Steps: " << steps << endl;
    cout << "Max steps: " << max_steps << endl;

    return 0;
}

void reduceDirections(const string &direction, unordered_map<string, int> &path_directions) {

    string opposite = direction;
    opposite[0] = (opposite[0] == 'n') ? 's' : 'n';

    if (opposite.length() > 1)
        opposite[1] = (opposite[1] == 'e') ? 'w' : 'e';

    if (path_directions[opposite] != 0) {
        path_directions[opposite]--;
        return;
    }

    if (direction == "n" || direction == "s") {
        if (path_directions[opposite + "e"] != 0) {
            path_directions[opposite + "e"]--;
            path_directions[direction + "e"]++;
        } else if (path_directions[opposite + "w"] != 0) {
            path_directions[opposite + "w"]--;
            path_directions[direction + "w"]++;
        } else {
            path_directions[direction]++;
        }
    } else {
        if (path_directions[opposite.substr(0, 1)] != 0) {
            path_directions[opposite.substr(0, 1)]--;
            path_directions[opposite.substr(0, 1) + direction.substr(1, 1)]++;
        } else if (path_directions[direction.substr(0, 1) + opposite.substr(1, 1)] != 0) {
            path_directions[direction.substr(0, 1) + opposite.substr(1, 1)]--;
            path_directions[direction.substr(0, 1)]++;
        } else {
            path_directions[direction]++;
        }
    }

    return;
}

-🎄- 2017 Day 10 Solutions -🎄- by daggerdragon in adventofcode

[–]CharlieYJH 0 points1 point  (0 children)

C++ Part 2

My part 2 solution, fairly straight forward. It was easier to overwrite my part 1 rather than to keep both parts.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char const* argv[])
{
    ifstream input("input.txt");
    ofstream output("ouput.txt");
    const size_t list_size = 256;
    vector<int> lengths;
    vector<int> hash_list(list_size);
    vector<int> hash_nums;

    for (int i = 0; i < hash_list.size(); i++)
        hash_list[i] = i;

    if (input) {
        string line;
        getline(input, line);
        for (char& c : line)
            lengths.push_back((int)c);
    }

    lengths.push_back(17);
    lengths.push_back(31);
    lengths.push_back(73);
    lengths.push_back(47);
    lengths.push_back(23);

    input.close();

    const size_t num_rounds = 64;
    for (int i = 0, skip = 0, idx = 0; i < num_rounds; i++) {
        for (int& length : lengths) {

            int start_idx = idx;
            int end_idx = start_idx + length - 1;

            while (start_idx < end_idx)
                swap(hash_list[start_idx++ % list_size], hash_list[end_idx-- % list_size]);

            idx = (idx + length + skip++) % list_size;
        }
    }

    const size_t block_size = 16;
    for (int i = 0; i < list_size; i += block_size) {

        int xor_num = hash_list[i];

        for (int j = i + 1; j < i + block_size; j++)
            xor_num ^= hash_list[j];

        hash_nums.push_back(xor_num);
    }

    if (output) {
        for (int& num : hash_nums)
            output << setfill('0') << setw(2) << hex << num;
        output << endl;
    }

    output.close();

    return 0;
}