chore: use into for answers
This commit is contained in:
parent
a67033d39b
commit
6a54618880
7 changed files with 33 additions and 48 deletions
|
@ -19,13 +19,11 @@ impl Solution for Day01 {
|
|||
left.sort();
|
||||
right.sort();
|
||||
|
||||
let sum: i32 = left
|
||||
.into_iter()
|
||||
left.into_iter()
|
||||
.zip(right)
|
||||
.map(|(a, b)| (a - b).abs())
|
||||
.sum();
|
||||
|
||||
Answer::Number(sum as u64)
|
||||
.sum::<i32>()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
|
@ -37,12 +35,10 @@ impl Solution for Day01 {
|
|||
|
||||
let counts = right.into_iter().counts();
|
||||
|
||||
let sum = left
|
||||
.into_iter()
|
||||
left.into_iter()
|
||||
.map(|n| counts.get(&n).unwrap_or(&0) * (n as usize))
|
||||
.sum::<usize>();
|
||||
|
||||
Answer::Number(sum as u64)
|
||||
.sum::<usize>()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,20 +10,19 @@ impl Solution for Day02 {
|
|||
}
|
||||
|
||||
fn part_a(&self, input: &str) -> Answer {
|
||||
let reports = input
|
||||
input
|
||||
.lines()
|
||||
.filter(|l| {
|
||||
let nums = l.split_whitespace().map(|n| n.parse::<i32>().unwrap());
|
||||
|
||||
is_valid(&nums)
|
||||
})
|
||||
.count();
|
||||
|
||||
Answer::Number(reports as u64)
|
||||
.count()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
let reports = input
|
||||
input
|
||||
.lines()
|
||||
.filter(|l| {
|
||||
let nums = l.split_whitespace().map(|n| n.parse::<i32>().unwrap());
|
||||
|
@ -45,9 +44,8 @@ impl Solution for Day02 {
|
|||
|
||||
valid
|
||||
})
|
||||
.count();
|
||||
|
||||
Answer::Number(reports as u64)
|
||||
.count()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,13 +13,11 @@ impl Solution for Day03 {
|
|||
fn part_a(&self, input: &str) -> Answer {
|
||||
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
|
||||
|
||||
let sum = re
|
||||
.captures_iter(input)
|
||||
re.captures_iter(input)
|
||||
.map(|c| c.extract())
|
||||
.map(|(_, [a, b])| a.parse::<u64>().unwrap() * b.parse::<u64>().unwrap())
|
||||
.sum::<u64>();
|
||||
|
||||
Answer::Number(sum)
|
||||
.sum::<u64>()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
|
|
|
@ -17,9 +17,7 @@ impl Solution for Day04 {
|
|||
.map(xmas_count)
|
||||
.sum::<usize>();
|
||||
|
||||
let sum = horizontal + vertical + diag_45 + diag_135;
|
||||
|
||||
Answer::Number(sum as u64)
|
||||
(horizontal + vertical + diag_45 + diag_135).into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
|
@ -32,7 +30,7 @@ impl Solution for Day04 {
|
|||
+ cross_mas_count(&input_180)
|
||||
+ cross_mas_count(&input_270);
|
||||
|
||||
Answer::Number(sum as u64)
|
||||
sum.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ impl Solution for Day05 {
|
|||
.into_grouping_map()
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let sum = input
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
l.split(",")
|
||||
|
@ -33,9 +33,8 @@ impl Solution for Day05 {
|
|||
let middle = (nums.len() - 1) / 2;
|
||||
nums[middle]
|
||||
})
|
||||
.sum::<u64>();
|
||||
|
||||
Answer::Number(sum)
|
||||
.sum::<u64>()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
|
@ -48,7 +47,7 @@ impl Solution for Day05 {
|
|||
.into_grouping_map()
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
let sum = input
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
l.split(",")
|
||||
|
@ -83,9 +82,8 @@ impl Solution for Day05 {
|
|||
let middle = (nums.len() - 1) / 2;
|
||||
nums[middle]
|
||||
})
|
||||
.sum::<u64>();
|
||||
|
||||
Answer::Number(sum)
|
||||
.sum::<u64>()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ impl Solution for Day06 {
|
|||
direction: Direction::Up,
|
||||
};
|
||||
|
||||
Answer::Number(get_positions(&grid, guard).len() as u64)
|
||||
get_positions(&grid, guard).len().into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
|
@ -48,7 +48,7 @@ impl Solution for Day06 {
|
|||
direction: Direction::Up,
|
||||
};
|
||||
|
||||
let count = get_positions(&grid, guard)
|
||||
get_positions(&grid, guard)
|
||||
.iter()
|
||||
.filter(|&&coords| {
|
||||
let tmp_guard = Guard {
|
||||
|
@ -58,9 +58,8 @@ impl Solution for Day06 {
|
|||
|
||||
is_infinite_loop(&grid, tmp_guard, coords)
|
||||
})
|
||||
.count();
|
||||
|
||||
Answer::Number(count as u64)
|
||||
.count()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ impl Solution for Day07 {
|
|||
}
|
||||
|
||||
fn part_a(&self, input: &str) -> Answer {
|
||||
let sum = input
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.split_once(": ").unwrap())
|
||||
.map(|(a, b)| {
|
||||
|
@ -38,13 +38,12 @@ impl Solution for Day07 {
|
|||
|
||||
None
|
||||
})
|
||||
.sum::<u64>();
|
||||
|
||||
Answer::Number(sum)
|
||||
.sum::<u64>()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn part_b(&self, input: &str) -> Answer {
|
||||
let sum = input
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.split_once(": ").unwrap())
|
||||
.map(|(a, b)| {
|
||||
|
@ -74,9 +73,8 @@ impl Solution for Day07 {
|
|||
|
||||
None
|
||||
})
|
||||
.sum::<u64>();
|
||||
|
||||
Answer::Number(sum)
|
||||
.sum::<u64>()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue