advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 21f409f22902654682d5e3852ddeffa0e700bd38
parent b585df7d57594a820b99444d7f420891e2d75e6c
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Fri, 25 Dec 2020 04:54:03 -0600

Add 2020 day 25

Diffstat:
A2020/day25/input.txt | 2++
A2020/day25/main.go | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/2020/day25/input.txt b/2020/day25/input.txt
@@ -0,0 +1,2 @@
+10441485
+1004920
diff --git a/2020/day25/main.go b/2020/day25/main.go
@@ -0,0 +1,49 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+)
+
+func readInput(filename string) (pubCard, pubDoor int64) {
+	file, _ := os.Open(filename)
+	defer file.Close()
+
+	scanner := bufio.NewScanner(file)
+	pubKeys := []int64{}
+	for scanner.Scan() {
+		key, _ := strconv.ParseInt(scanner.Text(), 10, 64)
+		pubKeys = append(pubKeys, key)
+	}
+	return pubKeys[0], pubKeys[1]
+}
+
+func main() {
+	pubCard, pubDoor := readInput("./input.txt")
+	// 17032383
+	fmt.Println(part1(pubCard, pubDoor))
+}
+
+func decodeLoopSize(val, subject, modulus, pubKey int64) (loopSize int) {
+	for val != pubKey {
+		loopSize += 1
+		val = (val * subject) % modulus
+	}
+	return
+}
+
+func part1(pubCard, pubDoor int64) (endKey int64) {
+	var (
+		val     int64 = 1
+		subject int64 = 7
+		modulus int64 = 20201227
+	)
+	cardLoopSize := decodeLoopSize(val, subject, modulus, pubCard)
+	subject = pubDoor
+	for i := 0; i < cardLoopSize; i++ {
+		val = (val * subject) % modulus
+	}
+	return val
+}