Skip to content

Commit 9a3b00f

Browse files
author
ynn
committed
fix!: fixes #2
1 parent e096ac1 commit 9a3b00f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ pub fn run(args: &Args) -> Result<(), Box<dyn Error>> {
3737
let mut ip = Interpreter::new();
3838

3939
loop {
40-
debug_print(args.verbose, &format!("{:?}", ip.cur));
4140
let cur_codel = img.get_codel_at(ip.cur);
4241
assert!(!cur_codel.is_black());
4342
if !cur_codel.is_white() {
43+
debug_print(args.verbose, &format!("{:?}", ip.cur));
44+
4445
let iter_max = 8; //changes `dp` and `cc` at most 7 times
4546
for i in 0..iter_max {
4647
//[spec]
@@ -94,12 +95,14 @@ pub fn run(args: &Args) -> Result<(), Box<dyn Error>> {
9495
//See `White Blocks` section in the spec: https://www.dangermouse.net/esoteric/piet.html
9596

9697
let mut visited = FxHashSet::default();
98+
99+
//FIXME: Currently, the average number of iterations needed to find a non-white codel or wall is the size of the current white block.
100+
// Ideally it should be O(1) (like `Block::get_corner_index()`).
97101
loop {
98-
let cur_codel = img.get_codel_at(ip.cur);
99-
if visited.contains(&(cur_codel, ip.dp)) {
102+
if visited.contains(&(ip.cur, ip.dp)) {
100103
return Ok(());
101104
}
102-
visited.insert((cur_codel, ip.dp));
105+
visited.insert((ip.cur, ip.dp));
103106

104107
let next_index = img.get_next_codel_index_in_dp_direction(ip.cur, &ip.dp);
105108
if next_index.is_none() {
@@ -113,8 +116,15 @@ pub fn run(args: &Args) -> Result<(), Box<dyn Error>> {
113116
ip.dp = ip.dp.turn_right();
114117
continue;
115118
}
119+
120+
debug_print(args.verbose, &format!("{:?}", ip.cur));
121+
116122
ip.cur = next_index.unwrap();
117123

124+
if next_codel.is_white() {
125+
continue;
126+
}
127+
118128
//spec: If the transition between colour blocks occurs via a slide across a white block, no command is executed.
119129

120130
break;

tests/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,26 @@ mod integration_tests {
606606
assert!(res.stdout.is_empty());
607607
assert!(res.stderr.contains("the top-left codel shall not be black"));
608608
}
609+
610+
#[test]
611+
fn test38() {
612+
let res = run("./test_images/original___issue_02.png", None);
613+
if !res.success() {
614+
println!("{}", res.stderr);
615+
}
616+
assert!(res.success());
617+
assert!(res.stdout.is_empty());
618+
assert!(res.stderr.is_empty());
619+
}
620+
621+
#[test]
622+
fn test39() {
623+
let res = run("./test_images/original___issue_02_related.png", None);
624+
if !res.success() {
625+
println!("{}", res.stderr);
626+
}
627+
assert!(res.success());
628+
assert_eq!("!", res.stdout);
629+
assert!(res.stderr.is_empty());
630+
}
609631
}

0 commit comments

Comments
 (0)