Browse Source

Have it for sample

Piotr Czajkowski 5 days ago
parent
commit
6b77b65aae
1 changed files with 20 additions and 3 deletions
  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)))
 }