Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} | ||
|
||
} |