d3
This commit is contained in:
		
							parent
							
								
									785a00400f
								
							
						
					
					
						commit
						81f11c52ef
					
				
					 2 changed files with 63 additions and 0 deletions
				
			
		
							
								
								
									
										38
									
								
								d3/d3.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								d3/d3.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,38 @@ | ||||||
|  | package d3 | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func D3(input string) int64 { | ||||||
|  | 	binaries := strings.Split(strings.TrimSpace(input), "\n") | ||||||
|  | 
 | ||||||
|  | 	ones := make([]int, len(binaries[0])) | ||||||
|  | 
 | ||||||
|  | 	for _, binary := range binaries { | ||||||
|  | 		for position, bit := range binary { | ||||||
|  | 			if byte(bit) == '1' { | ||||||
|  | 				ones[position]++ | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	gamma := make([]string, len(ones)) | ||||||
|  | 	epsilon := make([]string, len(ones)) | ||||||
|  | 
 | ||||||
|  | 	for pos, oneCount := range ones { | ||||||
|  | 		if oneCount > len(binaries)/2 { | ||||||
|  | 			gamma[pos] = "1" | ||||||
|  | 			epsilon[pos] = "0" | ||||||
|  | 		} else { | ||||||
|  | 			gamma[pos] = "0" | ||||||
|  | 			epsilon[pos] = "1" | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	gammaNum, _ := strconv.ParseInt(strings.Join(gamma, ""), 2, 64) | ||||||
|  | 	epsilonNum, _ := strconv.ParseInt(strings.Join(epsilon, ""), 2, 64) | ||||||
|  | 
 | ||||||
|  | 	return gammaNum * epsilonNum | ||||||
|  | } | ||||||
							
								
								
									
										25
									
								
								d3/d3_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								d3/d3_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | ||||||
|  | package d3 | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestD3(t *testing.T) { | ||||||
|  | 	input := ` | ||||||
|  | 00100 | ||||||
|  | 11110 | ||||||
|  | 10110 | ||||||
|  | 10111 | ||||||
|  | 10101 | ||||||
|  | 01111 | ||||||
|  | 00111 | ||||||
|  | 11100 | ||||||
|  | 10000 | ||||||
|  | 11001 | ||||||
|  | 00010 | ||||||
|  | 01010` | ||||||
|  | 
 | ||||||
|  | 	assert.EqualValues(t, 198, D3(input)) | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue