day2: improvements and clippies
This commit is contained in:
1 file changed
+28
-29
+28
-29
@@ -1,9 +1,14 @@
|
|||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
// #[aoc_generator(day2)]
|
|
||||||
// fn parse(input: &str) -> {
|
#[aoc_generator(day2)]
|
||||||
// todo!()
|
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";
|
const SEQUENCE: &[u8] = b"roygbv";
|
||||||
|
|
||||||
@@ -17,52 +22,46 @@ fn move_distance(start: u8, end: u8) -> i64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day2, part1)]
|
#[aoc(day2, part1)]
|
||||||
fn part1(input: &str) -> u64 {
|
fn part1(input: &[u8]) -> u64 {
|
||||||
let instructions = input.as_bytes();
|
|
||||||
let mut cur_pos = 0i64;
|
let mut cur_pos = 0i64;
|
||||||
|
|
||||||
for i in instructions {
|
for i in input {
|
||||||
let next_pos = SEQUENCE.iter().position(|c| c == i).unwrap() as u8;
|
cur_pos += move_distance((cur_pos % SEQUENCE.len() as i64) as u8, *i);
|
||||||
cur_pos += move_distance((cur_pos % SEQUENCE.len() as i64) as u8, next_pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cur_pos as u64
|
cur_pos as u64
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day2, part2)]
|
#[aoc(day2, part2)]
|
||||||
fn part2(input: &str) -> u64 {
|
fn part2(input: &[u8]) -> u64 {
|
||||||
let mut instructions = input
|
let mut instructions = input.to_owned();
|
||||||
.as_bytes()
|
|
||||||
.iter()
|
|
||||||
.map(|i| SEQUENCE.iter().position(|c| c == i).unwrap() as u8)
|
|
||||||
.collect_vec();
|
|
||||||
|
|
||||||
// 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();
|
instructions.sort_unstable();
|
||||||
let mut chunks = instructions.chunk_by(|a, b| a == b).collect_vec();
|
let mut chunks = instructions.chunk_by(|a, b| a == b).collect_vec();
|
||||||
let mut final_chunks = Vec::new();
|
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;
|
let mut cur_pos = 0;
|
||||||
while chunks.len() != 0 {
|
while !chunks.is_empty() {
|
||||||
let mut candidate = 0;
|
let mut candidate = 0;
|
||||||
let mut candidate_dist = move_distance(cur_pos, chunks[candidate][0]);
|
let mut candidate_dist = move_distance(cur_pos, chunks[candidate][0]);
|
||||||
for (i, chunk) in chunks.iter().enumerate().skip(1) {
|
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 = i;
|
||||||
candidate_dist = move_distance(cur_pos, chunk[0])
|
candidate_dist = dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur_pos = chunks[candidate][0];
|
cur_pos = chunks[candidate][0];
|
||||||
final_chunks.push(chunks.swap_remove(candidate))
|
final_chunks.push(chunks.remove(candidate))
|
||||||
}
|
}
|
||||||
let final_instructions = final_chunks
|
let final_instructions = final_chunks.into_iter().flatten().copied().collect_vec();
|
||||||
.into_iter()
|
|
||||||
.flatten()
|
|
||||||
.map(|c| SEQUENCE[*c as usize])
|
|
||||||
.collect_vec();
|
|
||||||
|
|
||||||
part1(&String::from_utf8_lossy(&final_instructions))
|
part1(&final_instructions)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -76,7 +75,7 @@ mod tests {
|
|||||||
#[case("vbgyor", 30)]
|
#[case("vbgyor", 30)]
|
||||||
#[case("vvvvvv", 35)]
|
#[case("vvvvvv", 35)]
|
||||||
fn part1_example(#[case] input: &str, #[case] expected: u64) {
|
fn part1_example(#[case] input: &str, #[case] expected: u64) {
|
||||||
assert_eq!(part1(input), expected);
|
assert_eq!(part1(&parse(input)), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@@ -84,6 +83,6 @@ mod tests {
|
|||||||
#[case("groovyyoyo", 55)]
|
#[case("groovyyoyo", 55)]
|
||||||
#[case("roygbvroygbvroygbv", 103)]
|
#[case("roygbvroygbvroygbv", 103)]
|
||||||
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
fn part2_example(#[case] input: &str, #[case] expected: u64) {
|
||||||
assert_eq!(part2(input), expected);
|
assert_eq!(part2(&parse(input)), expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user