Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AdventOfCode2020.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ library
Day21.Solution
Day22.Solution
Day24.Solution
Day25.Solution
Practice.Foldable
Template.Solution
other-modules:
Expand Down Expand Up @@ -107,6 +108,7 @@ test-suite AdventOfCode2020-test
Day21.SolutionSpec
Day22.SolutionSpec
Day24.SolutionSpec
Day25.SolutionSpec
Practice.FoldableSpec
Template.SolutionSpec
Paths_AdventOfCode2020
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ Solutions to adventofcode.com/2020
| [PR #38](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/38) | Day 22: Crab Combat | [src/Day22/Solution.hs](src/Day22/Solution.hs) | [test/Day22/SolutionSpec.hs](test/Day22/SolutionSpec.hs) |
| [PR #40](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/40) | Day 23: Crab Cups | `[in progress, 2 stars]` | `[in progress]` |
| [PR #44](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/44) | Day 24: Lobby Layout | [src/Day24/Solution.hs](src/Day24/Solution.hs) | [test/Day24/SolutionSpec.hs](test/Day24/SolutionSpec.hs) |
| [PR #45](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/45) | Day 25: Combo Breaker | `[in progress, 1 star ]` | `[in progress]` |
| [PR #45](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/45) | Day 25: Combo Breaker | [src/Day25/Solution.hs](src/Day25/Solution.hs) | [test/Day25/SolutionSpec.hs](test/Day25/SolutionSpec.hs) |

<!-- | [PR #25](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/25) | Day 15: Rambunctious Recitation | [src/Day15/Solution.hs](src/Day15/Solution.hs) | [test/Day15/SolutionSpec.hs](test/Day15/SolutionSpec.hs) | -->
<!-- | [PR #40](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/40) | Day 23: Crab Cups | [src/Day23/Solution.hs](src/Day23/Solution.hs) | [test/Day23/SolutionSpec.hs](test/Day23/SolutionSpec.hs) | -->
<!-- | [PR #45](https://github.com/manuphatak/HaskellAdventOfCode2020/pull/45) | Day 25: Combo Breaker | [src/Day25/Solution.hs](src/Day25/Solution.hs) | [test/Day25/SolutionSpec.hs](test/Day25/SolutionSpec.hs) | -->

## Run tests

Expand Down
61 changes: 61 additions & 0 deletions src/Day25/NOTES.md
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
62 changes: 62 additions & 0 deletions src/Day25/README.md
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
31 changes: 31 additions & 0 deletions src/Day25/Solution.hs
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

Copy link
Copy Markdown
Owner Author

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 mod in them, for example, solve (7^x) mod 20201227 = 13135480 for x.

However, WolframAlpha does:

https://www.wolframalpha.com/input/?i=%287%5Ex%29+mod+20201227%3D13135480

So, for now, I'm hardcoding the value.


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)
40 changes: 40 additions & 0 deletions test/Day25/SolutionSpec.hs
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
2 changes: 2 additions & 0 deletions test/Day25/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
13135480
8821721