day2: improvements and clippies

This commit is contained in:
ktims committed 2026-12-01 23:03:27 -07:00
1 parent 5507b187fd
commit 144d63eb66
1 file changed
+28 -29
+28 -29
View File
@@ -1,9 +1,14 @@
use aoc_runner_derive::{aoc, aoc_generator};
use itertools::Itertools;
// #[aoc_generator(day2)]
// fn parse(input: &str) -> {
// todo!()
// }
#[aoc_generator(day2)]
fn parse(input: &str) -> Vec<u8> {
input
.as_bytes()
.iter()
.map(|i| SEQUENCE.iter().position(|c| c == i).unwrap() as u8)
.collect_vec()
}
const SEQUENCE: &[u8] = b"roygbv";
@@ -17,52 +22,46 @@ fn move_distance(start: u8, end: u8) -> i64 {
}
#[aoc(day2, part1)]
fn part1(input: &str) -> u64 {
let instructions = input.as_bytes();
fn part1(input: &[u8]) -> u64 {
let mut cur_pos = 0i64;
for i in instructions {
let next_pos = SEQUENCE.iter().position(|c| c == i).unwrap() as u8;
cur_pos += move_distance((cur_pos % SEQUENCE.len() as i64) as u8, next_pos);
for i in input {
cur_pos += move_distance((cur_pos % SEQUENCE.len() as i64) as u8, *i);
}
cur_pos as u64
}
#[aoc(day2, part2)]
fn part2(input: &str) -> u64 {
let mut instructions = input
.as_bytes()
.iter()
.map(|i| SEQUENCE.iter().position(|c| c == i).unwrap() as u8)
.collect_vec();
fn part2(input: &[u8]) -> u64 {
let mut instructions = input.to_owned();
// bring like instructions together, but this will minimize the distance between blocks
// bring same instructions together, but this will minimize the distance between chunks so we need to spread them again
instructions.sort_unstable();
let mut chunks = instructions.chunk_by(|a, b| a == b).collect_vec();
let mut final_chunks = Vec::new();
// implement a very stupid selection algorithm for chunks
// implement a very stupid selection algorithm for chunks.
// we are already sorted so there is probably an optimal way to place them directly.
// maybe alternating once we find the 'pivot' or something.
// but this is plenty fast.
let mut cur_pos = 0;
while chunks.len() != 0 {
while !chunks.is_empty() {
let mut candidate = 0;
let mut candidate_dist = move_distance(cur_pos, chunks[candidate][0]);
for (i, chunk) in chunks.iter().enumerate().skip(1) {
if move_distance(cur_pos, chunk[0]) > candidate_dist {
let dist = move_distance(cur_pos, chunk[0]);
if dist > candidate_dist {
candidate = i;
candidate_dist = move_distance(cur_pos, chunk[0])
candidate_dist = dist;
}
}
cur_pos = chunks[candidate][0];
final_chunks.push(chunks.swap_remove(candidate))
final_chunks.push(chunks.remove(candidate))
}
let final_instructions = final_chunks
.into_iter()
.flatten()
.map(|c| SEQUENCE[*c as usize])
.collect_vec();
let final_instructions = final_chunks.into_iter().flatten().copied().collect_vec();
part1(&String::from_utf8_lossy(&final_instructions))
part1(&final_instructions)
}
#[cfg(test)]
@@ -76,7 +75,7 @@ mod tests {
#[case("vbgyor", 30)]
#[case("vvvvvv", 35)]
fn part1_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part1(input), expected);
assert_eq!(part1(&parse(input)), expected);
}
#[rstest]
@@ -84,6 +83,6 @@ mod tests {
#[case("groovyyoyo", 55)]
#[case("roygbvroygbvroygbv", 103)]
fn part2_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part2(input), expected);
assert_eq!(part2(&parse(input)), expected);
}
}