Skip to content

Commit

Permalink
Adds 5th concept step to tie it all together
Browse files Browse the repository at this point in the history
  • Loading branch information
stewartmurrie committed Nov 17, 2024
1 parent 40ad002 commit 6a60cab
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
11 changes: 11 additions & 0 deletions exercises/concept/secrets/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
- [One of the bitwise functions][bitwise-and] clears bits where the bit in the mask is 0.
- But, you may need to combine it with [another function][bitwise-complement] to clear bits where the mask is 1.

## 5. Decrypt a message

- Apply the other functions you wrote to the input in the following order, taking the output of one and using it as the input to the next one:

1. `setBits`
2. `flipBits`
3. `shiftBack`
4. `clearBits`

For step 4, you'll need to convert the binary number with the 1st and 5th bits set (10001) to decimal.

[bitwise-docs]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise
[bitwise-shiftRightZfBy]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#shiftRightZfBy
[bitwise-or]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#or
Expand Down
16 changes: 15 additions & 1 deletion exercises/concept/secrets/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ flipBits 23 157 --> 138

## 4. Clear specific bits

Lastly, there are also certain bits that always decrypt to 0.
There are also certain bits that always decrypt to 0.

Implement the `clearBits` functions that takes a mask and a value.
The bits in the `value` should be set to 0 where the bit in the mask is 1.
Expand All @@ -52,3 +52,17 @@ All other bits should be kept unchanged.
```elm
clearBits 2 15 --> 13
```

## 5. Decrypt a message

Now that you have all the functions you need, you can decode your friend's message.
Implement the `decrypt` function that performs the following operations:

1. Set the bits from the year your friend was born (1996)
2. Flip the result with the year that you first met (2009)
3. Shift the bits back by the number of classes you take together (5)
4. Clear the first and fifth bit.

```elm
decrypt 380182 -->
```
10 changes: 9 additions & 1 deletion exercises/concept/secrets/.meta/Exemplar.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Secrets exposing (clearBits, flipBits, setBits, shiftBack)
module Secrets exposing (clearBits, flipBits, setBits, shiftBack, decrypt)

import Bitwise

Expand All @@ -19,3 +19,11 @@ clearBits mask value =
mask
|> Bitwise.complement
|> Bitwise.and value


decrypt secret =
secret
|> setBits 1996
|> flipBits 2009
|> shiftBack 5
|> clearBits 17
6 changes: 5 additions & 1 deletion exercises/concept/secrets/src/Secrets.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Secrets exposing (clearBits, flipBits, setBits, shiftBack)
module Secrets exposing (clearBits, decrypt, flipBits, setBits, shiftBack)


shiftBack amount value =
Expand All @@ -15,3 +15,7 @@ flipBits mask value =

clearBits mask value =
Debug.todo "Please implement clearBits"


decrypt secret =
Debug.todo "Please implement decrypt"
10 changes: 10 additions & 0 deletions exercises/concept/secrets/tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ tests =
Secrets.clearBits 240 90
|> Expect.equal 10
]
, describe "5"
[ test "Decrypt 12345" <|
\_ ->
Secrets.decrypt 12345
|> Expect.equal 384
, test "Decrypt 123456789" <|
\_ ->
Secrets.decrypt 123456789
|> Expect.equal 3857984
]
]

0 comments on commit 6a60cab

Please sign in to comment.