Day 3
This commit is contained in:
		
							parent
							
								
									96e5cf31fa
								
							
						
					
					
						commit
						5c8dbe4878
					
				
					 7 changed files with 1354 additions and 0 deletions
				
			
		
							
								
								
									
										10
									
								
								day3/Makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								day3/Makefile
									
										
									
									
									
										Normal file
									
								
							|  | @ -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) | ||||||
							
								
								
									
										12
									
								
								day3/example.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								day3/example.txt
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | ||||||
|  | 00100 | ||||||
|  | 11110 | ||||||
|  | 10110 | ||||||
|  | 10111 | ||||||
|  | 10101 | ||||||
|  | 01111 | ||||||
|  | 00111 | ||||||
|  | 11100 | ||||||
|  | 10000 | ||||||
|  | 11001 | ||||||
|  | 00010 | ||||||
|  | 01010 | ||||||
							
								
								
									
										1000
									
								
								day3/realinput.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1000
									
								
								day3/realinput.txt
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										56
									
								
								day3/solution-part1.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								day3/solution-part1.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,56 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <string.h> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										43
									
								
								day3/solution-part1.sh
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										43
									
								
								day3/solution-part1.sh
									
										
									
									
									
										Executable file
									
								
							|  | @ -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))" | ||||||
							
								
								
									
										125
									
								
								day3/solution-part2.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								day3/solution-part2.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,125 @@ | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <string.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | 
 | ||||||
|  | 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; | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										108
									
								
								day3/solution-part2.sh
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										108
									
								
								day3/solution-part2.sh
									
										
									
									
									
										Executable file
									
								
							|  | @ -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))" | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue