Skip to content

Commit

Permalink
feat(2015.25-haskell): solve Part One
Browse files Browse the repository at this point in the history
  • Loading branch information
yurrriq committed May 17, 2024
1 parent 5f7e4f8 commit 4fd46d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023.6.2.11
2023.6.2.12
1 change: 1 addition & 0 deletions input/2015/day25.txt
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.
3 changes: 3 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ executables:
main: AdventOfCode.Year2015.Day24
dependencies:
- safe
aoc-2015-day25:
<<: *executable
main: AdventOfCode.Year2015.Day25
aoc-2016-day05:
<<: *executable
main: AdventOfCode.Year2016.Day05
Expand Down
42 changes: 42 additions & 0 deletions src/AdventOfCode/Year2015/Day25.hs
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 #-}

0 comments on commit 4fd46d6

Please sign in to comment.