Private Proxies – Buy Cheap Private Elite USA Proxy + 50% Discount!Private Proxies – Buy Cheap Private Elite USA Proxy + 50% Discount!Private Proxies – Buy Cheap Private Elite USA Proxy + 50% Discount!Private Proxies – Buy Cheap Private Elite USA Proxy + 50% Discount!
    0
  •   was successfully added to your cart.
  • Home
  • Buy proxies
  • Extra features
  • Help
  • Contact
  • Login
  • 50% OFF
    BUY NOW!
    50
    PROXIES
    $19
    --------------------
    BUY NOW!
    BUY NOW!
    BUY NOW!
    BUY NOW!
    BUY NOW!
    $29
    $49
    $109
    $179
    $299
    --------------------
    --------------------
    --------------------
    --------------------
    --------------------
    PROXIES
    PROXIES
    PROXIES
    PROXIES
    PROXIES
    100
    200
    500
    1,000
    2,000
    TOP SELLER
    BEST VALUE
    For All Private Proxies!

Until recently, I was very much only devoted to imperative languages (mainly C++ and C, to be precise), when I decided to venture into unknown waters by picking up a new, completely different language, which happened to be Haskell, a decision which happened to be influenced by the fact that I owned a copy of “Learn You a Haskell for Great Good!”, a book which I very much enjoyed learning from.

Some days ago, I finished said book, and, wanting to apply my newly acquired knowledge, ventured our to find some programming exercises. I quickly remembered Advent of Code, which offers a whole pre-Christmas period’s worth of easy to mildly difficult programming problems, a few of which I had already solved during the holidays.

I skimmed through the exercises, looking for one simple enough to be conquerable with my still very inadequate and shaky Haskell skills, and finally chose the task of Day 22.

Problem Description

Diagnostics indicate that the local grid computing cluster has been contaminated with the Sporifica Virus. The grid computing cluster is a seemingly-infinite two-dimensional grid of compute nodes. Each node is either clean or infected by the virus.

To prevent overloading the nodes (which would render them useless to the virus) or detection by system administrators, exactly one virus carrier moves through the network, infecting or cleaning nodes as it moves. The virus carrier is always located on a single node in the network (the current node) and keeps track of the direction it is facing.

To avoid detection, the virus carrier works in bursts; in each burst, it wakes up, does some work, and goes back to sleep. The following steps are all executed in order one time each burst:

  • If the current node is infected, it turns to its right. Otherwise, it turns to its left. (Turning is done in-place; the current node does not change.)
  • If the current node is clean, it becomes infected. Otherwise, it becomes cleaned. (This is done after the node is considered for the purposes of changing direction.)
  • The virus carrier moves forward one node in the direction it is facing. Diagnostics have also provided a map of the node infection status (your puzzle input).

Clean nodes are shown as .; infected nodes are shown as #. This map only shows the center of the grid; there are many more nodes beyond those shown, but none of them are currently infected.

The virus carrier begins in the middle of the map facing up.

(The full puzzle description, including examples, is available on the official AoC website.)

The mentioned puzzle input consists of a file containing a grid of . and #.

My Solution

import Data.List.Index import qualified Data.Set as Set import System.Environment import System.IO import qualified System.IO.Strict as IOS  type Position = (Int, Int) type Dimensions = (Int, Int) type NodeMap = Set.Set Position  data Rotation = Clockwise | Counterclockwise deriving (Eq, Show) data Direction = North | East | South | West deriving (Eq, Show)   nextDirection :: Rotation -> Direction -> Direction nextDirection rot dir     | rot == Clockwise && dir == West         = North     | rot == Clockwise && dir == East         = South     | rot == Clockwise && dir == North        = East     | rot == Clockwise && dir == South        = West     | rot == Counterclockwise && dir == West  = South     | rot == Counterclockwise && dir == East  = North     | rot == Counterclockwise && dir == North = West     | rot == Counterclockwise && dir == South = East  parseInput :: String -> (Dimensions, NodeMap) parseInput s = ((length . head . lines $   s, length . lines $   s),     foldl (\set (index, char) ->         if char == '#'             then Set.insert index set              else set)     Set.empty     . ifoldl (\ls index line ->         ls ++ zipWith (\ix (i, char) ->              ((i, ix), char))         (repeat index) (indexed line))     []     . lines $   s)  simulateNBurstsImpl :: Int -> (Int, Position, Direction, NodeMap)     -> (Int, Position, Direction, NodeMap) simulateNBurstsImpl 0 x = x simulateNBurstsImpl i (count, pos, dir, map) = simulateNBurstsImpl     (i - 1) transitionFunction      where         step (a, b) dir             | dir == West  = (a - 1, b)             | dir == East  = (a + 1, b)             | dir == North = (a, b - 1)             | dir == South = (a, b + 1)         transitionFunction              | Set.member pos map == True = let                     nextDir = nextDirection Clockwise dir                 in (count, step pos nextDir, nextDir, Set.delete pos map)             | Set.member pos map == False = let                     nextDir = nextDirection Counterclockwise dir                 in (count + 1, step pos nextDir, nextDir, Set.insert pos map)  simulateNBursts :: Int -> Dimensions -> NodeMap -> Int simulateNBursts i (width, height) map =     first (simulateNBurstsImpl i (0, startingPos, North, map))     where         startingPos = (width `quot` 2, height `quot` 2)         first (a, _, _, _) = a   main = getArgs >>= \(filename : _) ->     withFile filename ReadMode IOS.hGetContents >>= \content ->         let parsed = parseInput content in         print . simulateNBursts 10000 (fst parsed) . snd $   parsed 

