This commit is contained in:
Yann Esposito (Yogsototh) 2017-12-06 00:25:25 +01:00
parent 6c832066f4
commit 36d0387be5
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
3 changed files with 82 additions and 3 deletions

View file

@ -30,13 +30,16 @@ library
exposed-modules: exposed-modules:
Day1 Day1
Day2 Day2
other-modules:
Day3 Day3
other-modules:
Day4
Paths_adventofcode Paths_adventofcode
build-depends: build-depends:
base >=4.7 && <5 base >=4.7 && <5
, protolude , protolude
, containers
, foldl , foldl
, text
default-language: Haskell2010 default-language: Haskell2010
executable adventofcode-exe executable adventofcode-exe

View file

@ -15,10 +15,13 @@ library:
exposed-modules: exposed-modules:
- Day1 - Day1
- Day2 - Day2
- Day3
dependencies: dependencies:
- base >=4.7 && <5 - base >=4.7 && <5
- protolude - protolude
- containers
- foldl - foldl
- text
executables: executables:
adventofcode-exe: adventofcode-exe:
main: Main.hs main: Main.hs

View file

@ -1,5 +1,4 @@
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-| {-|
description: description:
@ -25,13 +24,38 @@ For example:
- Data from square 1024 must be carried 31 steps. - Data from square 1024 must be carried 31 steps.
How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port? How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port?
--- Part Two ---
As a stress test on the system, the programs here clear the grid and then store the value 1 in square 1. Then, in the same allocation order as shown above, they store the sum of the values in all adjacent squares, including diagonals.
So, the first few squares' values are chosen as follows:
Square 1 starts with the value 1.
Square 2 has only one adjacent filled square (with value 1), so it also stores 1.
Square 3 has both of the above squares as neighbors and stores the sum of their values, 2.
Square 4 has all three of the aforementioned squares as neighbors and stores the sum of their values, 4.
Square 5 only has the first and fourth squares as neighbors, so it gets the value 5.
Once a square is written, its value does not change. Therefore, the first few squares would receive the following values:
147 142 133 122 59
304 5 4 2 57
330 10 1 1 54
351 11 23 25 26
362 747 806---> ...
What is the first value written that is larger than your puzzle input?
|-} |-}
module Day2 where module Day3 where
import Protolude import Protolude
import qualified Control.Foldl as F import qualified Control.Foldl as F
import Data.List (words,lines) import Data.List (words,lines)
import qualified Data.Map as Map
input = 265149 input = 265149
@ -60,3 +84,52 @@ returnPathLength i =
& fmap (\(_,x,y) -> abs x + abs y) & fmap (\(_,x,y) -> abs x + abs y)
-- Solution 2
type Spiral = Map (Int,Int) Integer
spiralDebug = go Map.empty (0,0)
where
go s coord = let (nextspiral,nextcoord,val) = nextPoint s coord
neigh = neighbor s coord
in (val,nextcoord,neigh):go nextspiral nextcoord
spiral2 = map (\(x,_,_) -> x) spiralDebug
solution2 = head $ dropWhile (< 265149) spiral2
neighbor :: Spiral -> (Int,Int) -> [Maybe Integer]
neighbor spiral (x,y) =
[ (xn,yn) `Map.lookup` spiral |yn <- [y+1,y,y-1], xn <- [x-1,x,x+1]]
sumMaybes :: [Maybe Integer] -> Integer
sumMaybes l = max 1 $ maybe 1 sum (sequence (filter isJust l))
nextPoint :: Spiral -> (Int,Int) -> (Spiral,(Int,Int),Integer)
nextPoint spiral (x,y) =
let neigh = neighbor spiral (x,y)
s = sumMaybes neigh
coord = next neigh (x,y)
newSpiral = Map.insert (x,y) s spiral
in (newSpiral,coord,s)
next :: [Maybe Integer] -> (Int,Int) -> (Int,Int)
next [_ ,Nothing,_
,Nothing,_ ,Nothing
,_ ,Nothing,_ ] (x,y) = (x+1,y)
next [_ ,Nothing,_
,Just _, _ ,_
,_ ,_ ,_ ] (x,y) = (x,y+1)
next [_ ,_ ,_
,Nothing,_ ,_
,_ , Just _, _] (x,y) = (x-1,y)
next [_ ,_ ,_
,_ ,_ ,Just _
,_ ,Nothing, _] (x,y) = (x,y-1)
next [_ ,Just _, _
,_ ,_ ,Nothing
,_ ,_ ,_ ] (x,y) = (x+1,y)