added day 3 of aoc-2021
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 3 Dec 2021 15:01:16 +0000 (09:01 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 3 Dec 2021 15:01:16 +0000 (09:01 -0600)
core/io/reader.onyx
tests/aoc-2021/day03 [new file with mode: 0644]
tests/aoc-2021/day03.onyx [new file with mode: 0644]
tests/aoc-2021/input/day03.txt [new file with mode: 0644]

index 3eccde393a06043aa80d92b8c8269f067fa0a6ea..ca14f2f4490c1bdb770dabdab5ab7a923a928292 100644 (file)
@@ -173,7 +173,7 @@ read_i32 :: (use reader: ^Reader) -> i32 {
 
         start += 1;
         curr, err = peek_byte(reader);
-        if err != .None do break;
+        if err != .None || start == end do break;
     }
 
     if is_negative do n = -n;
@@ -199,7 +199,7 @@ read_i64 :: (use reader: ^Reader) -> i64 {
 
         start += 1;
         curr, err = peek_byte(reader);
-        if err != .None do break;
+        if err != .None || start == end do break;
     }
 
     if is_negative do n = -n;
@@ -218,7 +218,7 @@ read_u32 :: (use reader: ^Reader) -> u32 {
 
         start += 1;
         curr, err = peek_byte(reader);
-        if err != .None do break;
+        if err != .None || start == end do break;
     }
 
     return n;
@@ -236,7 +236,7 @@ read_u64 :: (use reader: ^Reader) -> u64 {
 
         start += 1;
         curr, err = peek_byte(reader);
-        if err != .None do break;
+        if err != .None || start == end do break;
     }
 
     return n;
diff --git a/tests/aoc-2021/day03 b/tests/aoc-2021/day03
new file mode 100644 (file)
index 0000000..dec7ee9
--- /dev/null
@@ -0,0 +1,2 @@
+Part 1: 738234
+Part 2: 3969126
diff --git a/tests/aoc-2021/day03.onyx b/tests/aoc-2021/day03.onyx
new file mode 100644 (file)
index 0000000..8779a63
--- /dev/null
@@ -0,0 +1,83 @@
+PART :: 2
+
+#load "core/std"
+
+use package core
+
+read_binary :: (r: ^io.Reader) -> i32 {
+    n := 0;
+
+    io.skip_whitespace(r);
+
+    curr, err := io.peek_byte(r);
+    if err != .None do return n;
+    while curr == #char "0" || curr == #char "1" {
+        n *= 2;
+        n += cast(u32) (curr - #char "0"); 
+
+        r.start += 1;
+        curr, err = io.peek_byte(r);
+        if err != .None || r.start == r.end do break;
+    }
+
+    return n;
+}
+
+main :: (args) => {
+    BITS :: 12
+
+    for io.with_file("./tests/aoc-2021/input/day03.txt") {
+        reader := io.reader_make(it);
+
+        nums := array.make(i32);
+        while !io.reader_empty(^reader) {
+            nums << read_binary(^reader);
+        }
+
+        ones_count: [BITS] i32;   // Every digit is 12-bits
+        for BITS {
+            ones_count[it] = 0;
+            for num: nums {
+                if num & (1 << it) != 0 do ones_count[it] += 1;
+            }
+        }
+
+        num1 := 0;
+        for BITS do num1 |= (1 << it) if ones_count[it] >= (nums.count / 2) else 0;
+
+        num2 := ((1 << BITS) - 1) & (~num1);
+
+        printf("Part 1: {}\n", num1 * num2);
+
+        oxygen_array := array.copy(^nums);
+        co2_array    := array.copy(^nums);
+
+        filter_array :: macro (arr: [..] i32, index: i32, comparison: Code) {
+            A := 0;
+            B := (arr.count + 1) / 2;
+
+            // Count the number of ones
+            for arr {
+                if (it & (1 << index)) != 0 do A += 1;
+            }
+
+            expected := (1 << index) if (#insert comparison) else 0;
+
+            while i := 0; i < arr.count {
+                defer i += 1;
+
+                if (arr[i] & (1 << index)) != expected {
+                    array.fast_delete(^arr, i);
+                    i -= 1;
+                }
+            }
+        }
+
+        for iter.as_iterator(range.{ BITS - 1, 0, -1 }) {
+            filter_array(oxygen_array, it, #(A >= B));
+            filter_array(co2_array,    it, #(A <  B));
+        }
+
+        printf("Part 2: {}\n", oxygen_array[0] * co2_array[0]);
+    }
+}
\ No newline at end of file
diff --git a/tests/aoc-2021/input/day03.txt b/tests/aoc-2021/input/day03.txt
new file mode 100644 (file)
index 0000000..a36715c
--- /dev/null
@@ -0,0 +1,1000 @@
+000001000101
+101011100101
+100011100110
+101001111000
+000001000110
+000110100010
+100010101110
+000011001110
+110001101011
+101100110111
+101011011110
+011110010101
+010011010011
+111101110011
+001111011001
+010100101101
+110110110111
+011111101100
+000000101011
+100010010011
+011010000001
+111000111011
+001111111100
+001111110111
+010100011100
+001010110100
+010110011000
+001010011110
+001110010010
+110100111100
+110010110000
+000110111000
+110011000110
+010011011111
+011000010101
+010101010100
+000010001101
+011001101100
+101100110011
+100011110101
+110010011010
+000011100010
+001100010001
+001100011100
+100010011111
+000001111110
+011111111000
+001100010011
+011011010101
+111000011010
+001110110111
+000001101000
+111011111010
+000100101111
+110100100100
+110000110011
+111100101100
+101011101111
+101101010111
+011111001010
+110010111000
+110111110001
+110011101000
+111101011011
+001001101110
+110110010100
+000001110111
+111111010001
+100101100101
+100100110010
+100100010010
+001100111110
+010110101111
+110000001111
+110001011001
+100001011000
+101101100111
+101100100100
+000111001100
+100101101110
+010010000011
+100010110110
+110010101101
+000011011001
+010011110100
+101110100111
+000100001001
+000100010011
+010111010101
+011010001000
+100011101100
+111010101000
+111101100110
+100110010011
+111100011000
+101111111110
+111001011110
+010101110101
+011110101100
+000100101101
+110100110101
+100100001011
+100110100110
+000011010000
+100011010000
+000010110101
+011010101110
+100001001000
+111011101111
+001011101000
+101110101110
+101101000010
+110100001000
+001010101010
+011011110101
+101110110000
+110011010100
+010010100000
+011001110001
+011110111110
+101100001100
+100110111111
+111101001000
+110001001010
+111000100110
+000000001001
+100100100011
+111111000011
+111001100110
+110001110001
+010001111011
+000010110011
+111011111100
+011010101010
+111001101001
+111101100101
+101100000001
+000110111001
+101101011001
+000100110011
+010010101010
+110000100010
+001101010100
+100000010101
+101011100100
+000000110000
+001111010110
+011011100111
+110010010111
+101101100101
+110111010110
+011001100011
+101100111010
+100011011000
+011001011001
+110011001100
+111111111001
+101100011101
+111110110101
+010110010101
+001110110100
+111101000000
+011001111100
+000101100010
+010100111110
+000101011000
+010011001110
+011111111101
+000000110101
+100101001000
+101110100110
+001001010010
+000001000000
+000101011011
+010001011111
+000111111111
+010100101001
+100111110001
+110110011110
+110111010011
+001010100010
+101100101011
+110010110101
+010001100001
+110110110100
+111011111011
+111100111100
+111101111001
+011000110111
+111010100000
+011101001100
+111000011000
+111100100111
+110110111001
+000100000111
+111101100001
+101010011111
+011000001011
+101111111000
+100110101011
+100111101001
+110101000111
+000111010000
+010011011001
+110101101111
+000001011010
+100001000110
+011011101110
+110100011111
+000101010110
+100001010100
+010000101011
+110111100001
+010111011011
+110101011110
+100111010010
+111000011101
+100001111110
+101110001100
+101011110000
+001110000010
+010001001000
+011011001001
+100100110100
+111111011001
+100011110111
+000001001001
+110010000110
+110001011000
+000000001100
+101111010101
+101110011100
+000000101101
+100110101001
+111010110010
+010111010010
+010101111001
+100010101000
+011001000111
+011011110100
+101010000110
+011010111011
+101100100001
+000101001110
+101110111001
+000010000101
+001011110110
+100000101000
+000010000110
+001011010100
+111101011000
+001010111001
+001111000000
+000100100001
+100101011000
+100000010010
+111101100010
+000011000111
+000110001000
+001110101101
+010011011110
+011101100000
+010100000001
+001110010001
+010000000100
+000100010010
+000101000011
+000111100001
+111010110100
+100111100110
+100100001001
+001111111111
+101010001100
+110011001001
+000011010101
+101001001111
+010010001100
+001010110010
+100011110110
+100001111010
+100100101001
+111010111000
+111011110111
+011110100000
+101110000010
+111111111000
+000110011010
+010010000111
+000111101000
+010100110110
+010001011010
+011001101010
+101010000101
+011110011100
+000000000110
+001001000110
+000001001011
+100011001001
+001001100011
+100101001110
+110110101101
+001001100110
+110000011111
+011110101011
+010111001001
+101011010010
+011101110000
+000000011110
+010000101101
+001000000111
+001010001010
+101111100111
+010111101001
+101010001000
+010110110001
+101110100010
+001001110101
+011000100100
+111011011110
+000101110101
+111010100110
+100110011011
+100010100010
+001001111101
+110110111010
+101101100011
+011000011000
+010011010000
+010000111100
+110110001101
+011000111101
+011101010011
+001000000001
+101010011100
+101111011100
+011010101111
+001110110010
+001111100011
+001011010011
+110000001110
+000000110111
+110011101100
+010000101100
+000101001010
+101010110111
+011100000001
+110011101111
+100011111111
+110100011100
+111000100111
+000000001000
+100011011100
+110010000101
+100110010010
+000100100000
+010101100110
+010100010011
+000110100111
+111001010001
+010001011011
+100011010101
+000100001010
+010000001000
+010011101011
+010100000101
+001111001100
+101000000001
+010010100101
+011001111000
+000101101001
+010101011110
+000001010001
+010110000101
+010000011111
+100110111011
+001010010100
+001100100001
+011111111001
+011100101001
+101001111100
+110100101100
+010010101001
+101000001010
+010010010101
+101000110011
+011100010000
+001001111010
+101011010111
+100100100111
+110001101111
+100000101101
+000111110100
+000010110110
+010001100101
+110000111111
+001011000100
+011010011100
+011110011010
+010100101111
+001001010100
+101111011111
+000100111100
+111110010011
+110011010010
+011100111101
+001000011111
+100111000101
+110001100100
+110000110101
+011001001111
+100110010101
+100011001111
+100010000111
+111000110101
+111000000000
+101101010001
+010001000100
+010000101110
+011111110001
+011010101000
+101101111110
+100101101111
+001111101100
+110011100100
+001011100010
+001101001010
+011110010011
+000100011101
+011000010011
+111110101111
+101101110010
+000011101010
+001010110101
+111111000110
+100000101011
+001000111111
+101011110100
+001001110001
+100111101010
+000111110101
+010110100010
+110011010111
+010110110111
+001100100100
+111000001111
+011000010000
+101010000111
+111101111010
+011101100010
+011000001101
+010000010010
+101010110000
+010000100000
+111011100100
+101101011000
+011000010110
+101111111111
+000000111101
+101100110110
+011000110011
+110111100000
+010101111100
+010101101011
+100011001101
+101100111011
+111000111010
+000110010111
+111111001111
+101010111010
+111011101001
+001111001110
+010010001101
+111110000100
+100000000100
+011101011101
+010000011101
+010001011000
+000110011000
+010001101011
+110110000011
+111110110000
+101001011100
+100011110010
+100000010011
+111011101101
+000101101110
+010000100111
+011000000001
+000111100100
+110111111001
+111010111001
+101111100101
+100110000011
+110010011101
+010110111010
+011110001001
+110000100000
+001000010100
+010101101001
+100100011110
+011000100101
+010010001010
+001010001111
+101011001010
+011110101111
+110011001111
+111001001011
+000000111001
+010010101100
+100101010011
+011111011000
+101010100110
+110101001101
+011000100000
+000101111011
+110011100010
+111011100001
+010001111000
+111110000101
+001100000001
+001101001111
+110001010101
+010011111001
+100100101000
+110011011110
+100011101101
+100000011011
+101101101010
+100111111100
+100001000001
+110101111111
+010000100110
+011010010101
+110011010110
+101001010011
+111010000001
+000100110101
+001100100111
+001011000011
+101110010000
+011010111110
+110101101011
+011010001110
+101100101111
+011111011011
+101110011111
+011000110101
+101100001011
+001010001100
+110111101011
+100010001100
+100111100111
+110000111011
+011001101000
+001100010111
+001101001001
+001111010011
+100111000111
+111011010100
+000011101110
+111101000011
+010000011001
+111101101000
+000111011100
+010100001001
+110011111110
+001011010010
+111000010100
+100001010000
+101000000000
+011110000000
+111111110001
+101010100011
+001011111010
+110011101110
+011110001011
+101011100000
+010101011100
+010001101101
+010010110110
+101011010100
+010001001100
+111001111010
+101111111101
+101000101011
+010010000100
+000011110001
+110010001000
+010111101011
+111010010101
+010110100111
+110000100110
+000011111110
+001000101011
+000000000010
+011100100010
+011110010000
+000010011111
+001000011000
+001111000110
+110110011101
+010100110010
+101101100110
+001101100100
+111000001000
+001011100100
+110110010001
+000100101010
+100100111111
+000100111011
+100001000011
+101011001001
+000110101111
+010110111111
+101100010011
+111010110101
+001010001110
+111000100011
+011000110001
+010000110000
+101001100110
+011001010001
+001011001111
+101111111010
+110111111100
+010000010001
+000100101011
+000000011100
+110010000011
+111000010000
+110101111011
+001010100100
+001001000111
+110110001111
+011111001111
+010010111000
+011010011000
+111100111101
+100100101011
+000110001101
+110011001101
+000000100010
+001000110100
+001010110001
+110001000110
+111011000101
+100001010011
+100111011101
+010101110000
+110010000010
+010001111010
+111010111111
+010110001101
+110001110101
+100110101100
+000001011011
+111111011111
+011001011011
+000101110011
+000011110000
+100100010110
+100000010001
+011100110110
+101011101011
+010100101110
+010011011101
+001001111000
+000000011101
+010101100000
+001000100011
+011011111001
+110011100000
+100000110111
+010000000011
+001010011010
+011100010110
+101010100001
+001110111000
+110101000110
+001110011010
+010100001011
+001001010110
+011010000110
+010010101000
+001110100011
+110101011001
+101111101001
+001101110111
+110101111001
+011010110110
+101001000101
+000000100111
+101011111110
+111111100001
+101000111111
+000110100011
+010001110110
+000111101010
+000011011101
+100001101010
+001011101111
+111100010101
+110110111000
+110110011111
+010110001011
+011010000101
+110101110011
+101011101100
+011101111100
+000111011010
+100001011101
+000010001111
+000110111100
+000101000101
+101010101010
+011111001001
+010111110011
+111010000000
+001100011101
+001000101000
+100101010100
+010011111110
+010011000001
+010110100100
+010011001010
+010010100010
+111100001011
+100110100111
+000010110010
+101111000100
+101000000011
+110101011000
+001000010111
+100100110111
+000000000011
+001011111000
+100101000101
+101100010101
+101100000101
+000101001111
+101111010001
+111101011110
+000000010001
+100001110100
+101111000000
+101000101110
+010100001110
+010000110011
+101101110110
+001011000101
+000100100010
+111100010000
+100000100010
+011101000101
+111000110111
+110110001011
+101101111001
+011001111010
+001111011010
+111010101100
+101110011101
+100110101101
+101011011011
+110100000001
+011001100100
+000001011101
+000011011110
+100101101101
+001110100110
+110011110111
+010001110000
+100011100011
+001110011001
+011011110110
+100000010100
+001010101101
+010110000111
+010001011001
+001111111000
+011000011001
+110101101100
+110101000011
+100000011101
+001100001011
+000011000000
+111000010101
+110000011010
+101111000111
+011101010000
+011111000111
+101010111000
+011000110110
+011101101001
+111110101010
+001010111000
+000010011100
+000100111010
+000111100010
+010101111110
+001000011101
+110110111011
+011001101110
+011111111100
+001100100011
+000100001101
+011100001111
+000001010011
+011001111110
+100001100010
+010001110100
+011010110001
+001010101000
+101100001001
+101010000001
+000001101100
+100000000111
+010100010111
+110001111001
+000010100111
+011100101000
+011010110100
+100111100010
+100101110011
+001111010001
+100111101011
+101111110100
+110010100111
+000011110110
+101100100011
+111011101110
+000011010110
+011010000011
+011001011010
+001010011101
+000110000101
+011110101110
+010110001001
+101110100101
+100111001110
+011011111101
+110010110100
+101010010010
+011110110000
+001100110110
+111001000101
+110000001100
+011010011011
+011111010110
+101010011110
+101100010001
+001110101001
+101101010110
+111110110110
+000000101000
+001111000001
+010010001111
+100011100010
+011111110010
+000010010001
+011100000000
+001001111001
+100101101010
+101000101101
+111010011100
+101001101000
+101100110100
+100111000011
+111011100011
+101100010110
+010010101110
+011010010011
+111001011100
+101100010010
+101000110111
+101011010001
+111000110100
+110000101001
+000010101010
+001000110000
+010110010011
+110110100111
+111110000000
+111001100101
+101101110011
+100001111000
+100010011001
+111101000101
+000101010101
+010110111011
+111111111111
+001100000000
+111110001111
+110001001011
+010110110000
+110110101111
+110001101101
+011111101001
+011101100101
+111101001100
+110001101000
+110001011100
+101100011100
+101110110101
+000110110011
+111110111110
+011111010000
+000111010100
+101110001000
+101111100110
+000101001011
+111101111011
+001100100110
+011110100010
+010111011001
+110110110011
+000100100101
+001101011111
+011010001010
+010000111110
+100010111011
+101000100101
+111111101111
+111111111110
+110111100111
+001011110001
+001101000010
+010111010011
+101101101110
+100110001011
+111100001111
+101010011010
+000110111111
+011110000100
+111100110011
+111110111101
+011001101111
+110110011001
+111010001101
+010000001101
+100001111101
+100101111111
+100111110111
+110001000001
+110111001101
+011001110100
+010001110101
+100010111110
+010010010010
+000010111101
+101001101100
+100011111100
+110010111010
+100101011011
+110110101011
+101101010000
+011110100011
+101100111111
+110001100001
+110100111010
+011000100011
+011000100010
+011111101111
+101000010110
+000101000010
+001000100001
+111101110010
+000111000001
+101000111000
+100110100011
+001001001111
+110101011010
+101011101101
+100011000110
+100100111000
+001111001111
+010100010100
+000100100100
+010100101011
+010001100000
+011010110111
+111110111000
+110100101001
+010100011000
+110110101110
+010000101001
+000111001010
+001001100111
+110000110110
+011000101010
+000011110100
+111110010000
+010000101111
+100111010101
+110110110000
+110100101110
+111000110010
+011110001100
+000001100100
+111110000111
+001000011001
\ No newline at end of file