Day 2 puzzle

master
Wynd 2022-12-02 13:07:53 +02:00
parent 8f28ad0005
commit c82535a0b0
9 changed files with 2617 additions and 4 deletions

17
.editorconfig 100644
View File

@ -0,0 +1,17 @@
# Editor configuration, see https://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = false
trim_trailing_whitespace = true
[*.md]
max_line_length = off
trim_trailing_whitespace = false
[*.yml]
indent_style = space
indent_size = 2

5
Cargo.lock generated
View File

@ -7,8 +7,13 @@ name = "aocrust"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"day1", "day1",
"day2",
] ]
[[package]] [[package]]
name = "day1" name = "day1"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "day2"
version = "0.1.0"

View File

@ -7,8 +7,10 @@ edition = "2021"
[workspace] [workspace]
members = [ members = [
"day1" "day1",
"day2"
] ]
[dependencies] [dependencies]
day1 = { path = "./day1" } day1 = { path = "./day1" }
day2 = { path = "./day2" }

8
day2/Cargo.toml 100644
View File

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

2500
day2/src/input.txt 100644

File diff suppressed because it is too large Load Diff

70
day2/src/lib.rs 100644
View File

@ -0,0 +1,70 @@
use std::fs;
pub fn start() {
let input = fs::read_to_string("./day2/src/input.txt").unwrap();
let rounds = input.split("\n");
let b = ["X", "Y", "Z"];
let a = ["A", "B", "C"];
let mut score1 = 0;
let mut score2 = 0;
rounds.map(|f| f.split_at(1))
.map(|f| {
let p = b.into_iter().position(|x| x == f.1.replace(" ", "")).unwrap();
let e = a.into_iter().position(|x| x == f.0.replace(" ", "")).unwrap();
(p, e)
})
.for_each(|f| {
let bonus;
let mut points = 0;
// draw
if f.0 == 1 {
bonus = 3;
points = f.1 + 1;
}// win
else if f.0 == 2 {
bonus = 6;
if f.1 == 0 {
points = 2;
}
else if f.1 == 1 {
points = 3;
}
else if f.1 == 2 {
points = 1;
}
}// lose
else {
bonus = 0;
if f.1 == 0 {
points = 3;
}
else if f.1 == 1 {
points = 1;
}
else if f.1 == 2 {
points = 2;
}
}
score2 += points + bonus;
let bonus: usize;
let points = f.0 + 1;
// draw
if f.0 == f.1 {
bonus = 3;
}// win
else if f.0 == 0 && f.1 == 2 || f.0 == 1 && f.1 == 0 || f.0 == 2 && f.1 == 1 {
bonus = 6;
}// lose
else {
bonus = 0;
}
score1 += points + bonus;
});
println!("{:?}", score1);
println!("{:?}", score2);
}

View File

@ -0,0 +1,2 @@
[toolchain]
channel= "nightly"

8
rustfmt.toml 100644
View File

@ -0,0 +1,8 @@
unstable_features = true
reorder_imports = true
hard_tabs = true
control_brace_style = "ClosingNextLine"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
edition = "2021"
newline_style = "Unix"

View File

@ -1,5 +1,6 @@
use day1; use day1;
use day2;
fn main() { fn main() {
day1::start(); // day1::start();
day2::start();
} }