format

format
This commit is contained in:
Adam Jeniski 2022-12-04 17:14:45 -05:00
parent 375e2fb1c3
commit cad101cf54
2 changed files with 54 additions and 39 deletions

View File

@ -13,20 +13,32 @@ advent_of_code_macro::solve_problem!(
input.lines().map(str::to_owned).collect()
},
part one |data: Input| {
data.iter().map(|s| {
data.iter()
.map(|s| {
use std::collections::HashSet;
let (left, right) = s.split_at(s.len() / 2);
let right_set: HashSet<char> = right.chars().collect();
get_priority(left.chars().find(|c| right_set.contains(c)).unwrap())
get_priority(
left.chars()
.find(|c| right_set.contains(c))
.unwrap(),
)
}).sum()
},
part two |data: Input| {
data.as_slice().chunks(3).map(|c| {
data.as_slice()
.chunks(3)
.map(|c| {
use std::collections::HashSet;
let second_set: HashSet<char> = c[1].chars().collect();
let third_set: HashSet<char> = c[2].chars().collect();
get_priority(c[0].chars().find(|c| second_set.contains(c) && third_set.contains(c)).unwrap())
}).sum()
get_priority(
c[0].chars()
.find(|c| second_set.contains(c) && third_set.contains(c))
.unwrap(),
)
})
.sum()
},
sample tests [157, 70],
star tests [7908, 2838]

View File

@ -2,7 +2,8 @@ advent_of_code_macro::solve_problem!(
day 4,
Input Vec<Vec<i32>>,
parse |input: &str| {
input.lines().map(|line| {
input.lines()
.map(|line| {
let (a, rest) = line.split_once('-').unwrap();
let (b, rest) = rest.split_once(',').unwrap();
let (c, d) = rest.split_once('-').unwrap();
@ -13,7 +14,8 @@ advent_of_code_macro::solve_problem!(
}).collect()
},
part one |data: Input| {
data.into_iter().fold(0, |acc, curr|
data.into_iter()
.fold(0, |acc, curr|
if (curr[0] <= curr[2] && curr[1] >= curr[3])
|| (curr[2] <= curr[0] && curr[3] >= curr[1]) {
acc + 1
@ -23,7 +25,8 @@ advent_of_code_macro::solve_problem!(
)
},
part two |data: Input| {
data.into_iter().fold(0, |acc, curr|
data.into_iter()
.fold(0, |acc, curr|
if (curr[0] >= curr[2] && curr[0] <= curr[3])
|| (curr[1] >= curr[2] && curr[1] <= curr[3])
|| (curr[2] >= curr[0] && curr[2] <= curr[1])