4 Commits 6b8033214f ... 57d9d27ea6

Author SHA1 Message Date
  Piotr Czajkowski 57d9d27ea6 Added description 1 year ago
  Piotr Czajkowski c3ad1bdc65 Solved part2 1 year ago
  Piotr Czajkowski 562bb54d26 Preparing for part2 1 year ago
  Piotr Czajkowski b0272680b6 Solved part1 1 year ago
2 changed files with 162 additions and 28 deletions
  1. 55 28
      20/code.go
  2. 107 0
      20/description.txt

+ 55 - 28
20/code.go

@@ -7,9 +7,15 @@ import (
 	"os"
 )
 
-func readInput(file *os.File) []int {
+type entry struct {
+	value int
+	id    int
+}
+
+func readInput(file *os.File) []entry {
 	scanner := bufio.NewScanner(file)
-	var numbers []int
+	var numbers []entry
+	count := 0
 
 	for scanner.Scan() {
 		line := scanner.Text()
@@ -17,21 +23,28 @@ func readInput(file *os.File) []int {
 			continue
 		}
 
-		var current int
-		n, err := fmt.Sscanf(line, "%d", &current)
+		var current entry
+		n, err := fmt.Sscanf(line, "%d", &current.value)
 		if n != 1 || err != nil {
 			log.Fatal("Can't parse:", line, err)
 		}
 
+		if current.value == 0 {
+			current.id = -1
+		} else {
+			current.id = count
+		}
+
 		numbers = append(numbers, current)
+		count++
 	}
 
 	return numbers
 }
 
-func indexOf(numbers []int, number int) int {
+func indexOf(numbers []entry, id int) int {
 	for i := range numbers {
-		if numbers[i] == number {
+		if numbers[i].id == id {
 			return i
 		}
 	}
@@ -39,68 +52,78 @@ func indexOf(numbers []int, number int) int {
 	return -1
 }
 
-func establishNewIndex(size int, current int, value int) int {
+func establishNewIndex(edge int, current int, value int) int {
 	delta := current + value
 	if delta <= 0 {
 		delta = 0 - delta
-		rest := delta % size
+		rest := delta % edge
 
-		return size - rest
+		return edge - rest
 	}
 
-	return delta % size
+	return delta % edge
 }
 
-func removeAt(numbers []int, index int) []int {
+func removeAt(numbers []entry, index int) []entry {
 	return append(numbers[:index], numbers[index+1:]...)
 }
 
-func addAt(numbers []int, value int, index int) []int {
+func addAt(numbers []entry, value entry, index int) []entry {
 	if index >= len(numbers) {
 		return append(numbers, value)
 	}
 
-	var temp []int
+	var temp []entry
 	temp = append(temp, numbers[:index]...)
 	temp = append(temp, value)
 
 	return append(temp, numbers[index:]...)
 }
 
-func mix(numbers []int) []int {
+func mix(numbers []entry, times int) []entry {
 	size := len(numbers)
 	edge := size - 1
-	mixed := make([]int, size)
+	mixed := make([]entry, size)
 	copy(mixed, numbers)
 
-	for i := range numbers {
-		if numbers[i] == 0 {
-			continue
-		}
+	for t := 0; t < times; t++ {
+		for i := range numbers {
+			if numbers[i].value == 0 {
+				continue
+			}
 
-		currentIndex := indexOf(mixed, numbers[i])
-		newIndex := establishNewIndex(edge, currentIndex, numbers[i])
+			currentIndex := indexOf(mixed, numbers[i].id)
+			newIndex := establishNewIndex(edge, currentIndex, numbers[i].value)
 
-		mixed = removeAt(mixed, currentIndex)
-		mixed = addAt(mixed, numbers[i], newIndex)
+			mixed = removeAt(mixed, currentIndex)
+			mixed = addAt(mixed, numbers[i], newIndex)
+		}
 	}
 
 	return mixed
 }
 
-func part1(mixed []int) int {
-	zeroIndex := indexOf(mixed, 0)
+func calculate(mixed []entry) int {
+	zeroIndex := indexOf(mixed, -1)
 	result := 0
 	size := len(mixed)
 
 	for i := 1; i < 4; i++ {
 		index := establishNewIndex(size, zeroIndex, i*1000)
-		result += mixed[index]
+		result += mixed[index].value
 	}
 
 	return result
 }
 
+func multiply(numbers []entry) []entry {
+	for i := range numbers {
+		numbers[i].value = numbers[i].value * 811589153
+	}
+
+	return numbers
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -114,6 +137,10 @@ func main() {
 	}
 
 	numbers := readInput(file)
-	mixed := mix(numbers)
-	fmt.Println("Part1:", part1(mixed))
+	mixed1 := mix(numbers, 1)
+	fmt.Println("Part1:", calculate(mixed1))
+
+	multiplied := multiply(numbers)
+	mixed10 := mix(multiplied, 10)
+	fmt.Println("Part2:", calculate(mixed10))
 }

+ 107 - 0
20/description.txt

@@ -0,0 +1,107 @@
+--- Day 20: Grove Positioning System ---
+
+It's finally time to meet back up with the Elves. When you try to contact them, however, you get no reply. Perhaps you're out of range?
+
+You know they're headed to the grove where the star fruit grows, so if you can figure out where that is, you should be able to meet back up with them.
+
+Fortunately, your handheld device has a file (your puzzle input) that contains the grove's coordinates! Unfortunately, the file is encrypted - just in case the device were to fall into the wrong hands.
+
+Maybe you can decrypt it?
+
+When you were still back at the camp, you overheard some Elves talking about coordinate file encryption. The main operation involved in decrypting the file is called mixing.
+
+The encrypted file is a list of numbers. To mix the file, move each number forward or backward in the file a number of positions equal to the value of the number being moved. The list is circular, so moving a number off one end of the list wraps back around to the other end as if the ends were connected.
+
+For example, to move the 1 in a sequence like 4, 5, 6, 1, 7, 8, 9, the 1 moves one position forward: 4, 5, 6, 7, 1, 8, 9. To move the -2 in a sequence like 4, -2, 5, 6, 7, 8, 9, the -2 moves two positions backward, wrapping around: 4, 5, 6, 7, 8, -2, 9.
+
+The numbers should be moved in the order they originally appear in the encrypted file. Numbers moving around during the mixing process do not change the order in which the numbers are moved.
+
+Consider this encrypted file:
+
+1
+2
+-3
+3
+-2
+0
+4
+
+Mixing this file proceeds as follows:
+
+Initial arrangement:
+1, 2, -3, 3, -2, 0, 4
+
+1 moves between 2 and -3:
+2, 1, -3, 3, -2, 0, 4
+
+2 moves between -3 and 3:
+1, -3, 2, 3, -2, 0, 4
+
+-3 moves between -2 and 0:
+1, 2, 3, -2, -3, 0, 4
+
+3 moves between 0 and 4:
+1, 2, -2, -3, 0, 3, 4
+
+-2 moves between 4 and 1:
+1, 2, -3, 0, 3, 4, -2
+
+0 does not move:
+1, 2, -3, 0, 3, 4, -2
+
+4 moves between -3 and 0:
+1, 2, -3, 4, 0, 3, -2
+
+Then, the grove coordinates can be found by looking at the 1000th, 2000th, and 3000th numbers after the value 0, wrapping around the list as necessary. In the above example, the 1000th number after 0 is 4, the 2000th is -3, and the 3000th is 2; adding these together produces 3.
+
+Mix your encrypted file exactly once. What is the sum of the three numbers that form the grove coordinates?
+
+Your puzzle answer was 4914.
+--- Part Two ---
+
+The grove coordinate values seem nonsensical. While you ponder the mysteries of Elf encryption, you suddenly remember the rest of the decryption routine you overheard back at camp.
+
+First, you need to apply the decryption key, 811589153. Multiply each number by the decryption key before you begin; this will produce the actual list of numbers to mix.
+
+Second, you need to mix the list of numbers ten times. The order in which the numbers are mixed does not change during mixing; the numbers are still moved in the order they appeared in the original, pre-mixed list. (So, if -3 appears fourth in the original list of numbers to mix, -3 will be the fourth number to move during each round of mixing.)
+
+Using the same example as above:
+
+Initial arrangement:
+811589153, 1623178306, -2434767459, 2434767459, -1623178306, 0, 3246356612
+
+After 1 round of mixing:
+0, -2434767459, 3246356612, -1623178306, 2434767459, 1623178306, 811589153
+
+After 2 rounds of mixing:
+0, 2434767459, 1623178306, 3246356612, -2434767459, -1623178306, 811589153
+
+After 3 rounds of mixing:
+0, 811589153, 2434767459, 3246356612, 1623178306, -1623178306, -2434767459
+
+After 4 rounds of mixing:
+0, 1623178306, -2434767459, 811589153, 2434767459, 3246356612, -1623178306
+
+After 5 rounds of mixing:
+0, 811589153, -1623178306, 1623178306, -2434767459, 3246356612, 2434767459
+
+After 6 rounds of mixing:
+0, 811589153, -1623178306, 3246356612, -2434767459, 1623178306, 2434767459
+
+After 7 rounds of mixing:
+0, -2434767459, 2434767459, 1623178306, -1623178306, 811589153, 3246356612
+
+After 8 rounds of mixing:
+0, 1623178306, 3246356612, 811589153, -2434767459, 2434767459, -1623178306
+
+After 9 rounds of mixing:
+0, 811589153, 1623178306, -2434767459, 3246356612, 2434767459, -1623178306
+
+After 10 rounds of mixing:
+0, -2434767459, 1623178306, 3246356612, -1623178306, 2434767459, 811589153
+
+The grove coordinates can still be found in the same way. Here, the 1000th number after 0 is 811589153, the 2000th is 2434767459, and the 3000th is -1623178306; adding these together produces 1623178306.
+
+Apply the decryption key and mix your encrypted file ten times. What is the sum of the three numbers that form the grove coordinates?
+
+Your puzzle answer was 7973051839072.