aboutsummaryrefslogtreecommitdiff
path: root/Puzzle13.hs
blob: 742329807e14675c2768630944925f9e6030b933 (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
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"