Compare commits

...

4 Commits

Author SHA1 Message Date
4f996e20bc cleanup 2025-12-01 17:16:23 -08:00
a7c32b213f clippies 2025-12-01 17:15:54 -08:00
aed73754c8 cleanup 2025-12-01 17:11:56 -08:00
1971d5a95c day4: both solutions 2025-12-01 16:55:44 -08:00
6 changed files with 86 additions and 23 deletions

View File

@@ -20,12 +20,12 @@ fn parse(input: &[u8]) -> Vec<u64> {
}
#[aoc(day1, part1)]
fn part1(input: &Vec<u64>) -> u64 {
fn part1(input: &[u64]) -> u64 {
*input.iter().max().unwrap()
}
#[aoc(day1, part2)]
fn part2(input: &Vec<u64>) -> u64 {
fn part2(input: &[u64]) -> u64 {
input.iter().sorted().rev().take(3).sum::<u64>()
}

View File

@@ -93,7 +93,7 @@ pub struct Move {
impl FromStr for Move {
type Err = Box<dyn std::error::Error>;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let (theirs, ours) = value.split_once(&" ").ok_or("Unable to split")?;
let (theirs, ours) = value.split_once(" ").ok_or("Unable to split")?;
Ok(Move {
ours: ours.parse()?,
theirs: theirs.parse()?,
@@ -115,7 +115,7 @@ pub struct MoveGoal {
impl FromStr for MoveGoal {
type Err = Box<dyn std::error::Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (theirs, goal) = s.split_once(&" ").ok_or("Unable to split")?;
let (theirs, goal) = s.split_once(" ").ok_or("Unable to split")?;
Ok(MoveGoal {
theirs: theirs.parse()?,
goal: goal.parse()?,
@@ -142,12 +142,12 @@ fn parse_part2(input: &[u8]) -> Vec<MoveGoal> {
}
#[aoc(day2, part1)]
fn part1(input: &Vec<Move>) -> u64 {
fn part1(input: &[Move]) -> u64 {
input.iter().map(|m| m.score()).sum()
}
#[aoc(day2, part2)]
fn part2(input: &Vec<MoveGoal>) -> u64 {
fn part2(input: &[MoveGoal]) -> u64 {
input.iter().map(|mg| mg.get_move().score()).sum()
}

View File

@@ -8,9 +8,7 @@ struct Rucksack {
impl From<&[u8]> for Rucksack {
fn from(s: &[u8]) -> Self {
Self {
items: s.iter().cloned().collect(),
}
Self { items: s.to_vec() }
}
}
@@ -18,8 +16,8 @@ impl Display for Rucksack {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"{}{}",
String::from_utf8_lossy(&self.compartments()[0]),
String::from_utf8_lossy(&self.compartments()[1])
String::from_utf8_lossy(self.compartments()[0]),
String::from_utf8_lossy(self.compartments()[1])
))
}
}
@@ -31,16 +29,13 @@ impl Rucksack {
}
fn common(&self) -> u8 {
for item in self.compartments()[0] {
if self.compartments()[1].contains(&item) {
if self.compartments()[1].contains(item) {
return *item;
}
}
panic!("Common item not found");
}
fn contains(&self, item: &u8) -> bool {
self.compartments().into_iter().flatten().contains(item)
}
fn common_with<'a>(&self, other: &[u8]) -> impl Iterator<Item = u8> {
fn common_with(&self, other: &[u8]) -> impl Iterator<Item = u8> {
self.compartments()
.into_iter()
.flatten()
@@ -50,9 +45,9 @@ impl Rucksack {
}
fn priority(c: u8) -> u8 {
if c >= b'A' && c <= b'Z' {
if c.is_ascii_uppercase() {
c - b'A' + 27
} else if c >= b'a' && c <= b'z' {
} else if c.is_ascii_lowercase() {
c - b'a' + 1
} else {
panic!("Invalid item {c}")
@@ -65,12 +60,12 @@ fn parse(input: &[u8]) -> Vec<Rucksack> {
}
#[aoc(day3, part1)]
fn part1(input: &Vec<Rucksack>) -> u64 {
fn part1(input: &[Rucksack]) -> u64 {
input.iter().map(|sack| priority(sack.common()) as u64).sum()
}
#[aoc(day3, part2)]
fn part2(input: &Vec<Rucksack>) -> u64 {
fn part2(input: &[Rucksack]) -> u64 {
let mut total = 0u64;
for sacks in input.chunks(3) {
let commons_1 = sacks[0].common_with(&sacks[1].items).unique().collect_vec();

67
src/day4.rs Normal file
View File

@@ -0,0 +1,67 @@
use std::ops::RangeInclusive;
use aoc_runner_derive::{aoc, aoc_generator};
struct Pair([RangeInclusive<u64>; 2]);
impl From<&str> for Pair {
fn from(value: &str) -> Self {
let ranges = value.split_once(',').unwrap();
Pair([parse_range(ranges.0), parse_range(ranges.1)])
}
}
impl Pair {
fn fully_contained(&self) -> bool {
(self.0[0].start() <= self.0[1].start() && self.0[0].end() >= self.0[1].end())
|| (self.0[1].start() <= self.0[0].start() && self.0[1].end() >= self.0[0].end())
}
fn overlaps(&self) -> bool {
self.0[0].contains(self.0[1].start())
|| self.0[0].contains(self.0[1].end())
|| self.0[1].contains(self.0[0].start())
|| self.0[1].contains(self.0[0].end())
}
}
fn parse_range(s: &str) -> RangeInclusive<u64> {
let (lower, upper) = s.split_once('-').unwrap();
RangeInclusive::new(lower.parse().unwrap(), upper.parse().unwrap())
}
#[aoc_generator(day4)]
fn parse(input: &str) -> Vec<Pair> {
input.lines().map(|l| l.into()).collect()
}
#[aoc(day4, part1)]
fn part1(input: &[Pair]) -> u64 {
input.iter().filter(|p| p.fully_contained()).count() as u64
}
#[aoc(day4, part2)]
fn part2(input: &[Pair]) -> u64 {
input.iter().filter(|p| p.overlaps()).count() as u64
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "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 part1_example() {
assert_eq!(part1(&parse(EXAMPLE)), 2);
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 4);
}
}

View File

@@ -42,7 +42,7 @@ fn part2(input: &[u8]) -> u64 {
return None;
}
}
return Some(i + 14);
Some(i + 14)
})
.unwrap() as u64
}
@@ -57,7 +57,7 @@ mod tests {
const EXAMPLE4: &[u8] = b"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw";
#[test]
fn problem1_example() {
fn part1_example() {
assert_eq!(part1(EXAMPLE1), 5);
assert_eq!(part1(EXAMPLE2), 6);
assert_eq!(part1(EXAMPLE3), 10);
@@ -65,7 +65,7 @@ mod tests {
}
#[test]
fn problem2_example() {
fn part2_example() {
assert_eq!(part2(EXAMPLE1), 23);
assert_eq!(part2(EXAMPLE2), 23);
assert_eq!(part2(EXAMPLE3), 29);

View File

@@ -1,3 +1,4 @@
mod day4;
mod day1;
mod day2;
mod day3;