Day 2 puzzle
parent
8f28ad0005
commit
c82535a0b0
|
@ -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
|
|
@ -7,8 +7,13 @@ name = "aocrust"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"day1",
|
||||
"day2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -7,8 +7,10 @@ edition = "2021"
|
|||
|
||||
[workspace]
|
||||
members = [
|
||||
"day1"
|
||||
"day1",
|
||||
"day2"
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
day1 = { path = "./day1" }
|
||||
day1 = { path = "./day1" }
|
||||
day2 = { path = "./day2" }
|
|
@ -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]
|
File diff suppressed because it is too large
Load Diff
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
[toolchain]
|
||||
channel= "nightly"
|
|
@ -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"
|
|
@ -1,5 +1,6 @@
|
|||
use day1;
|
||||
|
||||
use day2;
|
||||
fn main() {
|
||||
day1::start();
|
||||
// day1::start();
|
||||
day2::start();
|
||||
}
|
||||
|
|
Reference in New Issue