diff --git a/main.go b/main.go index 7d4b38b..09d11d2 100644 --- a/main.go +++ b/main.go @@ -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()) } diff --git a/twentyfive/day_twentyfive.go b/twentyfive/day_twentyfive.go new file mode 100644 index 0000000..ee99447 --- /dev/null +++ b/twentyfive/day_twentyfive.go @@ -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)) +} diff --git a/twentyfive/day_twentyfive_test.go b/twentyfive/day_twentyfive_test.go new file mode 100644 index 0000000..d9bb9a3 --- /dev/null +++ b/twentyfive/day_twentyfive_test.go @@ -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() + } +} diff --git a/twentyfive/input.txt b/twentyfive/input.txt new file mode 100644 index 0000000..26f0631 --- /dev/null +++ b/twentyfive/input.txt @@ -0,0 +1,2 @@ +2069194 +16426071 \ No newline at end of file diff --git a/twentyfive/sample.txt b/twentyfive/sample.txt new file mode 100644 index 0000000..55b307c --- /dev/null +++ b/twentyfive/sample.txt @@ -0,0 +1,2 @@ +5764801 +17807724 \ No newline at end of file