Skip to content

Conversation

@hroc135
Copy link
Owner

@hroc135 hroc135 commented Dec 27, 2024

- ^: XOR。numが1なら^numで0b11...10になる
- このままだと負の数が帰ってしまうので、1bit分のANDを取る
- gptは^num & 0x01というコードを書いたが、
16進数でANDを取るより2進数で取った方が直感に反しないので変えた
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはもっと単純に
if obstacleGrid[0][0] == 0 {
return 1
}
return 0
とすればいいでしょう。「obstacleGrid[0][0]が0なら1を、1なら0を返したかった」の文字通りですよね。
さらに、下の条件はobstacleGrid[0][0] == 1 で上に書かれていますね。あと、上は他とまとめられるはずです。

Comment on lines +138 to +145
// gridの始点(0,0)から進められるマスが全て障害である場合、0を返す
if (rowCount <= 1 || obstacleGrid[1][0] == 1) && (colCount <= 1 || obstacleGrid[0][1] == 1) {
return 0
}
// gridの終点へ進められるマスが全て障害である場合、0を返す
if (rowCount <= 1 || obstacleGrid[rowCount-2][colCount-1] == 1) && (colCount <= 1 || obstacleGrid[rowCount-1][colCount-2] == 1) {
return 0
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ、下のループとくっつきませんか。(特定の場合に速くなるかもしれませんが。)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こういう感じということでしょうか?

for i := 0; ... {
    for i := 0; ... {
        if obstacleGrid[i][j] == 1 { ... }
        if i == 0 && j == 0 { ... }
        if i == 0 && j == 1 {
            if obstacleGrid[0][1] == 1 && obstacleGrid[1][0] == 1 {
                return 0
            }
    }
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、いえ消してしまっていいだろう。
この「pattern defeatingな早期リターン」がないほうがよいという意図です。

pathCountGrid[0][0] = 1
continue
}
p := 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この後のコードで変更されてますが、やはりpという名前だと違和感がありますね。
まあ何が入る変数か、読めばすぐわかるのですが、あまりそういうことを読者に求める前提で書かないのが習慣として大事ですね(自戒の念を込めて)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね。スタイルガイドも合わせて確認しておきました
https://google.github.io/styleguide/go/decisions#single-letter-variable-names

- [[0,1,...],[1,...],...]
- [...,[...,1]]
- [...,[...,1],[...,1,0]]
- gridの大きさが1×1の時にobstacleGrid[0][0]が0なら1を、1なら0を返したかったので、

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - obstacleGrid[0][0]みたいな話でしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、そうです。難しく考えてしまっていました

pathCounts[c] = 0
continue
}
if r == 0 && c == 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一回しか通らないのでループ外に書いても良いと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは正しくは「最大1回通る」ですね。(0,0) が obstacle である場合はその前のブロックで処理されて continue します。また、ループの外に出しても (0,1), (1,0) の処理をしないといけないので結局ループを r = range(0, rowCount), c = range(0, columnCount) で回す必要があります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants