Compare commits

..

No commits in common. "8f513f74cc97b5de825d6b647d70e734cd78bde3" and "a67033d39b7f6a8de5597c49a5986b139f98cc47" have entirely different histories.

9 changed files with 48 additions and 148 deletions

View file

@ -19,11 +19,13 @@ impl Solution for Day01 {
left.sort();
right.sort();
left.into_iter()
let sum: i32 = left
.into_iter()
.zip(right)
.map(|(a, b)| (a - b).abs())
.sum::<i32>()
.into()
.sum();
Answer::Number(sum as u64)
}
fn part_b(&self, input: &str) -> Answer {
@ -35,10 +37,12 @@ impl Solution for Day01 {
let counts = right.into_iter().counts();
left.into_iter()
let sum = left
.into_iter()
.map(|n| counts.get(&n).unwrap_or(&0) * (n as usize))
.sum::<usize>()
.into()
.sum::<usize>();
Answer::Number(sum as u64)
}
}

View file

@ -10,19 +10,20 @@ impl Solution for Day02 {
}
fn part_a(&self, input: &str) -> Answer {
input
let reports = input
.lines()
.filter(|l| {
let nums = l.split_whitespace().map(|n| n.parse::<i32>().unwrap());
is_valid(&nums)
})
.count()
.into()
.count();
Answer::Number(reports as u64)
}
fn part_b(&self, input: &str) -> Answer {
input
let reports = input
.lines()
.filter(|l| {
let nums = l.split_whitespace().map(|n| n.parse::<i32>().unwrap());
@ -44,8 +45,9 @@ impl Solution for Day02 {
valid
})
.count()
.into()
.count();
Answer::Number(reports as u64)
}
}

View file

