From 6cf726976e228ca8cf2aa4e2b9458add2cc62416 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 10 Jan 2021 14:20:48 +0000 Subject: [PATCH] Done day 25 --- advent25/package.yaml | 60 +++++++++++++++ advent25/src/advent25.hs | 43 +++++++++++ data/advent25.txt | 2 + problems/day25.html | 154 +++++++++++++++++++++++++++++++++++++++ stack.yaml | 1 + 5 files changed, 260 insertions(+) create mode 100644 advent25/package.yaml create mode 100644 advent25/src/advent25.hs create mode 100644 data/advent25.txt create mode 100644 problems/day25.html diff --git a/advent25/package.yaml b/advent25/package.yaml new file mode 100644 index 0000000..cb87dbb --- /dev/null +++ b/advent25/package.yaml @@ -0,0 +1,60 @@ +# This YAML file describes your package. Stack will automatically generate a +# Cabal file when you run `stack build`. See the hpack website for help with +# this file: . + +name: advent25 +synopsis: Advent of Code +version: '0.0.1' + +default-extensions: +- AllowAmbiguousTypes +- ApplicativeDo +- BangPatterns +- BlockArguments +- DataKinds +- DeriveFoldable +- DeriveFunctor +- DeriveGeneric +- DeriveTraversable +- EmptyCase +- FlexibleContexts +- FlexibleInstances +- FunctionalDependencies +- GADTs +- GeneralizedNewtypeDeriving +- ImplicitParams +- KindSignatures +- LambdaCase +- MonadComprehensions +- MonoLocalBinds +- MultiParamTypeClasses +- MultiWayIf +- NamedFieldPuns +- NegativeLiterals +- NumDecimals +# - OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeFamilies +- TypeInType +- TypeOperators +- ViewPatterns + + +executables: + advent25: + main: advent25.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - arithmoi diff --git a/advent25/src/advent25.hs b/advent25/src/advent25.hs new file mode 100644 index 0000000..548f71e --- /dev/null +++ b/advent25/src/advent25.hs @@ -0,0 +1,43 @@ +-- import Debug.Trace + +-- import Math.NumberTheory.Moduli.DiscreteLogarithm +-- import Data.Finite (Finite, modulo, getFinite) +-- import GHC.TypeNats (KnownNat) +-- import Data.Maybe + +import Data.Semigroup + +newtype MExp = MExp Integer + deriving (Show, Eq) + +instance Semigroup MExp where + (MExp a) <> (MExp b) = MExp $ (a * b) `mod` modularBase + + +subject :: MExp +subject = MExp 7 + +modularBase :: Integer +modularBase = 20201227 + +main :: IO () +main = + do text <- readFile "data/advent25.txt" + let [cardKey, doorKey] = map read $ lines text + print $ part1 cardKey doorKey + +part1 cardKey doorKey = publicKey + where cardRounds = findRounds cardKey + MExp publicKey = stimes cardRounds (MExp doorKey) + +findRounds :: Integer -> Integer +findRounds target = fst $ until (foundRounds t) countingExponential (0, seed) + where t = MExp target + seed = MExp 1 + +foundRounds :: MExp -> (Integer, MExp) -> Bool +foundRounds target (_n, v) = v == target + +countingExponential :: (Integer, MExp) -> (Integer, MExp) +countingExponential (n, v) = (n + 1, v <> subject) + diff --git a/data/advent25.txt b/data/advent25.txt new file mode 100644 index 0000000..89d2212 --- /dev/null +++ b/data/advent25.txt @@ -0,0 +1,2 @@ +12578151 +5051300 \ No newline at end of file diff --git a/problems/day25.html b/problems/day25.html new file mode 100644 index 0000000..a43d9db --- /dev/null +++ b/problems/day25.html @@ -0,0 +1,154 @@ + + + + +Day 25 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 50*

  {:year 2020}

+ + + +
+ +

--- 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 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?

+
+

Your puzzle answer was 296776.

--- 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 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.

+
+

If you like, you can .

+

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, all that is left is for you to admire your Advent calendar.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 0c9524d..3c1855f 100644 --- a/stack.yaml +++ b/stack.yaml @@ -59,6 +59,7 @@ packages: - advent22 - advent23 - advent24 +- advent25 # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as -- 2.34.1