Skip to content

Commit

Permalink
day16: Start with The Pattern™.
Browse files Browse the repository at this point in the history
  • Loading branch information
jld committed Dec 23, 2019
1 parent f5aed8c commit 77182fa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions day16/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions day16/Cargo.toml
@@ -0,0 +1,9 @@
[package]
name = "day16"
version = "0.1.0"
authors = ["Jed Davis <jld@xlerb.net>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
69 changes: 69 additions & 0 deletions day16/src/main.rs
@@ -0,0 +1,69 @@
use std::iter::Iterator;

type Num = i32;

#[derive(Debug, Clone)]
struct ThePattern {
scale: usize,
low: usize,
high: usize,
}

impl ThePattern {
fn new(i: usize) -> Self {
Self {
scale: i + 1,
low: 1,
high: 0,
}
}
}

impl Iterator for ThePattern {
type Item = Num;

fn next(&mut self) -> Option<Num> {
debug_assert!(self.low <= self.scale);
if self.low == self.scale {
self.low = 0;
self.high += 1;
}
self.low += 1;
self.high &= 3;
Some(match self.high {
0 => 0,
1 => 1,
2 => 0,
3 => -1,
_ => panic!("unreachable")
})
}
}


fn main() {
println!("Hello, world!");
}


#[cfg(test)]
mod test {
use super::*;

#[test]
fn second_pattern() {
let expected = vec![0, 1, 1, 0, 0, -1, -1, 0, 0, 1, 1, 0, 0, -1, -1];
let actual: Vec<_> = ThePattern::new(1).take(expected.len()).collect();
assert_eq!(actual, expected);
}

#[test]
fn other_patterns() {
let p1: Vec<_> = ThePattern::new(0).take(8).collect();
assert_eq!(p1, vec![1, 0, -1, 0, 1, 0, -1, 0]);

let p3: Vec<_> = ThePattern::new(2).take(8).collect();
assert_eq!(p3, vec![0, 0, 1, 1, 1, 0, 0, 0]);
}

}

0 comments on commit 77182fa

Please sign in to comment.