diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2022-08-31 01:02:38 +0200 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2022-08-31 01:03:44 +0200 |
| commit | b5e1c27db8c7d330ac13e43927b791b21d95de18 (patch) | |
| tree | 3dd81d8144227a62fc460c5856820ed443f01289 | |
| parent | 2d030693d76bb57f2e01c5bdb4b97e2d8d7eb7af (diff) | |
day10
| -rw-r--r-- | src/bin/day10.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/bin/day10.rs b/src/bin/day10.rs new file mode 100644 index 0000000..562408b --- /dev/null +++ b/src/bin/day10.rs @@ -0,0 +1,44 @@ +fn main() { + let input = [1,3,2,1,1,3,1,1,1,2]; + println!("10a: {}", lookandsay_times(&input, 40).len()); + println!("10b: {}", lookandsay_times(&input, 50).len()); +} + +fn lookandsay(input: &[u8]) -> Vec<u8> { + let mut say = Vec::new(); + + let mut count = 1; + let mut prev = input[0]; + for n in input.iter().skip(1) { + if *n != prev { + say.push(count); + say.push(prev); + prev = *n; + count = 0; + } + count += 1; + } + say.push(count); + say.push(prev); + + say +} + +fn lookandsay_times(input: &[u8], times: u32) -> Vec<u8> { + let mut data = Vec::from(input); + for _ in 0..times { + data = lookandsay(&data); + } + data +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() { + assert_eq!(lookandsay(&[1,1,1,2,2,1]), [3,1,2,2,1,1]); + assert_eq!(lookandsay_times(&[1], 5), [3,1,2,2,1,1]); + } +} |
