Kattis Ants challenge
Multi tool use
$begingroup$
I am trying to learn Haskell doing some contest problems. I have worked a bit with the language but still have a very long way ahead.
Right now I am working with a problem called Ants:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
Input
The first line of input contains one integer giving the number of cases that follow, at most 100. The data for each case start with two integer numbers: the length l of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are between 0 and 1000000 and they are separated by whitespace.
Output
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Sample input
2
10 3
2 6 7
214 7
11 12 7 13
176 23 191
It is pretty straight forward, some parsing and minor calculation. My program finished running in 0.69 seconds, while the best result someone achieved with Haskell is something like 0.03! It is very annoying.
I have tried to speed it up with some even uglier code, but the best I have managed with this is 0.66 seconds.
Can anyone spot some obvious things about this code to improve the runtime?
main = do
contents <- getContents
let cases = parse contents
mapM_ putStrLn (map showcase cases)
type Case = (Int, [Int])
showcase :: Case -> String
showcase (len, positions) =
let ds = dists len positions
edgiest = (snd . maximum) ds
es = max (len - edgiest) edgiest
midmost = (snd . minimum) ds
mm = min (len - midmost) midmost
in show mm ++ " " ++ show es
dists :: Int -> [Int] -> [(Float, Int)]
dists l = map (x -> (abs (fromIntegral x - m), x))
where m = fromIntegral l / 2.0
parse :: String -> [Case]
parse = parseCases . tail . words -- skip the first integer
parseCases :: [String] -> [Case]
parseCases =
parseCases xs =
let (cs, rest) = parseCase xs
in cs : parseCases rest
parseCase :: [String] -> (Case, [String])
parseCase (len:a:xs) =
let ants = read a
positions = (map read . take ants) xs
rest = drop ants xs
in ((read len, positions), rest)
performance programming-challenge haskell
$endgroup$
add a comment |
$begingroup$
I am trying to learn Haskell doing some contest problems. I have worked a bit with the language but still have a very long way ahead.
Right now I am working with a problem called Ants:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
Input
The first line of input contains one integer giving the number of cases that follow, at most 100. The data for each case start with two integer numbers: the length l of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are between 0 and 1000000 and they are separated by whitespace.
Output
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Sample input
2
10 3
2 6 7
214 7
11 12 7 13
176 23 191
It is pretty straight forward, some parsing and minor calculation. My program finished running in 0.69 seconds, while the best result someone achieved with Haskell is something like 0.03! It is very annoying.
I have tried to speed it up with some even uglier code, but the best I have managed with this is 0.66 seconds.
Can anyone spot some obvious things about this code to improve the runtime?
main = do
contents <- getContents
let cases = parse contents
mapM_ putStrLn (map showcase cases)
type Case = (Int, [Int])
showcase :: Case -> String
showcase (len, positions) =
let ds = dists len positions
edgiest = (snd . maximum) ds
es = max (len - edgiest) edgiest
midmost = (snd . minimum) ds
mm = min (len - midmost) midmost
in show mm ++ " " ++ show es
dists :: Int -> [Int] -> [(Float, Int)]
dists l = map (x -> (abs (fromIntegral x - m), x))
where m = fromIntegral l / 2.0
parse :: String -> [Case]
parse = parseCases . tail . words -- skip the first integer
parseCases :: [String] -> [Case]
parseCases =
parseCases xs =
let (cs, rest) = parseCase xs
in cs : parseCases rest
parseCase :: [String] -> (Case, [String])
parseCase (len:a:xs) =
let ants = read a
positions = (map read . take ants) xs
rest = drop ants xs
in ((read len, positions), rest)
performance programming-challenge haskell
$endgroup$
add a comment |
$begingroup$
I am trying to learn Haskell doing some contest problems. I have worked a bit with the language but still have a very long way ahead.
Right now I am working with a problem called Ants:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
Input
The first line of input contains one integer giving the number of cases that follow, at most 100. The data for each case start with two integer numbers: the length l of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are between 0 and 1000000 and they are separated by whitespace.
Output
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Sample input
2
10 3
2 6 7
214 7
11 12 7 13
176 23 191
It is pretty straight forward, some parsing and minor calculation. My program finished running in 0.69 seconds, while the best result someone achieved with Haskell is something like 0.03! It is very annoying.
I have tried to speed it up with some even uglier code, but the best I have managed with this is 0.66 seconds.
Can anyone spot some obvious things about this code to improve the runtime?
main = do
contents <- getContents
let cases = parse contents
mapM_ putStrLn (map showcase cases)
type Case = (Int, [Int])
showcase :: Case -> String
showcase (len, positions) =
let ds = dists len positions
edgiest = (snd . maximum) ds
es = max (len - edgiest) edgiest
midmost = (snd . minimum) ds
mm = min (len - midmost) midmost
in show mm ++ " " ++ show es
dists :: Int -> [Int] -> [(Float, Int)]
dists l = map (x -> (abs (fromIntegral x - m), x))
where m = fromIntegral l / 2.0
parse :: String -> [Case]
parse = parseCases . tail . words -- skip the first integer
parseCases :: [String] -> [Case]
parseCases =
parseCases xs =
let (cs, rest) = parseCase xs
in cs : parseCases rest
parseCase :: [String] -> (Case, [String])
parseCase (len:a:xs) =
let ants = read a
positions = (map read . take ants) xs
rest = drop ants xs
in ((read len, positions), rest)
performance programming-challenge haskell
$endgroup$
I am trying to learn Haskell doing some contest problems. I have worked a bit with the language but still have a very long way ahead.
Right now I am working with a problem called Ants:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
Input
The first line of input contains one integer giving the number of cases that follow, at most 100. The data for each case start with two integer numbers: the length l of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are between 0 and 1000000 and they are separated by whitespace.
Output
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Sample input
2
10 3
2 6 7
214 7
11 12 7 13
176 23 191
It is pretty straight forward, some parsing and minor calculation. My program finished running in 0.69 seconds, while the best result someone achieved with Haskell is something like 0.03! It is very annoying.
I have tried to speed it up with some even uglier code, but the best I have managed with this is 0.66 seconds.
Can anyone spot some obvious things about this code to improve the runtime?
main = do
contents <- getContents
let cases = parse contents
mapM_ putStrLn (map showcase cases)
type Case = (Int, [Int])
showcase :: Case -> String
showcase (len, positions) =
let ds = dists len positions
edgiest = (snd . maximum) ds
es = max (len - edgiest) edgiest
midmost = (snd . minimum) ds
mm = min (len - midmost) midmost
in show mm ++ " " ++ show es
dists :: Int -> [Int] -> [(Float, Int)]
dists l = map (x -> (abs (fromIntegral x - m), x))
where m = fromIntegral l / 2.0
parse :: String -> [Case]
parse = parseCases . tail . words -- skip the first integer
parseCases :: [String] -> [Case]
parseCases =
parseCases xs =
let (cs, rest) = parseCase xs
in cs : parseCases rest
parseCase :: [String] -> (Case, [String])
parseCase (len:a:xs) =
let ants = read a
positions = (map read . take ants) xs
rest = drop ants xs
in ((read len, positions), rest)
performance programming-challenge haskell
performance programming-challenge haskell
edited 1 min ago
200_success
129k15152415
129k15152415
asked 6 hours ago
AmozAmoz
284
284
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211786%2fkattis-ants-challenge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211786%2fkattis-ants-challenge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
K0n7dDtwHLAFwq4qvs77DzOcXRPilLmjb5Ogqbq4U