Golf-y
This commit is contained in:
parent
0ad08975e6
commit
8dbd0cf9af
2 changed files with 23 additions and 36 deletions
|
@ -15,3 +15,6 @@ pub fn show_answers(answer1: &impl Debug, answer2: &impl Debug) {
|
|||
println!("Part 1: {:?}", answer1);
|
||||
println!("Part 2: {:?}", answer2);
|
||||
}
|
||||
pub fn show_both_answers((a, b): &(impl Debug, impl Debug)) {
|
||||
show_answers(a, b)
|
||||
}
|
||||
|
|
|
@ -1,56 +1,40 @@
|
|||
mod common;
|
||||
|
||||
fn main() {
|
||||
let input = common::day_input(4);
|
||||
println!("Part 1: {}", part1(&input));
|
||||
println!("Part 2: {}", part2(&input));
|
||||
}
|
||||
|
||||
fn nums(input: &str) -> Vec<i32> {
|
||||
fn processed_input(input: &str) -> Vec<Vec<i32>> {
|
||||
input
|
||||
.split(&['-', ','])
|
||||
.map(|s| s.parse::<i32>().unwrap())
|
||||
.lines()
|
||||
.map(|l| l.split(&['-', ',']).map(|s| s.parse().unwrap()).collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> i32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let n = nums(l);
|
||||
((n[0] <= n[2] && n[1] >= n[3]) || (n[2] <= n[0] && n[3] >= n[1])) as i32
|
||||
})
|
||||
.sum()
|
||||
fn both_parts(input: &Vec<Vec<i32>>) -> (usize, usize) {
|
||||
input.iter().fold((0, 0), |(a, b), n| {
|
||||
(
|
||||
(a + ((n[0] <= n[2] && n[1] >= n[3]) || (n[2] <= n[0] && n[3] >= n[1])) as usize),
|
||||
b + ((n[0] <= n[3] && n[2] <= n[1]) as usize),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> i32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let n = nums(l);
|
||||
(n[0] <= n[3] && n[2] <= n[1]) as i32
|
||||
})
|
||||
.sum()
|
||||
fn main() {
|
||||
let input = processed_input(&common::day_input(4));
|
||||
common::show_both_answers(&both_parts(&input))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "2-4,6-8
|
||||
#[test]
|
||||
fn test() {
|
||||
let input = processed_input(
|
||||
"2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8";
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
assert_eq!(part1(TEST_INPUT), 2)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
assert_eq!(part2(TEST_INPUT), 4)
|
||||
2-6,4-8",
|
||||
);
|
||||
assert_eq!(both_parts(&input), (2, 4));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue