Add day7
This commit is contained in:
parent
1ae8f8caf9
commit
49975b4b0f
61
day7/Cargo.lock
generated
Normal file
61
day7/Cargo.lock
generated
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day7"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rayon",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
"rayon-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rayon-core"
|
||||||
|
version = "1.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
7
day7/Cargo.toml
Normal file
7
day7/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "day7"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rayon = "1.10.0"
|
||||||
48
day7/src/main.rs
Normal file
48
day7/src/main.rs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
use rayon::prelude::*;
|
||||||
|
|
||||||
|
fn parse(input: &str) -> Vec<Vec<usize>> {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
line.replace(':', "")
|
||||||
|
.split(' ')
|
||||||
|
.map(|num| num.parse().unwrap())
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_possible(res: usize, curr: usize, nums: &[usize], part2: bool) -> bool {
|
||||||
|
if curr > res {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if nums.is_empty() {
|
||||||
|
return res == curr;
|
||||||
|
}
|
||||||
|
is_possible(res, curr * nums[0], &nums[1..], part2)
|
||||||
|
|| is_possible(res, curr + nums[0], &nums[1..], part2)
|
||||||
|
|| if part2 {
|
||||||
|
is_possible(
|
||||||
|
res,
|
||||||
|
curr * 10usize.pow(nums[0].ilog10() + 1) + nums[0],
|
||||||
|
&nums[1..],
|
||||||
|
part2,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part12(input: &[Vec<usize>], part2: bool) -> usize {
|
||||||
|
input
|
||||||
|
.par_iter()
|
||||||
|
.filter(|l| is_possible(l[0], l[1], &l[2..], part2))
|
||||||
|
.map(|l| l[0])
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = parse(include_str!("../input.txt"));
|
||||||
|
println!("{}", part12(&input, false));
|
||||||
|
println!("{}", part12(&input, true));
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user