Notes:

  • The program takes a single argument on the commandline; the path to the file containing the starting grid.
  • I decided on using a set as the underlying data structure as it makes it easy to work with growing grids. My first attempt used a two-dimensional sequence, but extending the grid turned out to be too much of a hassle for my taste.
  • The code assumes that all inputs are valid, including the fact that a commandline parameter was passed and points to a valid file.

Review Requests

Please feel free to review anything and everything that comes to mind! That said, I do have a few concrete questions:

  1. Having simulateNBursts as a beautified interface to simulateNBurstsImpl seems kind of ugly to me. Is there a way to clean this up and join the two functions? Or is this a common pattern?
  2. How readable is this code? As its author, I find it hard to judge how (un-)pleasant this code is to the eye of a third person, especially since I have next to no experience in reading and writing Haskell code. What can I do to improve readability?
  3. nextDirection seems very verbose to me. Is there a more concise way to implement it?

✓ Extra quality

ExtraProxies brings the best proxy quality for you with our private and reliable proxies

✓ Extra anonymity

Top level of anonymity and 100% safe proxies – this is what you get with every proxy package

✓ Extra speed

1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!

50 proxies

$19/month

50% DISCOUNT!
$0.38 per proxy
✓ Private
✓ Elite
✓ Anonymous
Buy now

100 proxies

$29/month

50% DISCOUNT!
$0.29 per proxy
✓ Private
✓ Elite
✓ Anonymous
Buy now

200 proxies

$49/month

50% DISCOUNT!
$0.25 per proxy
✓ Private
✓ Elite
✓ Anonymous
Buy now

500 proxies

$109/month

50% DISCOUNT!
$0.22 per proxy
✓ Private
✓ Elite
✓ Anonymous
Buy now

1,000 proxies

$179/month

50% DISCOUNT!
$0.18 per proxy
✓ Private
✓ Elite
✓ Anonymous
Buy now

2,000 proxies

$299/month

50% DISCOUNT!
$0.15 per proxy
✓ Private
✓ Elite
✓ Anonymous
Buy now

USA proxy location

We offer premium quality USA private proxies – the most essential proxies you can ever want from USA

100% anonymous

Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies

Unlimited bandwidth

Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!

Superfast speed

Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!

99,9% servers uptime

Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems

No usage restrictions

You have freedom to use your proxies with every software, browser or website you want without restrictions

Perfect for SEO

We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies

Big discounts

Buy more proxies and get better price – we offer various proxy packages with great deals and discounts

Premium support

We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!

Satisfaction guarantee

24/7 premium support, free proxy activation and 100% safe payments! Best reliability private proxies for your needs!

Best Proxy Packs

  • 2,000 Private Proxies $600.00 $299.00 / month
  • 1,000 Private Proxies $360.00 $179.00 / month

Quick Links

  • More information
  • Contact us
  • Privacy Policy
  • Terms and Conditions

Like And Follow Us


Copyright ExtraProxies.com | All Rights Reserved.
  • Checkout
  • Contact
  • Help
  • Home
  • My Account
  • My Cart
  • News
  • Privacy Policy
  • Proxy features
  • Proxy packs
  • Terms and Conditions
Private Proxies – Buy Cheap Private Elite USA Proxy + 50% Discount!
    0 items