aboutsummaryrefslogtreecommitdiff
path: root/Puzzle13.hs
blob: 3ed1be846cb0251350372867b3edae817119d3d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-
Copyright (C) 2017 Yuchen Pei.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with this program.  If not, see
<https://www.gnu.org/licenses/>.
-}
import Data.List.Split (splitOn)

f :: (Int, Int) -> Int
f (x, y) = if (y == 1) || (x `mod` (y * 2 - 2) == 0) then x * y else 0

g :: Int -> (Int, Int) -> Bool
g z (x, y) = (z + x) `mod` (2 * y - 2) /= 0

solve2' :: Int -> [(Int, Int)] -> Int
solve2' z xs
  | all (g z) xs = z
  | otherwise = solve2' (z + 1) xs

solve2 :: [Char] -> Int
solve2 = (solve2' 0) . parseInput

parseInput :: [Char] -> [(Int, Int)]
parseInput xs = (\ys -> let [x, y] = words ys in (read $ init x, read y)) <$> (splitOn "\n" xs)

solve1 :: [Char] -> Int
solve1 = sum . (fmap f) . parseInput

test = "0: 3\n1: 2\n4: 4\n6: 4"

input = "0: 3\n1: 2\n2: 6\n4: 4\n6: 4\n8: 8\n10: 6\n12: 8\n14: 5\n16: 6\n18: 8\n20: 8\n22: 12\n24: 6\n26: 9\n28: 8\n30: 12\n32: 12\n34: 17\n36: 12\n38: 8\n40: 12\n42: 12\n44: 10\n46: 12\n48: 12\n50: 12\n52: 14\n54: 14\n56: 10\n58: 14\n60: 12\n62: 14\n64: 14\n66: 14\n68: 14\n70: 14\n72: 14\n74: 14\n76: 14\n86: 14\n94: 20\n96: 18"