After the experiments of the previous quest, quest 3 was much simpler.
I created a Vote type to store votes, and a simple bit of string splitting populated it.
data Vote = Vote [Int] [Int] -- bases, then votes
deriving (Show, Eq, Ord)
parseVote :: String -> Vote
parseVote text = Vote (fmap digitToInt $ baVo !! 0) (fmap digitToInt $ baVo !! 1)
where content = (splitOn "-" text) !! 1
baVo = splitOn " " contentGiven the lists of Int values, calculating the final vote total was done with a couple of calls to zipWith. I had to take care of reversing the lists and repeating the bases to keep things valid. That did parts one and two.
voteToDec :: Vote -> Int
voteToDec (Vote bases ns) = sum $ zipWith (*) weights $ reverse ns
where weights = zipWith (^) (cycle $ reverse bases) [0..]For part three, I didn't do anything special, just count up the "votes" field to see if there were any smaller "votes" that gave the same result. I could use that to filter the input to only include valid votes.
validIso2 vote = not $ any ((== target) . voteToDec) $ smallerVotes vote
where target = voteToDec vote
smallerVotes :: Vote -> [Vote]
smallerVotes (Vote bases ns) = [Vote bases (fmap digitToInt $ show x) | x <- [1..limit]]
where limit = (foldl' (\a d -> a * 10 + d) 0 ns) - 1Code
You can get the code from Codeberg. or on my self-hosted Git site.