advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.go (944B)

    1 package main
    2 
    3 import (
    4 	"bufio"
    5 	"fmt"
    6 	"os"
    7 	"strconv"
    8 )
    9 
   10 func readInput(filename string) (pubCard, pubDoor int64) {
   11 	file, _ := os.Open(filename)
   12 	defer file.Close()
   13 
   14 	scanner := bufio.NewScanner(file)
   15 	pubKeys := []int64{}
   16 	for scanner.Scan() {
   17 		key, _ := strconv.ParseInt(scanner.Text(), 10, 64)
   18 		pubKeys = append(pubKeys, key)
   19 	}
   20 	return pubKeys[0], pubKeys[1]
   21 }
   22 
   23 func main() {
   24 	pubCard, pubDoor := readInput("./input.txt")
   25 	// 17032383
   26 	fmt.Println(part1(pubCard, pubDoor))
   27 }
   28 
   29 func decodeLoopSize(val, subject, modulus, pubKey int64) (loopSize int) {
   30 	for val != pubKey {
   31 		loopSize += 1
   32 		val = (val * subject) % modulus
   33 	}
   34 	return
   35 }
   36 
   37 func part1(pubCard, pubDoor int64) (endKey int64) {
   38 	var (
   39 		val     int64 = 1
   40 		subject int64 = 7
   41 		modulus int64 = 20201227
   42 	)
   43 	cardLoopSize := decodeLoopSize(val, subject, modulus, pubCard)
   44 	subject = pubDoor
   45 	for i := 0; i < cardLoopSize; i++ {
   46 		val = (val * subject) % modulus
   47 	}
   48 	return val
   49 }