update macro api

This commit is contained in:
2022-12-04 17:56:26 -05:00
parent cad101cf54
commit ee1a97f61a
6 changed files with 147 additions and 130 deletions
+23 -12
View File
@@ -1,16 +1,27 @@
advent_of_code_macro::solve_problem!(
type Input = Vec<Vec<i32>>;
fn parse(input: &str) -> Input {
input
.split("\n\n")
.map(|lines| lines.lines().map(|line| line.parse().unwrap()).collect())
.collect()
}
fn part_one(data: Input) -> i32 {
data.iter().map(|cals| cals.iter().sum()).max().unwrap()
}
fn part_two(data: Input) -> i32 {
let mut cals: std::collections::BinaryHeap<i32> =
data.iter().map(|cals| cals.iter().sum()).collect();
cals.pop().unwrap() + cals.pop().unwrap() + cals.pop().unwrap()
}
advent_of_code_macro::generate_tests!(
day 1,
Input Vec<Vec<i32>>,
parse |input: &str| {
input.split("\n\n").map(|lines| lines.lines().map(|line| line.parse().unwrap()).collect()).collect()
},
part one |data: Input| {
data.iter().map(|cals| cals.iter().sum()).max().unwrap()
},
part two |data: Input| {
let mut cals: std::collections::BinaryHeap<i32> = data.iter().map(|cals| cals.iter().sum()).collect();
cals.pop().unwrap() + cals.pop().unwrap() + cals.pop().unwrap()
},
parse,
part_one,
part_two,
sample tests [24_000, 45_000],
star tests [68_923, 200_044]
);
+29 -18
View File
@@ -1,5 +1,7 @@
use Shape::*;
type Input = Vec<Vec<char>>;
#[derive(Debug, PartialEq, Eq)]
enum Shape {
Rock,
@@ -37,15 +39,15 @@ impl Shape {
}
}
}
advent_of_code_macro::solve_problem!(
day 2,
Input Vec<Vec<char>>,
parse |input: &str| {
input.lines().map(|line| line.split(' ').map(|s| s.chars().next().unwrap()).collect()).collect()
},
part one |data: Input| {
data.iter().map(|round| {
fn parse(input: &str) -> Input {
input
.lines()
.map(|line| line.split(' ').map(|s| s.chars().next().unwrap()).collect())
.collect()
}
fn part_one(data: Input) -> i32 {
data.iter()
.map(|round| {
let my_move = Shape::from_char(round[1]);
let other_move = Shape::from_char(round[0]);
let round_result = if my_move == other_move {
@@ -56,16 +58,25 @@ advent_of_code_macro::solve_problem!(
6
};
round_result + my_move.as_int()
}).sum()
},
part two |data: Input| {
data.iter().map(|round| match round[1] {
'X' => Shape::from_char(round[0]).wins_to().as_int(),
'Y' => Shape::from_char(round[0]).as_int() + 3,
'Z' => Shape::from_char(round[0]).loses_to().as_int() + 6,
})
.sum()
}
fn part_two(data: Input) -> i32 {
data.iter()
.map(|round| match round[1] {
'X' => Shape::from_char(round[0]).wins_to().as_int(),
'Y' => Shape::from_char(round[0]).as_int() + 3,
'Z' => Shape::from_char(round[0]).loses_to().as_int() + 6,
_ => panic!("expected X, Y, or Z"),
}).sum()
},
})
.sum()
}
advent_of_code_macro::generate_tests!(
day 2,
parse,
part_one,
part_two,
sample tests [15, 12],
star tests [11063, 10349]
);
+34 -33
View File
@@ -1,3 +1,5 @@
type Input = Vec<String>;
fn get_priority(c: char) -> i32 {
match c {
'a'..='z' => c as i32 - 'a' as i32 + 1,
@@ -5,41 +7,40 @@ fn get_priority(c: char) -> i32 {
_ => panic!("expected letter"),
}
}
fn parse(input: &str) -> Input {
input.lines().map(str::to_owned).collect()
}
fn part_one(data: Input) -> i32 {
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())
})
.sum()
}
fn part_two(data: Input) -> i32 {
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()
}
advent_of_code_macro::solve_problem!(
advent_of_code_macro::generate_tests!(
day 3,
Input Vec<String>,
parse |input: &str| {
input.lines().map(str::to_owned).collect()
},
part one |data: Input| {
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(),
)
}).sum()
},
part two |data: Input| {
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()
},
parse,
part_one,
part_two,
sample tests [157, 70],
star tests [7908, 2838]
);
+45 -38
View File
@@ -1,42 +1,49 @@
advent_of_code_macro::solve_problem!(
type Input = Vec<Vec<i32>>;
fn parse(input: &str) -> Input {
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();
[a, b, c, d]
.into_iter()
.map(str::parse)
.map(Result::unwrap)
.collect()
})
.collect()
}
fn part_one(data: Input) -> i32 {
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
} else {
acc
}
})
}
fn part_two(data: Input) -> i32 {
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])
|| (curr[3] >= curr[0] && curr[3] <= curr[1])
{
acc + 1
} else {
acc
}
})
}
advent_of_code_macro::generate_tests!(
day 4,
Input Vec<Vec<i32>>,
parse |input: &str| {
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();
[a, b, c, d].into_iter()
.map(str::parse)
.map(Result::unwrap)
.collect()
}).collect()
},
part one |data: Input| {
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
} else {
acc
}
)
},
part two |data: Input| {
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])
|| (curr[3] >= curr[0] && curr[3] <= curr[1]) {
acc + 1
} else {
acc
}
)
},
parse,
part_one,
part_two,
sample tests [2, 4],
star tests [450, 837]
);