mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
format
format format
This commit is contained in:
parent
375e2fb1c3
commit
cad101cf54
@ -13,20 +13,32 @@ advent_of_code_macro::solve_problem!(
|
|||||||
input.lines().map(str::to_owned).collect()
|
input.lines().map(str::to_owned).collect()
|
||||||
},
|
},
|
||||||
part one |data: Input| {
|
part one |data: Input| {
|
||||||
data.iter().map(|s| {
|
data.iter()
|
||||||
use std::collections::HashSet;
|
.map(|s| {
|
||||||
let (left, right) = s.split_at(s.len() / 2);
|
use std::collections::HashSet;
|
||||||
let right_set: HashSet<char> = right.chars().collect();
|
let (left, right) = s.split_at(s.len() / 2);
|
||||||
get_priority(left.chars().find(|c| right_set.contains(c)).unwrap())
|
let right_set: HashSet<char> = right.chars().collect();
|
||||||
}).sum()
|
get_priority(
|
||||||
|
left.chars()
|
||||||
|
.find(|c| right_set.contains(c))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
}).sum()
|
||||||
},
|
},
|
||||||
part two |data: Input| {
|
part two |data: Input| {
|
||||||
data.as_slice().chunks(3).map(|c| {
|
data.as_slice()
|
||||||
use std::collections::HashSet;
|
.chunks(3)
|
||||||
let second_set: HashSet<char> = c[1].chars().collect();
|
.map(|c| {
|
||||||
let third_set: HashSet<char> = c[2].chars().collect();
|
use std::collections::HashSet;
|
||||||
get_priority(c[0].chars().find(|c| second_set.contains(c) && third_set.contains(c)).unwrap())
|
let second_set: HashSet<char> = c[1].chars().collect();
|
||||||
}).sum()
|
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()
|
||||||
},
|
},
|
||||||
sample tests [157, 70],
|
sample tests [157, 70],
|
||||||
star tests [7908, 2838]
|
star tests [7908, 2838]
|
||||||
|
@ -2,37 +2,40 @@ advent_of_code_macro::solve_problem!(
|
|||||||
day 4,
|
day 4,
|
||||||
Input Vec<Vec<i32>>,
|
Input Vec<Vec<i32>>,
|
||||||
parse |input: &str| {
|
parse |input: &str| {
|
||||||
input.lines().map(|line| {
|
input.lines()
|
||||||
let (a, rest) = line.split_once('-').unwrap();
|
.map(|line| {
|
||||||
let (b, rest) = rest.split_once(',').unwrap();
|
let (a, rest) = line.split_once('-').unwrap();
|
||||||
let (c, d) = rest.split_once('-').unwrap();
|
let (b, rest) = rest.split_once(',').unwrap();
|
||||||
[a, b, c, d].into_iter()
|
let (c, d) = rest.split_once('-').unwrap();
|
||||||
.map(str::parse)
|
[a, b, c, d].into_iter()
|
||||||
.map(Result::unwrap)
|
.map(str::parse)
|
||||||
.collect()
|
.map(Result::unwrap)
|
||||||
}).collect()
|
.collect()
|
||||||
|
}).collect()
|
||||||
},
|
},
|
||||||
part one |data: Input| {
|
part one |data: Input| {
|
||||||
data.into_iter().fold(0, |acc, curr|
|
data.into_iter()
|
||||||
if (curr[0] <= curr[2] && curr[1] >= curr[3])
|
.fold(0, |acc, curr|
|
||||||
|| (curr[2] <= curr[0] && curr[3] >= curr[1]) {
|
if (curr[0] <= curr[2] && curr[1] >= curr[3])
|
||||||
acc + 1
|
|| (curr[2] <= curr[0] && curr[3] >= curr[1]) {
|
||||||
} else {
|
acc + 1
|
||||||
acc
|
} else {
|
||||||
}
|
acc
|
||||||
)
|
}
|
||||||
|
)
|
||||||
},
|
},
|
||||||
part two |data: Input| {
|
part two |data: Input| {
|
||||||
data.into_iter().fold(0, |acc, curr|
|
data.into_iter()
|
||||||
if (curr[0] >= curr[2] && curr[0] <= curr[3])
|
.fold(0, |acc, curr|
|
||||||
|| (curr[1] >= curr[2] && curr[1] <= curr[3])
|
if (curr[0] >= curr[2] && curr[0] <= curr[3])
|
||||||
|| (curr[2] >= curr[0] && curr[2] <= curr[1])
|
|| (curr[1] >= curr[2] && curr[1] <= curr[3])
|
||||||
|| (curr[3] >= curr[0] && curr[3] <= curr[1]) {
|
|| (curr[2] >= curr[0] && curr[2] <= curr[1])
|
||||||
acc + 1
|
|| (curr[3] >= curr[0] && curr[3] <= curr[1]) {
|
||||||
} else {
|
acc + 1
|
||||||
acc
|
} else {
|
||||||
}
|
acc
|
||||||
)
|
}
|
||||||
|
)
|
||||||
},
|
},
|
||||||
sample tests [2, 4],
|
sample tests [2, 4],
|
||||||
star tests [450, 837]
|
star tests [450, 837]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user