Day 25 Part 1

Signed-off-by: James Griffin <james@unsupervised.ca>
This commit is contained in:
2020-12-25 17:35:21 -04:00
parent 75a6120c72
commit cc177955b3
5 changed files with 138 additions and 3 deletions

View File

@@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/thatguygriff/aoc2020/twentyfour"
"github.com/thatguygriff/aoc2020/twentyfive"
)
func main() {
@@ -100,6 +100,9 @@ func main() {
// fmt.Println(twentythree.PartTwo())
// Day 24
fmt.Println(twentyfour.PartOne())
fmt.Println(twentyfour.PartTwo())
// fmt.Println(twentyfour.PartOne())
// fmt.Println(twentyfour.PartTwo())
// Day 25
fmt.Println(twentyfive.PartOne())
}

View File

@@ -0,0 +1,65 @@
package twentyfive
import "fmt"
type key struct {
public int
sub int
loopsize int
}
func (k *key) generatePublic() {
v := 1
for i := 0; i < k.loopsize; i++ {
v *= k.sub
v %= 20201227
}
k.public = v
}
func (k *key) encryption(public int) int {
v := 1
for i := 0; i < k.loopsize; i++ {
v *= public
v %= 20201227
}
return v
}
func (k *key) determineLoopSize() {
if k.public == 0 {
return
}
v := 1
loopsize := 0
for v != k.public {
v *= k.sub
v %= 20201227
loopsize++
}
k.loopsize = loopsize
}
// PartOne What encryption key is the handshake trying to establish?
func PartOne() string {
card := key{
public: 2069194,
sub: 7,
}
door := key{
public: 16426071,
sub: 7,
}
card.determineLoopSize()
door.determineLoopSize()
return fmt.Sprintf("The handshake is trying to establish the key %d", door.encryption(card.public))
}

View File

@@ -0,0 +1,63 @@
package twentyfive
import "testing"
func Test_generate_encryption(t *testing.T) {
card := key{
public: 5764801,
sub: 7,
}
door := key{
public: 17807724,
sub: 7,
}
card.determineLoopSize()
door.determineLoopSize()
if card.loopsize != 8 {
t.Logf("expected loop size of 8, got %d", card.loopsize)
t.Fail()
}
if door.loopsize != 11 {
t.Logf("expected loop size of 11, got %d", door.loopsize)
t.Fail()
}
encryption1 := card.encryption(door.public)
encryption2 := door.encryption(card.public)
if encryption1 != 14897079 {
t.Logf("Expected encryption key of 14897079, got %d", encryption1)
t.FailNow()
}
if encryption1 != encryption2 {
t.Logf("Expected to generate the same encryption key, got %d and %d", encryption1, encryption2)
t.Fail()
}
}
func Test_part_one(t *testing.T) {
card := key{
public: 2069194,
sub: 7,
}
door := key{
public: 16426071,
sub: 7,
}
card.determineLoopSize()
door.determineLoopSize()
encryption1 := card.encryption(door.public)
if encryption1 != 11576351 {
t.Logf("Expected encryption key 11576351, got %d", encryption1)
t.FailNow()
}
}

2
twentyfive/input.txt Normal file
View File

@@ -0,0 +1,2 @@
2069194
16426071

2
twentyfive/sample.txt Normal file
View File

@@ -0,0 +1,2 @@
5764801
17807724