]> git.nihav.org Git - nihav.git/blobdiff - src/io/intcode.rs
fix limited unary code invocation parameters
[nihav.git] / src / io / intcode.rs
index 5e35a15a9f8416dec264c2bb2edc942111d1e4f4..a898a10444415dd419f90a940996c64cabbb29ac 100644 (file)
@@ -33,7 +33,7 @@ fn read_unary(br: &mut BitReader, terminator: u32) -> BitReaderResult<u32> {
     }
 }
 
-fn read_unary_lim(br: &mut BitReader, terminator: u32, len: u32) -> BitReaderResult<u32> {
+fn read_unary_lim(br: &mut BitReader, len: u32, terminator: u32) -> BitReaderResult<u32> {
     let mut res: u32 = 0;
     loop {
         if br.read(1)? == terminator { return Ok(res); }
@@ -71,11 +71,11 @@ fn read_rice(br: &mut BitReader, k: u8) -> BitReaderResult<u32> {
 }
 
 fn read_gamma(br: &mut BitReader) -> BitReaderResult<u32> {
-    let mut ret = 0;
+    let mut ret = 1;
     while br.read(1)? != 1 {
         ret = (ret << 1) | br.read(1)?;
     }
-    Ok(ret)
+    Ok(ret - 1)
 }
 
 fn read_gammap(br: &mut BitReader) -> BitReaderResult<u32> {
@@ -101,7 +101,7 @@ impl<'a> IntCodeReader for BitReader<'a> {
         match t {
             UintCodeType::UnaryOnes               => read_unary(self, 0),
             UintCodeType::UnaryZeroes             => read_unary(self, 1),
-            UintCodeType::LimitedUnary(len, term) => read_unary_lim(self, term, len),
+            UintCodeType::LimitedUnary(len, term) => read_unary_lim(self, len, term),
             UintCodeType::Unary012                => read_unary_lim(self, 2, 0),
             UintCodeType::Unary210                => read_unary210(self),
             UintCodeType::Golomb(m)               => read_golomb(self, m),