@ -13,11 +13,13 @@ impl Solution for Day03 {
fn part_a(&self, input: &str) -> Answer {
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
re.captures_iter(input)
let sum = re
.captures_iter(input)
.map(|c| c.extract())
.map(|(_, [a, b])| a.parse::<u64>().unwrap() * b.parse::<u64>().unwrap())
.sum::<u64>()
.into()
.sum::<u64>();
Answer::Number(sum)
}
fn part_b(&self, input: &str) -> Answer {

View file

@ -17,7 +17,9 @@ impl Solution for Day04 {
.map(xmas_count)
.sum::<usize>();
(horizontal + vertical + diag_45 + diag_135).into()
let sum = horizontal + vertical + diag_45 + diag_135;
Answer::Number(sum as u64)
}
fn part_b(&self, input: &str) -> Answer {
@ -30,7 +32,7 @@ impl Solution for Day04 {
+ cross_mas_count(&input_180)
+ cross_mas_count(&input_270);
sum.into()
Answer::Number(sum as u64)
}
}

View file

@ -21,7 +21,7 @@ impl Solution for Day05 {
.into_grouping_map()
.collect::<HashSet<_>>();
input
let sum = input
.lines()
.map(|l| {
l.split(",")
@ -33,8 +33,9 @@ impl Solution for Day05 {
let middle = (nums.len() - 1) / 2;
nums[middle]
})
.sum::<u64>()
.into()
.sum::<u64>();
Answer::Number(sum)
}
fn part_b(&self, input: &str) -> Answer {
@ -47,7 +48,7 @@ impl Solution for Day05 {
.into_grouping_map()
.collect::<HashSet<_>>();
input
let sum = input
.lines()
.map(|l| {
l.split(",")
@ -82,8 +83,9 @@ impl Solution for Day05 {
let middle = (nums.len() - 1) / 2;
nums[middle]
})
.sum::<u64>()
.into()
.sum::<u64>();
Answer::Number(sum)
}
}

View file

@ -27,7 +27,7 @@ impl Solution for Day06 {
direction: Direction::Up,
};
get_positions(&grid, guard).len().into()
Answer::Number(get_positions(&grid, guard).len() as u64)
}
fn part_b(&self, input: &str) -> Answer {
@ -48,7 +48,7 @@ impl Solution for Day06 {
direction: Direction::Up,
};
get_positions(&grid, guard)
let count = get_positions(&grid, guard)
.iter()
.filter(|&&coords| {
let tmp_guard = Guard {
@ -58,8 +58,9 @@ impl Solution for Day06 {
is_infinite_loop(&grid, tmp_guard, coords)
})
.count()
.into()
.count();
Answer::Number(count as u64)
}
}

View file

@ -8,7 +8,7 @@ impl Solution for Day07 {
}
fn part_a(&self, input: &str) -> Answer {
input
let sum = input
.lines()
.map(|l| l.split_once(": ").unwrap())
.map(|(a, b)| {
@ -38,12 +38,13 @@ impl Solution for Day07 {
None
})
.sum::<u64>()
.into()
.sum::<u64>();
Answer::Number(sum)
}
fn part_b(&self, input: &str) -> Answer {
input
let sum = input
.lines()
.map(|l| l.split_once(": ").unwrap())
.map(|(a, b)| {
@ -73,8 +74,9 @@ impl Solution for Day07 {
None
})
.sum::<u64>()
.into()
.sum::<u64>();
Answer::Number(sum)
}
}

View file

@ -1,113 +0,0 @@
use itertools::Itertools;
use crate::common::{Answer, Solution};
pub struct Day08;
impl Solution for Day08 {
fn name(&self) -> &'static str {
"Resonant Collinearity"
}
fn part_a(&self, input: &str) -> Answer {
let bound_x = 0..(input.lines().count() as i64);
let bound_y = 0..(input.lines().next().unwrap().len() as i64);
let antennas = input
.lines()
.enumerate()
.flat_map(|(x, l)| {
l.chars()
.enumerate()
.filter(|(_, c)| *c != '.')
.map(move |(y, c)| (c, (x as i64, y as i64)))
})
.into_group_map();
antennas
.iter()
.flat_map(|(_, pos)| pos.iter().tuple_combinations::<(_, _)>())
.flat_map(|((x1, y1), (x2, y2))| {
let diff_x = x1 - x2;
let diff_y = y1 - y2;
[(x1 + diff_x, y1 + diff_y), (x2 - diff_x, y2 - diff_y)]
})
.filter(|(x, y)| bound_x.contains(x) && bound_y.contains(y))
.unique()
.count()
.into()
}
fn part_b(&self, input: &str) -> Answer {
let bound_x = 0..(input.lines().count() as i64);
let bound_y = 0..(input.lines().next().unwrap().len() as i64);
let antennas = input
.lines()
.enumerate()
.flat_map(|(x, l)| {
l.chars()
.enumerate()
.filter(|(_, c)| *c != '.')
.map(move |(y, c)| (c, (x as i64, y as i64)))
})
.into_group_map();
antennas
.iter()
.flat_map(|(_, pos)| pos.iter().tuple_combinations::<(_, _)>())
.flat_map(|((x1, y1), (x2, y2))| {
let diff_x = x1 - x2;
let diff_y = y1 - y2;
// we go from 0 because antennas also count as antinodes here
let before = (0..)
.map(move |n| (x1 + diff_x * n, y1 + diff_y * n))
.take_while(|(x, y)| bound_x.contains(x) && bound_y.contains(y));
let after = (0..)
.map(move |n| (x2 - diff_x * n, y2 - diff_y * n))
.take_while(|(x, y)| bound_x.contains(x) && bound_y.contains(y));
before.chain(after)
})
.filter(|(x, y)| bound_x.contains(x) && bound_y.contains(y))
.unique()
.count()
.into()
}
}
#[cfg(test)]
mod test {
use super::Day08;
use crate::common::Solution;
use indoc::indoc;
const INPUT: &str = indoc! {"
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"};
#[test]
fn part_a() {
assert_eq!(Day08.part_a(INPUT), 14.into());
}
#[test]
fn part_b() {
assert_eq!(Day08.part_b(INPUT), 34.into());
}
}

View file

@ -7,7 +7,6 @@ mod day_04;
mod day_05;
mod day_06;
mod day_07;
mod day_08;
pub const SOLUTIONS: &[&dyn Solution] = &[
&day_01::Day01,
@ -17,5 +16,4 @@ pub const SOLUTIONS: &[&dyn Solution] = &[
&day_05::Day05,
&day_06::Day06,
&day_07::Day07,
&day_08::Day08,
];