-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
260 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -19,3 +19,4 @@ import Y2016.D02 as X | |
import Y2016.D09 as X | ||
import Y2016.D10 as X | ||
import Y2016.D12 as X | ||
import Y2016.D13 as X |
This file contains 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,52 @@ | ||
{-| | ||
Module: Y2016.D13 | ||
Description: Advent of Code Day 13 Solutions. | ||
License: MIT | ||
Maintainer: @tylerjl | ||
Solutions to the day 13 set of problems for <adventofcode.com>. | ||
-} | ||
module Y2016.D13 (floodOffice, officePath) where | ||
|
||
import Algorithm.Search (dijkstra) | ||
import Data.Bits (popCount) | ||
import Data.Set (Set) | ||
import qualified Data.Set as S | ||
import Y2015.Util ((<&&>)) | ||
|
||
type Point = (Int, Int) | ||
data Tile = Space | Wall deriving Show | ||
|
||
officePath :: Int -> Point -> Maybe Int | ||
officePath seed target = fst | ||
<$> dijkstra (neighbors seed) (const . const 1) (== target) (1, 1) | ||
|
||
floodOffice :: Int -> Int -> Set Point | ||
floodOffice seed maxSteps = go 0 S.empty (1, 1) | ||
where | ||
go :: Int -> Set Point -> Point -> Set Point | ||
go steps seen node | ||
| steps > maxSteps || node `S.member` seen = S.empty | ||
| otherwise = S.unions | ||
$ S.singleton node | ||
: map (go (succ steps) (node `S.insert` seen)) (neighbors seed node) | ||
|
||
neighbors :: Int -> Point -> [Point] | ||
neighbors seed point = filter (inBounds <&&> (open . tileAt seed)) | ||
$ adjacentTo point | ||
where | ||
open Space = True | ||
open Wall = False | ||
inBounds (x, y) = x >= 0 && y >= 0 | ||
|
||
adjacentTo :: Point -> [Point] | ||
adjacentTo (x, y) = | ||
[ (x, y - 1) | ||
, (x - 1, y), (x + 1, y) | ||
, (x, y + 1) | ||
] | ||
|
||
tileAt :: Int -> Point -> Tile | ||
tileAt seed (x, y) = if even (popCount q) then Space else Wall | ||
where | ||
q = seed + ((x * x) + (3 * x) + (2 * (x * y)) + y + (y * y)) |
This file contains 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 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,12 @@ | ||
module Y2016.D13Spec (spec) where | ||
|
||
import Y2016 | ||
|
||
import Test.Hspec | ||
|
||
spec :: Spec | ||
spec = parallel $ do | ||
describe "Day 13" $ do | ||
describe "officePath" $ do | ||
it "solves the example" $ do | ||
officePath 10 (7, 4) `shouldBe` Just 11 |