mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2023 day 15 part 1
This commit is contained in:
@@ -1,7 +1,46 @@
|
|||||||
pub fn part1(_input: &[u8]) -> anyhow::Result<String> {
|
fn trim(input: &[u8]) -> &[u8] {
|
||||||
anyhow::bail!("Not implemented")
|
let whitespace = input
|
||||||
|
.iter()
|
||||||
|
.rev()
|
||||||
|
.take_while(|c| c.is_ascii_whitespace())
|
||||||
|
.count();
|
||||||
|
|
||||||
|
&input[..(input.len() - whitespace)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash(input: &[u8]) -> u32 {
|
||||||
|
input
|
||||||
|
.iter()
|
||||||
|
.fold(0, |cur, &c| ((cur + u32::from(c)) * 17) % 256)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &[u8]) -> anyhow::Result<String> {
|
||||||
|
let input = trim(input);
|
||||||
|
|
||||||
|
Ok(input
|
||||||
|
.split(|&c| c == b',')
|
||||||
|
.map(hash)
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
pub fn part2(_input: &[u8]) -> anyhow::Result<String> {
|
||||||
anyhow::bail!("Not implemented")
|
anyhow::bail!("Not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SAMPLE: &[u8] = include_bytes!("samples/15.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_hash() {
|
||||||
|
assert_eq!(hash(b"HASH"), 52);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample_part1() {
|
||||||
|
assert_eq!("1320", part1(SAMPLE).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
1
2023/src/samples/15.txt
Normal file
1
2023/src/samples/15.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
||||||
Reference in New Issue
Block a user