-
Notifications
You must be signed in to change notification settings - Fork 0
Solve Day 25 #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manuphatak
wants to merge
8
commits into
main
Choose a base branch
from
day_25
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solve Day 25 #45
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a8b4fb0
Setup Day 25
manuphatak bb51367
Solve part 1
manuphatak 8e462b6
Update specs
manuphatak 19913eb
Update readme to include part 2
manuphatak 9058d17
Merge remote-tracking branch 'origin/main' into day_25
manuphatak 968853c
Update readme to include links to solution
manuphatak 24ee23e
Use explicit imports
manuphatak 02f2905
Remove part 2
manuphatak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| ## NOTES | ||
|
|
||
| `transform subjectNumber loopSize` appears to be `subjectNumber ^ loopSize mod 20201227` | ||
|
|
||
| https://www.wolframalpha.com/input/?i=%287+%5E+x%29+mod+20201227 | ||
|
|
||
| ```hs | ||
| (7 ^ x) mod 20201227 | ||
| x = floor (log 20201227 / log 7) | ||
| ``` | ||
|
|
||
| ### HandShake steps | ||
|
|
||
| ```hs | ||
|
|
||
| -- cardsPublicKey' = transform 7 <cardsSecretLoopSize> | ||
| -- doorsPublicKey' = transform 7 <doorsSecretLoopSize> | ||
|
|
||
| -- <cardsSecretLoopSize> = crack 7 cardsPublicKey | ||
| -- <doorsSecretLoopSize> = crack 7 doorsPublicKey | ||
|
|
||
| -- encryptionKey = transform doorsPublicKey <cardsSecretLoopSize> | ||
| -- encryptionKey = transform cardsPublicKey <doorsSecretLoopSize> | ||
|
|
||
| -- encryptionKey = transform doorsPublicKey (crack 7 cardsPublicKey) | ||
| -- encryptionKey = transform cardsPublicKey (crack 7 doorsPublicKey) | ||
| ``` | ||
|
|
||
| ```hs | ||
|
|
||
| cardsPublicKey :: Int | ||
| cardsPublicKey = 5764801 | ||
|
|
||
| doorsPublicKey :: Int | ||
| doorsPublicKey = 17807724 | ||
|
|
||
| >>> transform 7 8 | ||
| 5764801 | ||
|
|
||
| >>> crack 7 cardsPublicKey | ||
| 8 | ||
|
|
||
| >>> transform 7 11 | ||
| 17807724 | ||
|
|
||
| >>> crack 7 doorsPublicKey | ||
| 11 | ||
|
|
||
| >>> transform doorsPublicKey (crack 7 cardsPublicKey) | ||
| 14897079 | ||
|
|
||
| >>> transform cardsPublicKey (crack 7 doorsPublicKey) | ||
| 14897079 | ||
| ``` | ||
|
|
||
| ## Seed values | ||
|
|
||
| I still don't know how to solve equations that have `mod` and wolfram alpha | ||
| does. | ||
|
|
||
| I got the `seed` value from here: https://www.wolframalpha.com/input/?i=%287%5Ex%29+mod+20201227%3D13135480 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| ## Day 25: Combo Breaker | ||
|
|
||
| You finally reach the check-in desk. Unfortunately, their registration systems are currently offline, and they cannot check you in. Noticing the look on your face, they quickly add that tech support is already on the way! They even created all the room keys this morning; you can take yours now and give them your room deposit once the registration system comes back online. | ||
|
|
||
| The room key is a small [RFID][1] card. Your room is on the 25th floor and the elevators are also temporarily out of service, so it takes what little energy you have left to even climb the stairs and navigate the halls. You finally reach the door to your room, swipe your card, and - _beep_ \- the light turns red. | ||
|
|
||
| Examining the card more closely, you discover a phone number for tech support. | ||
|
|
||
| "Hello! How can we help you today?" You explain the situation. | ||
|
|
||
| "Well, it sounds like the card isn't sending the right command to unlock the door. If you go back to the check-in desk, surely someone there can reset it for you." Still catching your breath, you describe the status of the elevator and the exact number of stairs you just had to climb. | ||
|
|
||
| "I see! Well, your only other option would be to reverse-engineer the cryptographic handshake the card does with the door and then inject your own commands into the data stream, but that's definitely impossible." You thank them for their time. | ||
|
|
||
| Unfortunately for the door, you know a thing or two about cryptographic handshakes. | ||
|
|
||
| The handshake used by the card and the door involves an operation that _transforms_ a _subject number_ . To transform a subject number, start with the value `1` . Then, a number of times called the _loop size_ , perform the following steps: | ||
|
|
||
| - Set the value to itself multiplied by the _subject number_ . | ||
| - Set the value to the remainder after dividing the value by _`20201227`_ . | ||
|
|
||
| The card always uses a specific, secret _loop size_ when it transforms a subject number. The door always uses a different, secret loop size. | ||
|
|
||
| The cryptographic handshake works like this: | ||
|
|
||
| - The _card_ transforms the subject number of _`7`_ according to the _card's_ secret loop size. The result is called the _card's public key_ . | ||
| - The _door_ transforms the subject number of _`7`_ according to the _door's_ secret loop size. The result is called the _door's public key_ . | ||
| - The card and door use the wireless RFID signal to transmit the two public keys (your puzzle input) to the other device. Now, the _card_ has the _door's_ public key, and the _door_ has the _card's_ public key. Because you can eavesdrop on the signal, you have both public keys, but neither device's loop size. | ||
| - The _card_ transforms the subject number of _the door's public key_ according to the _card's_ loop size. The result is the _encryption key_ . | ||
| - The _door_ transforms the subject number of _the card's public key_ according to the _door's_ loop size. The result is the same _encryption key_ as the _card_ calculated. | ||
|
|
||
| If you can use the two public keys to determine each device's loop size, you will have enough information to calculate the secret _encryption key_ that the card and door use to communicate; this would let you send the `unlock` command directly to the door! | ||
|
|
||
| For example, suppose you know that the card's public key is `5764801` . With a little trial and error, you can work out that the card's loop size must be _`8`_ , because transforming the initial subject number of `7` with a loop size of `8` produces `5764801` . | ||
|
|
||
| Then, suppose you know that the door's public key is `17807724` . By the same process, you can determine that the door's loop size is _`11`_ , because transforming the initial subject number of `7` with a loop size of `11` produces `17807724` . | ||
|
|
||
| At this point, you can use either device's loop size with the other device's public key to calculate the _encryption key_ . Transforming the subject number of `17807724` (the door's public key) with a loop size of `8` (the card's loop size) produces the encryption key, _`14897079`_ . (Transforming the subject number of `5764801` (the card's public key) with a loop size of `11` (the door's loop size) produces the same encryption key: _`14897079`_ .) | ||
|
|
||
| _What encryption key is the handshake trying to establish?_ | ||
|
|
||
| ## Part Two | ||
|
|
||
| The light turns green and the door unlocks. As you collapse onto the bed in your room, your pager goes off! | ||
|
|
||
| "It's an emergency!" the Elf calling you explains. "The [soft serve][2] machine in the cafeteria on sub-basement 7 just failed and you're the only one that knows how to fix it! We've already dispatched a reindeer to your location to pick you up." | ||
|
|
||
| You hear the sound of hooves landing on your balcony. | ||
|
|
||
| The reindeer carefully explores the contents of your room while you figure out how you're going to pay the _50 stars_ you owe the resort before you leave. Noticing that you look concerned, the reindeer wanders over to you; you see that it's carrying a small pouch. | ||
|
|
||
| "Sorry for the trouble," a note in the pouch reads. Sitting at the bottom of the pouch is a gold coin with a little picture of a starfish on it. | ||
|
|
||
| Looks like you only needed _49 stars_ after all. | ||
|
|
||
| ## Link | ||
|
|
||
| [https://adventofcode.com/2020/day/25][3] | ||
|
|
||
| [1]: https://en.wikipedia.org/wiki/Radio-frequency_identification | ||
| [2]: https://en.wikipedia.org/wiki/Soft_serve | ||
| [3]: https://adventofcode.com/2020/day/25 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| module Day25.Solution | ||
| ( crack, | ||
| findEncryptionKey, | ||
| part1, | ||
| transform, | ||
| ) | ||
| where | ||
|
|
||
| part1 :: String -> String | ||
| part1 = show . findEncryptionKey 7 1591838 . readPublicKeys | ||
|
|
||
| type LoopSize = Integer | ||
|
|
||
| type SubjectNumber = Integer | ||
|
|
||
| readPublicKeys :: String -> (Integer, Integer) | ||
| readPublicKeys = asPair . map read . lines | ||
| where | ||
| asPair (a : b : _) = (a, b) | ||
| asPair _ = undefined | ||
|
|
||
| transform :: SubjectNumber -> LoopSize -> SubjectNumber | ||
| transform subjectNumber privateKey = (subjectNumber ^ privateKey) `mod` 20201227 | ||
|
|
||
| crack :: LoopSize -> Integer -> SubjectNumber -> SubjectNumber | ||
| crack loopSize privateKey publicKey | ||
| | transform loopSize privateKey == publicKey = privateKey | ||
| | otherwise = undefined | ||
|
|
||
| findEncryptionKey :: LoopSize -> Integer -> (Integer, Integer) -> Integer | ||
| findEncryptionKey loopSize seed (a, b) = transform b (crack loopSize seed a) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| module Day25.SolutionSpec (spec) where | ||
|
|
||
| import Day25.Solution | ||
| ( crack, | ||
| findEncryptionKey, | ||
| part1, | ||
| transform, | ||
| ) | ||
| import Test.Hspec | ||
|
|
||
| spec :: Spec | ||
| spec = parallel $ do | ||
| it "solves Part 1" $ do | ||
| input <- readFile "./test/Day25/input.txt" | ||
| part1 input `shouldBe` "8329514" | ||
|
|
||
| let cardsPrivateKey = 8 | ||
| let cardsPublicKey = 5764801 | ||
| let doorsPrivateKey = 11 | ||
| let doorsPublicKey = 17807724 | ||
| let encryptionKey = 14897079 | ||
| describe "transform" $ do | ||
| context "given cardsPrivateKey" $ | ||
| it "is cardsPublicKey" $ do | ||
| transform 7 cardsPrivateKey `shouldBe` cardsPublicKey | ||
| context "given doorsPrivateKey" $ | ||
| it "is doorsPublicKey" $ do | ||
| transform 7 doorsPrivateKey `shouldBe` doorsPublicKey | ||
| describe "crack" $ do | ||
| context "given cardsPublicKey" $ | ||
| it "is cardsPrivateKey" $ do | ||
| crack 7 cardsPrivateKey cardsPublicKey `shouldBe` cardsPrivateKey | ||
| context "given doorsPublicKey" $ | ||
| it "is doorsPrivateKey" $ do | ||
| crack 7 doorsPrivateKey doorsPublicKey `shouldBe` doorsPrivateKey | ||
| describe "findEncryptionKey" $ do | ||
| it "is the encryptionKey" $ do | ||
| findEncryptionKey 7 doorsPrivateKey (doorsPublicKey, cardsPublicKey) `shouldBe` encryptionKey | ||
| it "is the encryptionKey" $ do | ||
| findEncryptionKey 7 cardsPrivateKey (cardsPublicKey, doorsPublicKey) `shouldBe` encryptionKey |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 13135480 | ||
| 8821721 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO
I still don't know how to solve equations with
modin them, for example, solve(7^x) mod 20201227 = 13135480forx.However, WolframAlpha does:
https://www.wolframalpha.com/input/?i=%287%5Ex%29+mod+20201227%3D13135480
So, for now, I'm hardcoding the value.