summaryrefslogtreecommitdiffstats
path: root/src/2021/7/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/2021/7/mod.rs')
-rw-r--r--src/2021/7/mod.rs23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/2021/7/mod.rs b/src/2021/7/mod.rs
index 1d8320a..ce942dc 100644
--- a/src/2021/7/mod.rs
+++ b/src/2021/7/mod.rs
@@ -1,5 +1,11 @@
-pub fn part1() -> anyhow::Result<i64> {
- let crabs: Vec<_> = data_ints!(b',')?.collect();
+pub fn parse(fh: std::fs::File) -> anyhow::Result<Vec<i64>> {
+ Ok(
+ crate::util::parse::ints(crate::util::parse::split(fh, b','))
+ .collect(),
+ )
+}
+
+pub fn part1(crabs: Vec<i64>) -> anyhow::Result<i64> {
Ok((0..=crabs.iter().copied().max().unwrap())
.map(|start| {
crabs.iter().copied().map(|crab| (crab - start).abs()).sum()
@@ -8,8 +14,7 @@ pub fn part1() -> anyhow::Result<i64> {
.unwrap())
}
-pub fn part2() -> anyhow::Result<i64> {
- let crabs: Vec<_> = data_ints!(b',')?.collect();
+pub fn part2(crabs: Vec<i64>) -> anyhow::Result<i64> {
Ok((0..=crabs.iter().copied().max().unwrap())
.map(|start| {
crabs
@@ -27,6 +32,12 @@ pub fn part2() -> anyhow::Result<i64> {
#[test]
fn test() {
- assert_eq!(part1().unwrap(), 333755);
- assert_eq!(part2().unwrap(), 94017638);
+ assert_eq!(
+ part1(parse(crate::util::data(2021, 7).unwrap()).unwrap()).unwrap(),
+ 333755
+ );
+ assert_eq!(
+ part2(parse(crate::util::data(2021, 7).unwrap()).unwrap()).unwrap(),
+ 94017638
+ );
}