{-# Language BangPatterns, QuasiQuotes #-}
module Main where
import Advent (format)
import Data.List (delete)
main :: IO ()
IO ()
main =
do [(Int, Int)]
input <- [format|2017 24 (%d/%d%n)*|]
let bridges :: [(Int, Int)]
bridges = Int -> Int -> Int -> [(Int, Int)] -> [(Int, Int)]
search Int
0 Int
0 Int
0 [(Int, Int)]
input
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (((Int, Int) -> Int) -> [(Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Int) -> Int
forall a b. (a, b) -> b
snd [(Int, Int)]
bridges))
Int -> IO ()
forall a. Show a => a -> IO ()
print ((Int, Int) -> Int
forall a b. (a, b) -> b
snd ([(Int, Int)] -> (Int, Int)
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [(Int, Int)]
bridges))
orient ::
Int ->
(Int,Int) ->
[(Int,Int)]
orient :: Int -> (Int, Int) -> [(Int, Int)]
orient Int
a (Int
b,Int
c)
| Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
b = [(Int
b,Int
c)]
| Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
c = [(Int
c,Int
b)]
| Bool
otherwise = []
search ::
Int ->
Int ->
Int ->
[(Int,Int)] ->
[(Int,Int)]
search :: Int -> Int -> Int -> [(Int, Int)] -> [(Int, Int)]
search !Int
len !Int
weight !Int
match [(Int, Int)]
pieces =
(Int
len,Int
weight) (Int, Int) -> [(Int, Int)] -> [(Int, Int)]
forall a. a -> [a] -> [a]
:
do (Int, Int)
piece <- [(Int, Int)]
pieces
(Int
a,Int
b) <- Int -> (Int, Int) -> [(Int, Int)]
orient Int
match (Int, Int)
piece
Int -> Int -> Int -> [(Int, Int)] -> [(Int, Int)]
search (Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int
weightInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
aInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
b) Int
b ((Int, Int) -> [(Int, Int)] -> [(Int, Int)]
forall a. Eq a => a -> [a] -> [a]
delete (Int, Int)
piece [(Int, Int)]
pieces)