Archived
3rd: optimize embarassing binary to decimal conversion
This commit is contained in:
+7
-83
@@ -13,7 +13,7 @@ func Run() {
|
|||||||
values := readInput()
|
values := readInput()
|
||||||
heatmap := createHeatmap(values)
|
heatmap := createHeatmap(values)
|
||||||
fmt.Printf("power: %d\n", calcPower(calcGamma(heatmap, len(values)), calcEpsilon(heatmap, len(values))))
|
fmt.Printf("power: %d\n", calcPower(calcGamma(heatmap, len(values)), calcEpsilon(heatmap, len(values))))
|
||||||
fmt.Printf("oxygen * co2: %d\n", calcOxygen(values)*calcCO2(values))
|
fmt.Printf("oxygen * co2 (life support): %d\n", calcOxygen(values)*calcCO2(values))
|
||||||
}
|
}
|
||||||
|
|
||||||
func calcOxygen(values [][]int) int {
|
func calcOxygen(values [][]int) int {
|
||||||
@@ -116,89 +116,13 @@ func calcPower(gamma []int, epsilon []int) int {
|
|||||||
return convertToDecimal(gamma) * convertToDecimal(epsilon)
|
return convertToDecimal(gamma) * convertToDecimal(epsilon)
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertToDecimal(gamma []int) int {
|
func convertToDecimal(i []int) int {
|
||||||
gammaDecimalValue := 0
|
s := []string{}
|
||||||
for k, v := range gamma {
|
for _, v := range i {
|
||||||
switch k {
|
s = append(s, strconv.Itoa(v))
|
||||||
case 0:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 2048
|
|
||||||
}
|
}
|
||||||
case 1:
|
r, _ := strconv.ParseInt(strings.Join(s, ""), 2, 64)
|
||||||
if v == 1 {
|
return int(r)
|
||||||
gammaDecimalValue += 1024
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 512
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 256
|
|
||||||
}
|
|
||||||
case 4:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 128
|
|
||||||
}
|
|
||||||
case 5:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 64
|
|
||||||
}
|
|
||||||
case 6:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 32
|
|
||||||
}
|
|
||||||
case 7:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 16
|
|
||||||
}
|
|
||||||
case 8:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 8
|
|
||||||
}
|
|
||||||
case 9:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 4
|
|
||||||
}
|
|
||||||
case 10:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 2
|
|
||||||
}
|
|
||||||
case 11:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return gammaDecimalValue
|
|
||||||
}
|
|
||||||
func convertToDecimal5(gamma []int) int {
|
|
||||||
gammaDecimalValue := 0
|
|
||||||
for k, v := range gamma {
|
|
||||||
switch k {
|
|
||||||
case 0:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 16
|
|
||||||
}
|
|
||||||
case 1:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 8
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 4
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue += 2
|
|
||||||
}
|
|
||||||
case 4:
|
|
||||||
if v == 1 {
|
|
||||||
gammaDecimalValue++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return gammaDecimalValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func calcGamma(heatmap []int, lines int) []int {
|
func calcGamma(heatmap []int, lines int) []int {
|
||||||
|
|||||||
Reference in New Issue
Block a user