<commit_before>-- http://codeforces.com/problemset/problem/1256/D

import           Data.List
import           Data.Maybe


zeroChar = '0'
oneChar = '1'


data Case = Case { moves  :: Int
                 , string :: String }


getMoves :: String -> Int
getMoves string = read $ last $ words string :: Int


toCase :: (String, String) -> Case
toCase (one, two) = Case { moves = getMoves one, string = two }


toPairs :: [a] -> [(a,a)]
toPairs []                  = []
toPairs [_]                 = error "list size is odd"
toPairs (first:second:tail) = (first, second) : toPairs tail


calculateIndicesAfterMoves :: [Int] -> Int -> Int -> [Int]
calculateIndicesAfterMoves [] _ _ = []
calculateIndicesAfterMoves initialZeros 0 _ = initialZeros
calculateIndicesAfterMoves (initialZero:initialZeros) moves lastZeroIndex =
  newZeroIndex : calculateIndicesAfterMoves initialZeros updatedMoves (lastZeroIndex + 1)
  where desiredMoves = initialZero - lastZeroIndex
        allowedMoves = min desiredMoves moves
        updatedMoves = moves - allowedMoves
        newZeroIndex = initialZero - allowedMoves


buildFinalList :: [Int] -> Int -> String -> String
buildFinalList [] index ones = ones
buildFinalList (zero:zeros) index (one:ones)
  | zero == index = zeroChar : buildFinalList zeros (index + 1) ones
  | otherwise     = one : buildFinalList (zero:zeros) (index + 1) ones


solve :: Case -> String
solve (Case moves string) = do
  let initialZeroIndices = elemIndices zeroChar string
  let finalZeroIndices   = calculateIndicesAfterMoves initialZeroIndices moves 0
  let ones               = replicate (length string) oneChar
  buildFinalList finalZeroIndices 0 ones


main :: IO ()
main = interact $
  unlines . map (id . solve . toCase) . -- Why id? See: https://stackoverflow.com/a/12104586/2420718
  toPairs . tail . lines
<commit_msg>Improve Haskell implementation. Still fails on test 15<commit_after>-- http://codeforces.com/problemset/problem/1256/D

import Data.List

zeroChar = '0'
oneChar = '1'


data Case = Case { moves  :: Int
                 , string :: String }


getMoves :: String -> Int
getMoves string = read $ last $ words string :: Int


toCase :: (String, String) -> Case
toCase (one, two) = Case { moves = getMoves one, string = two }


toPairs :: [a] -> [(a,a)]
toPairs []                  = []
toPairs [_]                 = error "list size is odd"
toPairs (first:second:tail) = (first, second) : toPairs tail


calculateIndicesAfterMoves :: [Int] -> [Int] -> Int -> [Int]
calculateIndicesAfterMoves [] _ _ = []
calculateIndicesAfterMoves initialZeros _ 0 = initialZeros
calculateIndicesAfterMoves (initialZero:initialZeros) (ideal:ideals) moves =
  newZeroIndex : calculateIndicesAfterMoves initialZeros ideals updatedMoves
  where desiredMoves = abs $ initialZero - ideal
        allowedMoves = min desiredMoves moves
        updatedMoves = moves - allowedMoves
        newZeroIndex = initialZero - allowedMoves


buildFinalList :: [Int] -> Int -> String -> String
buildFinalList [] index ones = ones
buildFinalList (zero:zeros) index (one:ones)
  | zero == index = zeroChar : buildFinalList zeros (index + 1) ones
  | otherwise     = one : buildFinalList (zero:zeros) (index + 1) ones


solve :: Case -> String
solve (Case moves string) = do
  let initialZeroIndices = elemIndices zeroChar string
  let ideals             = [0..(length initialZeroIndices)]
  let finalZeroIndices   = calculateIndicesAfterMoves initialZeroIndices ideals moves
  let ones               = replicate (length string) oneChar
  buildFinalList finalZeroIndices 0 ones


main :: IO ()
main = interact $
  unlines . map (id . solve . toCase) . -- Why id? See: https://stackoverflow.com/a/12104586/2420718
  toPairs . tail . lines

{'fraction_non_alphanumeric': 0.10436178753010436, 'fraction_numerical': 0.014717687985014717, 'mean_word_length': 3.238095238095238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3381488', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1125, 'n_words': 335}