Explorar o código

Have it for sample

Piotr Czajkowski hai 4 días
pai
achega
6b77b65aae
Modificáronse 1 ficheiros con 20 adicións e 3 borrados
  1. 20 3
      17/code.go

+ 20 - 3
17/code.go

@@ -71,6 +71,15 @@ func getCombo(operand int, registers []Register) int {
 	return -1000000
 }
 
+func powerOfTwo(power int) int {
+	result := 1
+	for i := 0; i < power; i++ {
+		result *= 2
+	}
+
+	return result
+}
+
 func process(registers []Register, program []int) []int {
 	edge := len(program) - 1
 	var instructionPointer int
@@ -79,7 +88,7 @@ func process(registers []Register, program []int) []int {
 	for instructionPointer < edge {
 		switch program[instructionPointer] {
 		case 0:
-			registers[0].value = registers[0].value / (getCombo(program[instructionPointer+1], registers) * getCombo(program[instructionPointer+1], registers))
+			registers[0].value = registers[0].value / (powerOfTwo(getCombo(program[instructionPointer+1], registers)))
 		case 1:
 			registers[1].value ^= program[instructionPointer+1]
 		case 2:
@@ -101,12 +110,20 @@ func process(registers []Register, program []int) []int {
 		}
 
 		instructionPointer += 2
-		fmt.Println(instructionPointer, registers, results)
 	}
 
 	return results
 }
 
+func arrayToString(arr []int) string {
+	strSlice := make([]string, len(arr))
+	for i := range arr {
+		strSlice[i] = fmt.Sprintf("%d", arr[i])
+	}
+
+	return strings.Join(strSlice, ",")
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -119,5 +136,5 @@ func main() {
 	}
 
 	registers, program := readInput(file)
-	fmt.Println(process(registers, program))
+	fmt.Println("Part1:", arrayToString(process(registers, program)))
 }