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
3 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{-| | ||
Module : Main | ||
Description : Day 16 solution | ||
Copyright : (c) Eric Mertens, 2019 | ||
License : ISC | ||
Maintainer : emertens@gmail.com | ||
<https://adventofcode.com/2019/day/16> | ||
This code is not fast but if you let it run it will finish. | ||
I still want to figure out a more efficient solution. | ||
-} | ||
module Main (main) where | ||
|
||
import Advent | ||
import Control.Applicative | ||
import qualified Data.Vector.Unboxed as V | ||
|
||
main :: IO () | ||
main = | ||
do [inp] <- getParsedLines 16 (many anySingle) | ||
let ns = digits inp | ||
|
||
putStrLn $ concatMap show $ V.toList $ V.take 8 $ iterate fft ns !! 100 | ||
|
||
let offset = read (take 7 inp) | ||
|
||
let ns' = V.concat (replicate 10000 ns) | ||
putStrLn $ concatMap show $ V.toList $ V.take 8 $ V.drop offset $ iterate fft ns' !! 100 | ||
|
||
digits :: String -> V.Vector Int | ||
digits = V.fromList . map (read . pure) | ||
|
||
fft :: V.Vector Int -> V.Vector Int | ||
fft xs = V.generate (V.length xs) one | ||
where | ||
n = V.length xs | ||
ps = V.scanl (+) 0 xs | ||
factors i = takeWhile (\(Region _ a _) -> a < n) (regions i) | ||
|
||
one i = abs $ sum [ m * (ps V.! min n end - ps V.! start) | Region m start end <- factors i] | ||
`rem` 10 | ||
|
||
data Region = Region !Int !Int !Int | ||
deriving Show | ||
|
||
regions :: Int -> [Region] | ||
regions i = go 0 | ||
where | ||
n = i + 1 | ||
go offset = Region 1 (offset + i) (offset + i+n) | ||
: Region (-1) (offset + i + n*2) (offset + i+n*2+ n) | ||
: go (offset + 4 * n) |
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 @@ | ||
59782619540402316074783022180346847593683757122943307667976220344797950034514416918778776585040527955353805734321825495534399127207245390950629733658814914072657145711801385002282630494752854444244301169223921275844497892361271504096167480707096198155369207586705067956112600088460634830206233130995298022405587358756907593027694240400890003211841796487770173357003673931768403098808243977129249867076581200289745279553289300165042557391962340424462139799923966162395369050372874851854914571896058891964384077773019120993386024960845623120768409036628948085303152029722788889436708810209513982988162590896085150414396795104755977641352501522955134675 |