-
-
Notifications
You must be signed in to change notification settings - Fork 5
Day 2: Rock Paper Scissors #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What would your total score be if everything goes exactly according to your strategy guide? So loop through every line, figure out who won to get the score, then add a score based on the second letter. |
Started with this - I copied it in from my previous day, then used the comments to prompt Copilot to write some code: use std::fs;
use std::io;
use std::io::BufRead;
fn main() -> io::Result<()> {
let file = fs::File::open("input.txt")?;
let reader = io::BufReader::new(file);
let mut score = 0;
for line in reader.lines() {
let line = line?;
// Line is of form "A B" - split into two variables
let (a, b) = line.split_at(1);
// Print those
println!("{} {}", a, b);
}
Ok(())
} |
I prompted:
And got back: let winner = match (opponent, me) {
("A", "Z") | ("B", "X") | ("C", "Y") => "opponent",
("A", "X") | ("B", "Y") | ("C", "Z") => "me",
_ => "draw",
}; I'll rewrite that to use score numbers instead. |
Turns out it was splitting it into |
Weird, my code is correct against the example data but when I try to run it against the input and submit the result on Avent of Code it says:
|
Here's my code so far: advent-of-code-2022-in-rust/02/rock-paper-scissors.rs Lines 1 to 38 in c1f2e5f
|
Added a comment and now I can see the problem - it's with the /*
A/X = rock
B/Y = paper
C/Z = scissors
*/
// Who won? Opponent A beats me Z, B beats me X, C beats me Y
let win_score = match (opponent, me) {
("A", "Z") | ("B", "X") | ("C", "Y") => 0,
("A", "X") | ("B", "Y") | ("C", "Z") => 6,
_ => 3,
}; A, X should not result in a win for me (6 points) - it's a draw. |
OK, that fixed it - got through to the next question.
So scoring is still the same, but now the second column tells you how many points you got because we assume you played the right item? Oh I get it, the columns now just say "opponent picked X and you won/lost/tied" so I have to figure out what I must have picked to get that outcome - so I can calculate that |
Prompt-generated this to make it easier to follow: let mut parts = line.split(" ");
let opponent_code = parts.next().unwrap();
let outcome_code = parts.next().unwrap();
/*
A = rock
B = paper
C = scissors
X = I lose
Y = draw
Z = I win
*/
let opponent = match opponent_code {
"A" => "rock",
"B" => "paper",
"C" => "scissors",
_ => "unknown",
};
let outcome = match(outcome_code) {
"X" => "lose",
"Y" => "draw",
"Z" => "win",
_ => "unknown",
}; |
|
I pasted that into https://chat.openai.com/
It replied:
|
advent-of-code-2022-in-rust/02/rock-paper-scissors2.rs Lines 1 to 72 in 8b4d09b
Tried that, Advent says "That's not the right answer; your answer is too low". |
I forgot to add |
That passed. |
Code review: Instead of writing stringly typed code, I would suggest parsing into enums to leverage Rusts type system and get the guarantees it provides, which is one of rusts big advantages. For example like this.
Which will create new types named Move and Outcome that can only hold those alternatives. The AI can probably tell you how to parse the text into those types. This would also have avoided the error since then the rust compiler would have know that there are no other possible options and not needed an "unknown" case. And if you had missed a potential case in the match the compiler would have told you which one, turning that into a compile-time error instead of a runtime error. |
Out of curiosity, what rustc version are you running? For a while, the full output of a case like the missing fallback in the match looks like this, which hopefully would have been helpful:
|
https://adventofcode.com/2022/day/2
Puzzle input: https://adventofcode.com/2022/day/2/input
e.g.
The text was updated successfully, but these errors were encountered: