From 5c8dbe4878c158c351da44b0a48838f6a8996ab1 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Fri, 3 Dec 2021 23:52:44 +0000 Subject: [PATCH] Day 3 --- day3/Makefile | 10 + day3/example.txt | 12 + day3/realinput.txt | 1000 ++++++++++++++++++++++++++++++++++++++++ day3/solution-part1.c | 56 +++ day3/solution-part1.sh | 43 ++ day3/solution-part2.c | 125 +++++ day3/solution-part2.sh | 108 +++++ 7 files changed, 1354 insertions(+) create mode 100644 day3/Makefile create mode 100644 day3/example.txt create mode 100644 day3/realinput.txt create mode 100644 day3/solution-part1.c create mode 100755 day3/solution-part1.sh create mode 100644 day3/solution-part2.c create mode 100755 day3/solution-part2.sh diff --git a/day3/Makefile b/day3/Makefile new file mode 100644 index 0000000..da2e708 --- /dev/null +++ b/day3/Makefile @@ -0,0 +1,10 @@ +solution-part%: solution-part%.c + gcc $< -o $@ + +test-%: solution-% example.txt + $< example.txt + +run-%: solution-% realinput.txt + $< realinput.txt + +.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2) diff --git a/day3/example.txt b/day3/example.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/day3/example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/day3/realinput.txt b/day3/realinput.txt new file mode 100644 index 0000000..6a7de1a --- /dev/null +++ b/day3/realinput.txt @@ -0,0 +1,1000 @@ +101011111001 +100100110111 +101010101011 +010100010000 +001101101101 +110001101010 +001001011001 +101111101111 +110001001000 +111111000000 +111100110001 +101100010100 +000010000011 +000110000011 +101010100101 +000111001010 +100101101110 +111111010101 +110010001100 +000011001101 +001001111110 +100101001100 +000011100100 +000100110011 +000101101000 +101010011011 +101011110000 +010010110011 +110110101110 +001110000110 +001110111101 +110111100101 +000010101010 +101010101010 +011011001101 +110110000111 +011010011000 +000111000101 +100000101011 +010010101100 +110000101110 +000010001001 +101010001100 +101111110101 +101111011110 +110011100001 +011011100100 +111100011010 +110110101001 +101011100010 +111000110110 +111011100101 +111000001111 +101110110011 +111101000000 +101111100110 +011111101101 +100111110111 +000111011011 +001001001111 +100010110000 +001101010000 +100101101001 +100001010000 +110101110001 +100111111111 +101101000101 +001110100110 +101010110000 +101111010010 +101011100101 +001110010100 +011110110100 +100011000000 +001000000100 +010000010001 +101101111011 +111010001011 +101010100000 +101001000000 +001000010101 +010011000111 +101010111100 +111101000101 +111111100010 +100010011101 +101001111101 +010111001111 +111111110110 +100111101010 +010101000101 +100101010010 +010110010001 +011101000111 +011110011010 +100000111100 +011111111000 +001111010001 +110110100111 +111000001011 +110010010011 +101010110010 +110000110111 +101111010000 +110111010000 +110011100011 +010000100111 +001010110001 +011101011010 +110111010010 +011001001101 +101101001111 +011101110100 +001101101110 +011011101101 +111001010001 +001100101000 +010011100000 +011000010100 +111000111111 +001001100001 +010011001010 +001110101100 +010010111010 +000111111011 +010010000110 +110001100100 +001110110111 +000010001111 +100011100001 +100001101110 +100001110111 +010110100000 +100101101111 +001100000111 +101100111001 +100100001100 +100000101100 +010101000110 +101110001000 +101110000110 +101110011100 +001001010001 +110111101011 +101001000001 +111100111110 +001100010100 +010001111111 +110011000011 +100110011101 +011010100100 +011111000011 +000110001000 +010010111111 +010011111011 +001000111110 +000111101011 +100010111100 +010110001111 +011011000000 +010111000011 +010001011111 +111000101100 +010110110000 +001100111011 +011110100101 +100011100011 +001000101000 +101011001011 +101111001111 +111000100101 +111101100111 +111010101000 +001110000111 +010101011110 +001111010100 +101011011101 +000111101010 +000011001011 +111000010001 +011001011011 +011011011001 +011110111000 +000010001100 +011110010000 +010000101001 +101011011111 +100111001100 +101000011100 +111000000100 +011100110000 +011100101010 +010001110011 +100100100010 +011110010010 +011000100111 +110101000101 +111011000001 +011000010111 +010011110011 +011001000001 +000100101110 +100111110001 +010010011010 +111101011001 +100110101111 +100010001100 +010010000111 +100011100100 +111001111011 +110000010111 +101001010001 +011111001010 +110111111100 +000101111101 +101011000111 +000100010000 +111100001111 +100000110010 +110001110100 +000001001100 +000101000001 +100111101011 +011000110110 +101111000000 +010100101100 +101111100011 +100111001101 +100000010010 +100101001111 +011110100010 +110110100110 +100000100001 +110100001110 +010011001111 +011010010000 +000000010101 +111111011100 +000011100000 +011110111101 +110000011100 +011011011000 +010100111010 +010010100101 +001110100011 +000110000100 +100111000101 +000010001000 +110110110000 +010011000100 +011001100010 +110000011110 +000110100111 +010111111011 +011100000011 +100011101110 +001100011010 +000010100100 +000011110000 +000000101000 +101001101001 +101111010101 +101100101101 +110000000000 +111011001110 +001100101110 +100101111000 +010001000111 +111100111010 +000100011101 +010001000011 +100000101001 +111111101101 +000000011101 +001101110110 +001100000101 +010110011110 +001011100100 +110001100101 +001011110011 +011101111000 +001001100110 +000001011000 +001011010000 +110001010100 +000100011011 +010101101010 +101000101010 +101111011000 +110001010111 +111100110011 +101011101000 +000100010110 +111010000110 +000100000001 +101011110101 +010010010011 +100101011001 +100101001010 +110100110101 +010011100111 +100110010101 +111010000011 +011011110110 +111000101110 +001011101100 +010110100010 +011001101011 +011000101101 +011011111110 +011011111001 +100001001101 +101110011011 +001111011100 +111110010011 +000010101011 +001111101010 +100001101000 +111100100000 +010011100100 +011101101100 +000001101011 +001100001110 +000011000111 +100110001000 +101101101011 +001011011110 +101010111101 +101110100110 +011111000100 +111101111000 +001011110111 +010010011001 +111110110001 +100101000100 +011011010101 +100010010000 +100010101000 +110010000100 +000001001111 +101110011010 +010000111111 +111101010001 +100000000000 +011101100110 +001110101101 +001001110010 +110011101001 +101101011001 +000000110101 +011101100011 +010100000011 +101011110010 +100101000010 +100110001100 +000111111010 +111100011001 +111110101001 +010000111000 +100110000011 +100011001110 +000100010010 +001101100100 +000000001100 +011101001001 +100001001000 +101100111000 +110011011111 +111110101110 +101101001010 +111000011001 +111110011001 +110111010100 +011001111011 +101101111000 +010010010100 +100000110110 +011110010100 +010010101010 +001111100100 +100000011101 +110100010010 +101101100000 +111001111010 +010001100110 +011000110101 +011110010011 +011111100000 +001000101100 +000100010111 +011000101010 +011000010110 +010001101011 +110101010000 +000100100100 +011011001100 +010100011101 +100111110110 +111010000001 +001001111111 +010100001111 +011111101011 +011000011011 +111010000100 +110001001111 +111011011010 +001110001110 +100100101001 +101101101001 +001100001001 +001000001110 +101111000010 +000110001110 +101111110010 +110011001011 +000000010100 +110100110001 +111000011111 +100000001110 +100100011011 +101100100011 +011101000010 +001100100101 +111110101101 +011100001001 +101100000110 +101011010011 +000101101100 +001011101001 +110000000101 +011111011110 +000001100110 +100110011011 +111010100100 +011001101001 +010111110011 +110111000000 +011100101011 +010110100111 +110011001001 +000010111011 +101110010110 +000001011001 +100101000111 +110110111011 +000101110001 +100000111101 +011110001011 +000101010101 +101011110011 +000110011001 +101010000001 +100001011110 +010000000111 +000011100110 +100111100010 +110101000111 +001010101110 +111000100000 +001100101101 +000111010111 +101110000000 +011111000010 +111000001110 +001100110101 +101001000100 +010111011101 +000100000100 +111111000111 +111011001111 +101000011110 +110011101101 +111101000011 +101101111001 +010010010101 +110000100010 +000100100111 +000110111111 +000011001000 +011011100001 +101100111101 +011111000001 +000110101100 +010010111001 +111100101110 +100111010100 +011111000000 +001011001000 +101111000011 +000100001010 +110100010000 +110001000011 +010010101111 +001010001010 +010001110001 +011000001101 +101001010111 +000000011100 +010011111000 +110010011011 +001110100111 +011101000011 +111110011101 +100111000000 +101011101110 +111000010111 +101001000011 +011110011001 +000100010001 +101110000100 +111101101101 +000100100110 +010011010001 +000101100110 +010111110001 +001110111100 +100000111111 +101100000011 +110011000100 +100000001011 +010101100000 +101010000101 +010110101101 +010011100001 +001101001111 +101100100111 +100000001101 +100100101100 +101111010111 +010101001100 +011001111111 +010111110000 +010100110101 +101111010001 +111101111100 +011010011011 +111100110100 +111110001010 +011110011111 +000110001100 +011001010111 +000010010111 +011100101111 +111110111000 +110010110010 +000101111010 +001010010100 +100110110001 +101101010001 +100001111011 +010101001001 +000001000010 +100101100011 +010100101010 +001001001100 +101000110110 +111110001000 +100101110010 +001000111011 +100111010111 +000000110000 +110101111000 +011000101110 +110101001111 +101000010010 +101011000010 +100110000111 +110101011001 +110010010100 +010000001111 +110011011001 +111010110100 +010000101010 +101100111011 +000000011010 +110110001000 +001011011011 +101110100001 +010100011011 +111101001101 +010000011001 +110010100100 +101101011010 +110000100001 +010101111000 +101000111010 +011011010100 +011110011101 +010000100100 +001111101001 +010011110000 +111000100111 +010101010101 +000001001010 +010001001100 +111011111100 +001110000011 +110101101000 +111010011110 +110011010011 +001010101101 +001111110001 +110001011110 +011110010001 +010010000100 +111010111001 +000100111111 +000001010101 +001010110111 +000110010011 +000010011111 +011110100001 +011111010110 +001110010110 +001001000011 +101111011101 +011100110111 +010000001101 +010010100111 +010000110011 +011110110111 +010000110110 +011101011011 +000010100001 +001100011111 +011110110000 +111011011011 +101011101111 +010001000101 +010100001000 +010100101110 +001000000101 +001001011011 +111001000101 +110101110100 +111111010111 +101010100111 +110000011010 +000100100000 +011101000001 +000011000001 +010101010001 +010010011100 +010001110101 +010110110111 +010011110010 +011011100110 +000110100011 +100010100011 +101111110011 +001011100101 +011101110000 +110001110000 +111010010000 +111111000011 +010010001100 +010111101010 +010010101000 +111001010111 +010110110011 +001101110010 +011111001111 +010100110100 +010010100100 +101011101100 +000111101001 +000001011010 +101010111000 +100011111111 +100100011111 +111000010010 +011011100010 +000111010010 +101010101111 +010000100101 +100110110101 +011011100011 +011011110011 +111011000100 +100010001010 +110000010000 +100101011111 +101100001100 +111110101111 +100111000100 +011101001110 +100011011101 +111101011010 +110101111001 +001000110101 +111110101100 +001010010111 +100110000000 +110101010100 +110110001101 +011110101101 +100110101110 +000000000010 +011010111001 +000001101111 +000100000101 +110000100011 +001010000101 +100111011010 +000001101001 +001011000001 +000111100110 +111111110011 +000101000000 +111000110000 +000010000111 +000101010001 +000011110001 +101100110100 +100001111110 +001111111111 +100011000110 +011100011001 +101011110100 +001010001111 +100010100001 +001111100110 +010110110010 +110010110101 +011000110011 +000101011110 +101000100100 +010110010110 +001000000110 +110000100100 +011101101011 +100100000111 +000111010101 +110111000100 +001010110000 +101001110001 +001001101011 +101000100101 +111111000010 +000010110101 +110000000110 +000100110000 +111111011011 +101101100101 +011001010010 +111111101111 +000111101101 +100110110011 +110000001000 +111000001101 +110011010010 +110110111110 +000001011111 +001100001010 +111101011100 +010010100010 +110100011011 +110001101011 +010111111010 +011111100100 +001010010110 +001010111111 +111110111110 +011010000110 +111111111001 +011011011110 +001010011101 +001110010111 +001001111100 +110010111000 +000111000100 +001100001000 +110110110100 +101001010000 +000101000101 +010101101100 +101010110110 +111101011000 +101010011100 +110010110011 +111110001110 +100100101110 +001010000000 +001000110010 +101000111111 +111110111101 +000110010110 +101100100110 +100010011001 +100110100010 +111101100110 +111100111111 +010111011011 +111100011111 +001010010011 +111101010010 +110111001010 +010110000000 +011111011001 +101011100011 +011100110011 +111100011110 +101001001001 +100101100110 +000100110010 +010011101110 +000011110111 +000000011111 +110011011100 +111110100100 +101110101111 +001101011000 +101101110001 +101011000100 +111001101100 +110111111001 +011010101001 +000000000000 +111100000101 +110010111001 +000100001110 +110101111100 +110010011111 +001001110111 +111111111111 +000110011100 +110101111010 +011010111011 +111000000101 +011110100111 +001010101111 +001011011001 +101010001011 +100001000000 +111110111010 +001110111110 +000000110100 +111110111001 +111000010101 +110011110111 +110111001101 +111011010010 +110100000110 +100111111100 +010001100010 +111100010000 +100111011101 +101001000111 +111110110011 +010101010100 +101000110111 +000111101100 +100001011001 +010000011111 +000100101011 +011011011010 +001111000010 +111001101011 +100011010111 +110111100111 +100001010110 +100001110101 +100110110111 +011111111011 +111111101110 +111111001000 +101010000110 +110010010110 +100110100111 +011101011100 +011010011101 +101111101101 +110111010110 +110000000100 +110000110100 +001100000110 +001011001101 +001010011111 +011001110000 +100111100100 +001111000000 +110100011001 +110000110011 +000000101101 +111101111111 +111111010010 +011101101110 +111101101100 +100000110000 +110101010001 +101001110100 +010010110110 +111011000111 +101100000100 +010011010101 +111101000001 +101001111000 +101110000011 +001011011101 +111010000010 +001001110100 +111111001001 +001001010000 +001110000001 +111100000000 +010101110100 +000101011011 +001100011001 +001110001100 +001110100010 +000000100111 +000100111000 +110100101100 +110100111010 +111001101101 +111100101011 +011101100000 +100000100011 +010111110010 +010011001000 +101001100110 +100110001111 +000110110101 +111001110110 +000101010110 +011000011010 +010100110110 +110100111111 +111000110011 +001011111000 +001000000000 +111111010100 +111111100110 +000000001111 +110001000001 +010000101000 +101111111010 +100100001101 +111011110100 +110101110011 +010111111110 +001000101111 +100110101100 +110010010010 +010010001000 +011111101100 +111100100011 +110111101000 +100000111001 +001011100011 +001010111100 +000100001100 +010111000110 +001111000101 +001110000010 +110100101000 +001110000101 +101000110000 +001001011101 +101001011001 +011110100011 +101010010110 +101000001101 +110111111111 +010010001101 +011100010110 +000100100001 +001111111101 +001001111000 +001100101111 +111010111100 +110100010111 +000000010011 +100110110000 +101100101111 +100011010100 +001001101110 +110001101000 +110101101110 +010011110101 +010000110001 +100011111000 +111110000010 +100111100000 +100001100110 +110111000110 +000101100011 +010110001100 +100000100010 +001011000000 +110101000010 +101110101001 +011010100110 +111111110000 +001010110010 +010000001011 +100000010011 +010111010111 +101111100010 +010100010011 diff --git a/day3/solution-part1.c b/day3/solution-part1.c new file mode 100644 index 0000000..d58cabe --- /dev/null +++ b/day3/solution-part1.c @@ -0,0 +1,56 @@ +#include +#include + + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + + unsigned int gamma = 0; + unsigned int epsilon = 0; + unsigned int lineLength = 0; + unsigned int noOfLines = 0; + + // Read the values into an array + FILE *fp=fopen(argv[1], "r"); + char ch = fgetc(fp); + while ( ch != '\n' ){ + lineLength++; + ch = fgetc(fp); + } + + rewind(fp); + + unsigned int digitCount[lineLength]; + memset( &digitCount, 0, lineLength*sizeof(unsigned int) ); + + + for ( int i = 0; !feof(fp); i++){ + ch=fgetc(fp); + switch (ch) { + case '\n': + // I will be incremented at the end of the iteration + i=-1; + noOfLines++; + break; + case '1': + digitCount[i]++; + break; + } + } + + for (unsigned int i=0; i < lineLength; i++ ){ + if ( digitCount[i] > ( (double)noOfLines ) / 2 ){ + gamma = gamma | 1 << (lineLength - i - 1); + } else { + epsilon = epsilon | 1 << (lineLength - i - 1); + } + } + + printf("Answer: %d", gamma * epsilon); + + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +} diff --git a/day3/solution-part1.sh b/day3/solution-part1.sh new file mode 100755 index 0000000..92bc246 --- /dev/null +++ b/day3/solution-part1.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +file="$1" + +ones=(0 0 0 0 0 0 0 0 0 0 0 0) +lines=0 +gamma="" +epsilon="" + +while read line; do + + bitn=0 + while read bit; do + if [ "$bit" -eq 1 ]; then + ones[$bitn]=$((${ones[$bitn]} + 1)) + fi + bitn=$(( bitn + 1 )) + done < <(echo "$line" | grep -o .) + + lines="$((lines + 1))" + +done < <(cat "$file") + +echo "${ones[@]}" + +for i in ${ones[@]}; do + if [ "$i" -gt "$(( lines / 2 ))" ]; then + gamma="${gamma}1" + epsilon="${epsilon}0" + else + gamma="${gamma}0" + epsilon="${epsilon}1" + fi +done + + +gammaDecimal="$( echo "ibase=2; $gamma" | bc )" +epsilonDecimal="$( echo "ibase=2; $epsilon" | bc )" + +echo "Gamma: $gamma -> $gammaDecimal" +echo "Epsilon: $epsilon -> $epsilonDecimal" + +echo "Answer: $((gammaDecimal * epsilonDecimal))" diff --git a/day3/solution-part2.c b/day3/solution-part2.c new file mode 100644 index 0000000..a8dc2e6 --- /dev/null +++ b/day3/solution-part2.c @@ -0,0 +1,125 @@ +#include +#include +#include + +int countLines(char filename[]){ + FILE *fp; + fp=fopen(filename, "r"); + long int lines =0; + + if ( fp == NULL ) { + return -1; + } + + while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c"))) + ++lines; + + fclose(fp); // closing file + return lines; +} + + +void countPositionBits(int linesize, int noOfLines, char *lines[noOfLines], unsigned int digitCount[]){ + for ( int i = 0; i < noOfLines; i++){ + for ( int j = 0; j < linesize; j++ ){ + if ( lines[i][j] == '1' ) + digitCount[j]++; + } + } +} + +char* filterO2List(unsigned int linesize, unsigned int noOfLines, char **lines, unsigned int iteration){ + if ( noOfLines == 1 ) + return lines[0]; + + unsigned int digitCount[linesize]; + memset( &digitCount, 0, linesize*sizeof(unsigned int) ); + countPositionBits(linesize, noOfLines, lines, digitCount); + + char bitKeep = ( digitCount[iteration] >= ((double)noOfLines) / 2 ) ? '1' : '0'; + + unsigned int newLinesIndex=0; + //Not sure why +4 here but any less and it buffer overflows + char **newLines = (char**)malloc((digitCount[iteration]+4) * sizeof(char*)); + + for ( unsigned int i=0; i < noOfLines; i++ ){ + if (lines[i][iteration] == bitKeep) + newLines[newLinesIndex++] = lines[i]; + } + + return filterO2List( linesize, newLinesIndex, newLines, ++iteration ); + +} + +char* filterCO2List(unsigned int linesize, unsigned int noOfLines, char **lines, unsigned int iteration){ + if ( noOfLines == 1 ) + return lines[0]; + + unsigned int digitCount[linesize]; + memset( &digitCount, 0, linesize*sizeof(unsigned int) ); + countPositionBits(linesize, noOfLines, lines, digitCount); + + char bitKeep = ( digitCount[iteration] >= ((double)noOfLines) / 2 ) ? '0' : '1'; + + unsigned int newLinesIndex=0; + //Not sure why +4 here but any less and it buffer overflows + char **newLines = (char**)malloc((digitCount[iteration]+4) * sizeof(char*)); + + for ( unsigned int i=0; i < noOfLines; i++ ){ + if (lines[i][iteration] == bitKeep) + newLines[newLinesIndex++] = lines[i]; + } + + return filterCO2List( linesize, newLinesIndex, newLines, ++iteration ); + +} + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + + unsigned int gamma = 0; + unsigned int epsilon = 0; + unsigned int lineLength = 0; + unsigned int noOfLines = countLines( argv[1] ); + unsigned int iteration=0; + + // Read the values into an array + FILE *fp=fopen(argv[1], "r"); + char ch = fgetc(fp); + while ( ch != '\n' ){ + lineLength++; + ch = fgetc(fp); + } + + rewind(fp); + + unsigned int digitCount[lineLength]; + memset( &digitCount, 0, lineLength*sizeof(int) ); + + // Make a big array to hold the lines + char **lines = (char**)malloc(noOfLines * sizeof(char*)); + + // Loop through the file and populate the array + for ( unsigned int i = 0; !feof(fp); i++){ + + char *line = (char*)malloc(lineLength); + fscanf(fp, "%s\n", line); + lines[i] = line; + } + + char *o2Rating = filterO2List( lineLength, noOfLines, lines, 0 ); + char *co2Rating = filterCO2List( lineLength, noOfLines, lines, 0 ); + + int o2RatingDec = (int)strtol(o2Rating, NULL, 2); + int co2RatingDec = (int)strtol(co2Rating, NULL, 2); + + printf("O2 Rating: %s -> %i\n", o2Rating, o2RatingDec); + printf("CO2 Rating: %s -> %i\n", co2Rating, co2RatingDec); + printf("Answer: %i\n", o2RatingDec * co2RatingDec); + + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +} diff --git a/day3/solution-part2.sh b/day3/solution-part2.sh new file mode 100755 index 0000000..2d04975 --- /dev/null +++ b/day3/solution-part2.sh @@ -0,0 +1,108 @@ +#!/usr/bin/env bash + +file="$1" + + +gamma="" +epsilon="" + +countPositionBits(){ + ones=(0 0 0 0 0 0 0 0 0 0 0 0) + #ones=(0 0 0 0 0) + stdin="$(cat -)" + + while read line; do + + bitn=0 + while read bit; do + if [ "$bit" -eq 1 ]; then + ones[$bitn]=$((${ones[$bitn]} + 1)) + fi + bitn=$(( bitn + 1 )) + done < <(echo "$line" | grep -o .) + + + done < <(echo "$stdin") + + printf '%s\n' "${ones[@]}" + +} + +dots(){ + seq $i | sed 's/.*/./' | tr -d '\n' +} + +ogr="$(cat "$1")" +csr="$(cat "$1")" + +echo "OGR" + +i=0 +while [ "$(echo "$ogr" | wc -l)" -gt 1 ]; do + counts="$(echo "$ogr" | countPositionBits)" + noofogr="$(echo "$ogr" | wc -l)" + noofones="$( echo "$counts" | sed -n "$((i + 1))p" )" + echo "$ogr" + echo "No of ones $noofones" + echo "No of ogr $noofogr" + if [ "$((noofones * 10))" -ge "$(( noofogr * 10 / 2 ))" ]; then + echo "Filtering only 1s in position $i" + echo "^$(dots $i)1" + ogr="$(echo "$ogr" | grep "^$(dots $i)1" )" + else + echo "Filtering only 0s in position $i" + ogr="$(echo "$ogr" | grep "^$(dots $i)0" )" + fi + echo -e "\n---\n" + i="$((i + 1))" +done + +echo "CSR" + +i=0 +while [ "$(echo "$csr" | wc -l)" -gt 1 ]; do + counts="$(echo "$csr" | countPositionBits)" + noofcsr="$(echo "$csr" | wc -l)" + noofones="$( echo "$counts" | sed -n "$((i + 1))p" )" + echo "$csr" + echo "No of ones $noofones" + echo "No of csr $noofcsr" + if [ "$((noofones * 10))" -ge "$(( noofcsr * 10 / 2 ))" ]; then + echo "Filtering only 0s in position $i" + echo "^$(dots $i)1" + csr="$(echo "$csr" | grep "^$(dots $i)0" )" + else + echo "Filtering only 1s in position $i" + csr="$(echo "$csr" | grep "^$(dots $i)1" )" + fi + echo -e "\n---\n" + i="$((i + 1))" +done + + +ogrDecimal="$( echo "ibase=2; $ogr" | bc )" +csrDecimal="$( echo "ibase=2; $csr" | bc )" + +echo "Oxxygen Generator Rating: $ogr -> $ogrDecimal" +echo "CO2 Scrubber Rating: $csr -> $csrDecimal" + +echo "Answer: $((ogrDecimal * csrDecimal))" + + +#while read i; do +# if [ "$i" -gt "$(( lines / 2 ))" ]; then +# gamma="${gamma}1" +# epsilon="${epsilon}0" +# else +# gamma="${gamma}0" +# epsilon="${epsilon}1" +# fi +#done < <(echo "") +# +#gammaDecimal="$( echo "ibase=2; $gamma" | bc )" +#epsilonDecimal="$( echo "ibase=2; $epsilon" | bc )" +# +#echo "Gamma: $gamma -> $gammaDecimal" +#echo "Epsilon: $epsilon -> $epsilonDecimal" +# +#echo "Answer: $((gammaDecimal * epsilonDecimal))"