Day 2
This commit is contained in:
parent
bbdab23098
commit
8d235ecbc4
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -9,3 +9,11 @@ version = "0.1.0"
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "d01t2"
|
name = "d01t2"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d02t1"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d02t2"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
|
@ -4,4 +4,6 @@ resolver = "2"
|
||||||
members = [
|
members = [
|
||||||
"d01t1",
|
"d01t1",
|
||||||
"d01t2",
|
"d01t2",
|
||||||
|
"d02t1",
|
||||||
|
"d02t2",
|
||||||
]
|
]
|
||||||
|
|
8
d02t1/Cargo.toml
Normal file
8
d02t1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "d02t1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
22
d02t1/src/main.rs
Normal file
22
d02t1/src/main.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
fn main() {
|
||||||
|
let input = include_str!("../input.txt");
|
||||||
|
let mut result = 0;
|
||||||
|
for line in input.lines() {
|
||||||
|
let (game_id, game_values) = line.split_once(": ").unwrap();
|
||||||
|
if !game_values.split("; ").any(|die_set| {
|
||||||
|
die_set.split(", ").any(|v| {
|
||||||
|
let (val, color) = v.split_once(" ").unwrap();
|
||||||
|
let val = val.parse::<usize>().unwrap();
|
||||||
|
match color {
|
||||||
|
"red" => val > 12,
|
||||||
|
"green" => val > 13,
|
||||||
|
"blue" => val > 14,
|
||||||
|
c => panic!("unknown color {c}"),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}) {
|
||||||
|
result += game_id.split_once(" ").unwrap().1.parse::<i32>().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("d02t1: {result}");
|
||||||
|
}
|
8
d02t2/Cargo.toml
Normal file
8
d02t2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "d02t2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
24
d02t2/src/main.rs
Normal file
24
d02t2/src/main.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
fn main() {
|
||||||
|
let input = include_str!("../input.txt");
|
||||||
|
let result = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let (_game_id, game_values) = line.split_once(": ").unwrap();
|
||||||
|
let mut min_red = 0;
|
||||||
|
let mut min_green = 0;
|
||||||
|
let mut min_blue = 0;
|
||||||
|
game_values.replace(";", ",").split(", ").for_each(|v| {
|
||||||
|
let (val, color) = v.split_once(" ").unwrap();
|
||||||
|
let val = val.parse::<usize>().unwrap();
|
||||||
|
match color {
|
||||||
|
"red" => min_red = min_red.max(val),
|
||||||
|
"green" => min_green = min_green.max(val),
|
||||||
|
"blue" => min_blue = min_blue.max(val),
|
||||||
|
c => panic!("unknown color {c}"),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
min_red * min_green * min_blue
|
||||||
|
})
|
||||||
|
.sum::<usize>();
|
||||||
|
println!("d02t2: {result}");
|
||||||
|
}
|
Loading…
Reference in a new issue