Piotr Czajkowski пре 4 дана
родитељ
комит
6b77b65aae
1 измењених фајлова са 20 додато и 3 уклоњено
  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)))
 }