-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2015.25-haskell): solve Part One
- Loading branch information
Showing
4 changed files
with
47 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2023.6.2.11 | ||
2023.6.2.12 |
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 @@ | ||
To continue, please consult the code grid in the manual. Enter the code at row 3010, column 3019. |
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,42 @@ | ||
module AdventOfCode.Year2015.Day25 where | ||
|
||
import AdventOfCode.Input (parseInput) | ||
import AdventOfCode.TH (inputFilePath) | ||
import Control.Monad (void) | ||
import Text.Trifecta (Parser, natural, string) | ||
|
||
type Coordinates = (Integer, Integer) | ||
|
||
main :: IO () | ||
main = | ||
do | ||
putStr "Part One: " | ||
print . partOne =<< getInput | ||
|
||
partOne :: Coordinates -> Integer | ||
partOne (column, row) = 20151125 * (252533 ^ (n - 1)) `mod` 33554393 | ||
where | ||
n = a000124 row + a000217 (column + row - 1) - a000217 row | ||
|
||
getInput :: IO Coordinates | ||
getInput = parseInput coordinates $(inputFilePath) | ||
|
||
coordinates :: Parser Coordinates | ||
coordinates = | ||
do | ||
void $ string "To continue, please consult the code grid in the manual. " | ||
row <- string "Enter the code at row " *> natural | ||
column <- string ", column " *> natural | ||
pure (column, row) | ||
|
||
-- https://oeis.org/A000124 | ||
-- https://en.wikipedia.org/wiki/Lazy_caterer's_sequence | ||
a000124 :: (Integral a) => a -> a | ||
a000124 n = n * (n - 1) `div` 2 + 1 | ||
{-# SPECIALIZE INLINE a000124 :: Integer -> Integer #-} | ||
|
||
-- https://oeis.org/A000217 | ||
-- https://en.wikipedia.org/wiki/Triangular_number | ||
a000217 :: (Integral a) => a -> a | ||
a000217 n = n * (n + 1) `div` 2 | ||
{-# SPECIALIZE INLINE a000217 :: Integer -> Integer #-} |