day 2 :DDD

This commit is contained in:
Suguivy 2022-12-02 13:24:34 +01:00
parent f52ee7b545
commit 5f28c47370
5 changed files with 2614 additions and 23 deletions

4
Cargo.lock generated
View File

@ -6,6 +6,10 @@ version = 3
name = "day01"
version = "0.1.0"
[[package]]
name = "day02"
version = "0.1.0"
[[package]]
name = "tiesto"
version = "0.1.0"

View File

@ -4,27 +4,27 @@ members = [
"tiesto",
"day01",
"day02",
"day03",
"day04",
"day05",
"day06",
"day07",
"day08",
"day09",
"day10",
"day11",
"day12",
"day13",
"day14",
"day15",
"day16",
"day17",
"day18",
"day19",
"day20",
"day21",
"day22",
"day23",
"day24",
"day25",
# "day03",
# "day04",
# "day05",
# "day06",
# "day07",
# "day08",
# "day09",
# "day10",
# "day11",
# "day12",
# "day13",
# "day14",
# "day15",
# "day16",
# "day17",
# "day18",
# "day19",
# "day20",
# "day21",
# "day22",
# "day23",
# "day24",
# "day25",
]

8
day02/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day02"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2500
day02/input.txt Normal file

File diff suppressed because it is too large Load Diff

79
day02/src/main.rs Normal file
View File

@ -0,0 +1,79 @@
use std::fs;
fn main() {
const FILE_PATH: &str = "input.txt";
println!("Hi this is the second day of AOC2022, first we will read the file {}", FILE_PATH);
let contents = fs::read_to_string(FILE_PATH)
.expect("Should have been able to read the file");
println!("The amount of points you whould get is: {}", get_points(&contents, &get_round));
println!("The ammount of point you whould get using the real strategy are: {}", get_points(&contents, &get_true_round));
}
fn get_true_round(a: &str, b: &str) -> i32 {
let b = match b {
"X" => 0,
"Y" => 3,
"Z" => 6,
_ => panic!("WTF")
};
let a = match a {
"A" => 1,
"B" => 2,
"C" => 3,
_ => panic!("WTF")
};
((b/3+2)+a)%3+b
}
fn get_round(a: &str, b: &str) -> i32 {
let tot = match b {
"X" => 1,
"Y" => 2,
"Z" => 3,
_ => panic!("WTF")
};
let a = match a {
"A" => 1,
"B" => 2,
"C" => 3,
_ => panic!("WTF")
};
return match a - tot{
1 => 0,
0 => 3,
2 => 6,
-1 => 6,
-2 => 0,
_ => panic!("Numers")
} + tot
}
fn get_points(input: &str, f: &dyn Fn(&str,&str) -> i32) -> i32 {
input.lines().map(|l| {
let mut n = l.split_whitespace();
let a = n.next().expect("input must be correct");
f(a, n.next().expect("input must be correct"))
}
).sum()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn it_works() {
let input = r#"A Y
B X
C Z"#;
assert_eq!(get_points(input, &get_round), 15);
}
#[test]
fn it_works2() {
let input = r#"A Y
B X
C Z"#;
assert_eq!(get_points(input, &get_true_round), 12);
}
}