day 2
This commit is contained in:
parent
80ad4b4e1d
commit
f600082452
2
rust/.gitignore
vendored
Normal file
2
rust/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
part1
|
||||||
|
part2
|
5
rust/2/README.md
Normal file
5
rust/2/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Actually organised my solutions and stopped committing the binary.
|
||||||
|
|
||||||
|
Spent way too long coming up with a "clever" solution to calculate RPS victory. Part 2 was quite a bit easier with just one line of mod3 arithmetic.
|
||||||
|
|
||||||
|
I'm committing all my scratch buffers of sketching out how a solution should work because why not
|
2500
rust/2/input.txt
Normal file
2500
rust/2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
55
rust/2/part1.rs
Normal file
55
rust/2/part1.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() -> () {
|
||||||
|
let path = "input.txt";
|
||||||
|
|
||||||
|
let input = File::open(path).unwrap();
|
||||||
|
let buf = BufReader::new(input);
|
||||||
|
|
||||||
|
let score: i32 = buf
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let opponent: i32 = match &line[0..1] {
|
||||||
|
"A" => 0,
|
||||||
|
"B" => 1,
|
||||||
|
"C" => 2,
|
||||||
|
_ => panic!("Invalid input"),
|
||||||
|
};
|
||||||
|
let you: i32 = match &line[2..3] {
|
||||||
|
"X" => 0,
|
||||||
|
"Y" => 1,
|
||||||
|
"Z" => 2,
|
||||||
|
_ => panic!("Invalid input"),
|
||||||
|
};
|
||||||
|
pick_score(you) + win_score(opponent, you)
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("{}", score);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transforms the "wrapping" cases of RPS into the trivial "middle" case where A wins if A > B
|
||||||
|
fn normalise_mod3(opponent: i32, you: i32) -> (i32, i32) {
|
||||||
|
let i = match opponent {
|
||||||
|
0 => 1,
|
||||||
|
2 => 2,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
((opponent + i) % 3, (you + i) % 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get score component for losing (0 points), drawing (3 points) or winning (6 points) the round
|
||||||
|
fn win_score(opponent: i32, you: i32) -> i32 {
|
||||||
|
let (opponent, you) = normalise_mod3(opponent, you);
|
||||||
|
let is_won = you - opponent.signum() + 1; // 0 on loss, 1 on draw, 2 on win}
|
||||||
|
3 * is_won
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get score component for your pick (trivial)
|
||||||
|
fn pick_score(you: i32) -> i32 {
|
||||||
|
you + 1
|
||||||
|
}
|
36
rust/2/part2.rs
Normal file
36
rust/2/part2.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn main() -> () {
|
||||||
|
let path = "input.txt";
|
||||||
|
|
||||||
|
let input = File::open(path).unwrap();
|
||||||
|
let buf = BufReader::new(input);
|
||||||
|
|
||||||
|
let score: i32 = buf
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let opponent: i32 = match &line[0..1] {
|
||||||
|
"A" => 0,
|
||||||
|
"B" => 1,
|
||||||
|
"C" => 2,
|
||||||
|
_ => panic!("Invalid input"),
|
||||||
|
};
|
||||||
|
match &line[2..3] {
|
||||||
|
"X" => 0 + pick_score(opponent, -1),
|
||||||
|
"Y" => 3 + pick_score(opponent, 0),
|
||||||
|
"Z" => 6 + pick_score(opponent, 1),
|
||||||
|
_ => panic!("Invalid input"),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("{}", score);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pick_score(opponent: i32, outcome: i32) -> i32 {
|
||||||
|
(opponent + outcome + 3) % 3 + 1
|
||||||
|
}
|
11
rust/2/sampleinput.txt
Normal file
11
rust/2/sampleinput.txt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
A X draw = 1 + 3 = 4. Also my parsing just takes the 1st and 3rd characters of
|
||||||
|
the lines so I can type anything else after that and it'll work as an input
|
||||||
|
file kek
|
||||||
|
A Y win = 2 + 6 = 8
|
||||||
|
A Z loss = 3 + 0 = 3 <- not 9
|
||||||
|
B X loss = 1 + 0 = 1
|
||||||
|
B Y draw = 2 + 3 = 5
|
||||||
|
B Z win = 3 + 6 = 9
|
||||||
|
C X win = 1 + 6 = 7 <- not 1
|
||||||
|
C Y loss = 2 + 0 = 2
|
||||||
|
C Z draw = 3 + 3 = 6
|
55
rust/2/thoughts.txt
Normal file
55
rust/2/thoughts.txt
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
A X draw
|
||||||
|
A Y win
|
||||||
|
A Z loss
|
||||||
|
|
||||||
|
B X loss
|
||||||
|
B Y draw
|
||||||
|
B Z win
|
||||||
|
|
||||||
|
C X win
|
||||||
|
C Y loss
|
||||||
|
C Z draw
|
||||||
|
|
||||||
|
#########
|
||||||
|
|
||||||
|
1 1 draw
|
||||||
|
1 2 win
|
||||||
|
1 3 loss <-- !!
|
||||||
|
|
||||||
|
2 1 loss
|
||||||
|
2 2 draw
|
||||||
|
2 3 win
|
||||||
|
|
||||||
|
3 1 win <-- !!
|
||||||
|
3 2 loss
|
||||||
|
3 3 draw
|
||||||
|
|
||||||
|
########
|
||||||
|
|
||||||
|
1 1 draw
|
||||||
|
1 2 win
|
||||||
|
1 0 loss <-- !!
|
||||||
|
|
||||||
|
1 0 loss
|
||||||
|
1 1 draw
|
||||||
|
1 2 win
|
||||||
|
|
||||||
|
1 0 win <-- !!
|
||||||
|
1 1 loss
|
||||||
|
1 2 draw
|
||||||
|
|
||||||
|
#################
|
||||||
|
part2
|
||||||
|
################
|
||||||
|
|
||||||
|
0 X lose: 2 => 3pts == 0 + 3 - 1 mod 3 = 2
|
||||||
|
0 Y draw: 0 => 1pts == 0 + 3 - 0 mod 3 = 0
|
||||||
|
0 Z win: 1 => 2pts == 0 + 3 + 1 mod 3 = 1
|
||||||
|
|
||||||
|
1 X lose: 0 => == 1 + 3 - 1 mod 3 = 0
|
||||||
|
1 Y draw: 1 == 1 + 3 + 0 mod 3 = 1
|
||||||
|
1 Z win: 2 == 1 + 3 + 1 mod 3 = 2
|
||||||
|
|
||||||
|
2 X lose: 1 == 2 + 3 - 1 mod 3 = 1
|
||||||
|
2 Y draw: 2 == 2 + 3 + 0 mod 3 = 2
|
||||||
|
2 Z win: 0 == 2 + 3 + 1 mod 3 = 0
|
Loading…
Reference in New Issue
Block a user