Try gaining performance.. fail
This commit is contained in:
parent
9e4c93628d
commit
43c1425903
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -23,6 +23,13 @@ dependencies = [
|
||||||
name = "day02"
|
name = "day02"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day02-gen-problem"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "day03"
|
name = "day03"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [ "day01", "day01-gen-problem", "day02", "day03", "day04", "day05", "day06", "day07", "day08", "day09", "day10", "day11", "day12", "day13", "day14", "day15", "day16", "day17", "day18", "day19", "day20", "day21", "day22", "day23", "day24", ]
|
members = [ "day01", "day01-gen-problem", "day02", "day02-gen-problem", "day03", "day04", "day05", "day06", "day07", "day08", "day09", "day10", "day11", "day12", "day13", "day14", "day15", "day16", "day17", "day18", "day19", "day20", "day21", "day22", "day23", "day24", ]
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
#debug = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
strip = "symbols"
|
strip = "symbols"
|
||||||
lto = true
|
lto = true
|
||||||
|
|
9
day02-gen-problem/Cargo.toml
Normal file
9
day02-gen-problem/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "day02-gen-problem"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
29
day02-gen-problem/src/main.rs
Normal file
29
day02-gen-problem/src/main.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use std::{fs::File, io::BufWriter, io::Write};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
const LINE_COUNT: usize = 20_000_000;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let outfile = ::std::env::args()
|
||||||
|
.skip(1)
|
||||||
|
.next()
|
||||||
|
.expect("Output file as first parameter");
|
||||||
|
let mut outfile = BufWriter::new(File::create(outfile).expect("Can write outfile"));
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
(0..LINE_COUNT)
|
||||||
|
.map(|_| match rng.gen_range(1..=9) {
|
||||||
|
1 => "A X",
|
||||||
|
2 => "B X",
|
||||||
|
3 => "C X",
|
||||||
|
4 => "A Y",
|
||||||
|
5 => "B Y",
|
||||||
|
6 => "C Y",
|
||||||
|
7 => "A Z",
|
||||||
|
8 => "B Z",
|
||||||
|
9 => "C Z",
|
||||||
|
_ => unreachable!(),
|
||||||
|
})
|
||||||
|
.for_each(|line| writeln!(outfile, "{line}").expect("Writing works"));
|
||||||
|
outfile.flush().expect("Flushing works");
|
||||||
|
}
|
|
@ -8,3 +8,6 @@ build = true
|
||||||
app = true
|
app = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
first = []
|
||||||
|
|
|
@ -3,37 +3,37 @@ use std::{
|
||||||
io::{BufRead, BufReader},
|
io::{BufRead, BufReader},
|
||||||
};
|
};
|
||||||
|
|
||||||
// fn calculate_game_score<S: AsRef<str>>(line: S) -> usize {
|
fn calculate_game_score(line: String) -> usize {
|
||||||
// let line = line.as_ref();
|
let line = line.as_bytes();
|
||||||
// match line {
|
let left = line[0] - b'A';
|
||||||
// "A X" => 1 + 3, // Rock -- Rock
|
let right = line[2] - b'X';
|
||||||
// "B X" => 1 + 0, // Paper -> Rock
|
#[cfg(feature = "first")]
|
||||||
// "C X" => 1 + 6, // Scissor <- Rock
|
|
||||||
// "A Y" => 2 + 6,
|
|
||||||
// "B Y" => 2 + 3,
|
|
||||||
// "C Y" => 2 + 0,
|
|
||||||
// "A Z" => 3 + 0,
|
|
||||||
// "B Z" => 3 + 6,
|
|
||||||
// "C Z" => 3 + 3,
|
|
||||||
// _ => unreachable!(),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn calculate_game_score<S: AsRef<str>>(line: S) -> usize {
|
|
||||||
let line = line.as_ref();
|
|
||||||
match line {
|
match line {
|
||||||
|
(0, 0) => 1 + 3,
|
||||||
|
(1, 0) => 1 + 0,
|
||||||
|
(2, 0) => 1 + 6,
|
||||||
|
(0, 1) => 2 + 6,
|
||||||
|
(1, 1) => 2 + 3,
|
||||||
|
(2, 1) => 2 + 0,
|
||||||
|
(0, 2) => 3 + 0,
|
||||||
|
(1, 2) => 3 + 6,
|
||||||
|
(2, 2) => 3 + 3,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "first"))]
|
||||||
|
match (left, right) {
|
||||||
// Rock -> 1
|
// Rock -> 1
|
||||||
// Paper -> 2
|
// Paper -> 2
|
||||||
// Scissor -> 3
|
// Scissor -> 3
|
||||||
"A X" => 3 + 0,
|
(0, 0) => 3 + 0,
|
||||||
"B X" => 1 + 0,
|
(1, 0) => 1 + 0,
|
||||||
"C X" => 2 + 0,
|
(2, 0) => 2 + 0,
|
||||||
"A Y" => 1 + 3,
|
(0, 1) => 1 + 3,
|
||||||
"B Y" => 2 + 3,
|
(1, 1) => 2 + 3,
|
||||||
"C Y" => 3 + 3,
|
(2, 1) => 3 + 3,
|
||||||
"A Z" => 2 + 6,
|
(0, 2) => 2 + 6,
|
||||||
"B Z" => 3 + 6,
|
(1, 2) => 3 + 6,
|
||||||
"C Z" => 1 + 6,
|
(2, 2) => 1 + 6,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue