Created
January 9, 2013 06:36
-
-
Save mattsan/4491137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "answer.hpp" | |
#include <sstream> | |
bool between(int x1, int x2, int x) | |
{ | |
return ((x1 - x) * (x2 - x)) <= 0; | |
} | |
bool inside(int x1, int y1, int x2, int y2, int x, int y) | |
{ | |
return between(x1, x2, x) && between(y1, y2, y); | |
} | |
std::string solve(const std::string& s) | |
{ | |
std::istringstream iss(s); | |
int p1, p2, p3, p4, p5, p6; | |
char h1, h2, c1, h3, h4; | |
iss >> p1 >> h1 >> p2 >> h2 >> p3 >> c1 >> p4 >> h3 >> p5 >> h4 >> p6; | |
if((h1 != '-') || (h2 != '-') || (h3 != '-') || (h4 != '-') || (c1 != ',') || ( ! iss.eof())) | |
{ | |
return "-"; | |
} | |
int x11 = p1 / 10, y11 = p1 % 10; | |
int x12 = p2 / 10, y12 = p2 % 10; | |
int x13 = p3 / 10, y13 = p3 % 10; | |
int x21 = p4 / 10, y21 = p4 % 10; | |
int x22 = p5 / 10, y22 = p5 % 10; | |
int x23 = p6 / 10, y23 = p6 % 10; | |
int count = 0; | |
for(int y = 0; y < 10; ++y) | |
{ | |
for(int x = 0; x < 10; ++x) | |
{ | |
if( (inside(x11, y11, x12, y12, x, y) || inside(x11, y11, x13, y13, x, y)) && (inside(x21, y21, x22, y22, x, y) || inside(x21, y21, x23, y23, x, y)) ) | |
{ | |
++count; | |
} | |
} | |
} | |
std::ostringstream oss; | |
oss << count; | |
return oss.str(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DOUKAKU_ANSWER_HPP | |
#define DOUKAKU_ANSWER_HPP | |
#include <string> | |
std::string solve(const std::string& s); | |
#endif//DOUKAKU_ANSWER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Answer(solve) where | |
type Pos = (Int, Int) | |
between x1 x2 x = (x1 - x) * (x2 - x) <= 0 | |
inside x1 y1 x2 y2 x y = (between x1 x2 x) && (between y1 y2 y) | |
l_intersect :: (Pos, Pos, Pos) -> (Pos, Pos, Pos) -> String | |
l_intersect ((x11, y11), (x12, y12), (x13, y13)) ((x21, y21), (x22, y22), (x23, y23)) = | |
show $ length [1| x <- [0..9], y<-[0..9], ((inside x11 y11 x12 y12 x y) || (inside x11 y11 x13 y13 x y)) && ((inside x21 y21 x22 y22 x y) || (inside x21 y21 x23 y23 x y))] | |
solve :: String -> String | |
solve [x11,y11,'-',x12,y12,'-',x13,y13,',',x21,y21,'-',x22,y22,'-',x23,y23] = | |
l_intersect ((read [x11], read [y11]), (read [x12], read [y12]), (read [x13], read [y13])) | |
((read [x21], read [y21]), (read [x22], read [y22]), (read [x23], read [y23])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1 | 23-94-28,89-06-51 | 11 | |
---|---|---|---|
#2 | 11-84-58,02-73-69 | 40 | |
#3 | 18-41-86,77-04-32 | 26 | |
#4 | 81-88-23,64-58-14 | 0 | |
#5 | 31-29-07,41-87-69 | 0 | |
#6 | 83-13-40,18-10-94 | 1 | |
#7 | 77-80-92,21-72-38 | 2 | |
#8 | 57-70-91,55-19-08 | 3 | |
#9 | 18-22-75,66-80-91 | 4 | |
#10 | 51-93-78,54-49-06 | 5 | |
#11 | 58-70-96,17-43-76 | 6 | |
#12 | 58-07-12,58-82-93 | 7 | |
#13 | 41-29-07,35-95-88 | 8 | |
#14 | 88-26-60,42-29-07 | 9 | |
#15 | 18-40-85,34-40-91 | 10 | |
#16 | 36-60-96,53-96-89 | 11 | |
#17 | 51-39-02,44-98-69 | 12 | |
#18 | 48-06-20,76-04-42 | 13 | |
#19 | 85-29-18,26-50-93 | 14 | |
#20 | 27-50-91,43-29-07 | 15 | |
#21 | 57-06-20,48-60-91 | 16 | |
#22 | 52-98-89,21-76-67 | 17 | |
#23 | 67-12-40,45-80-92 | 18 | |
#24 | 47-03-10,26-30-82 | 19 | |
#25 | 74-28-06,21-86-37 | 20 | |
#26 | 65-01-20,73-39-05 | 21 | |
#27 | 17-72-86,36-50-94 | 22 | |
#28 | 51-29-07,77-15-41 | 23 | |
#29 | 33-98-39,82-16-02 | 24 | |
#30 | 75-05-10,37-81-96 | 25 | |
#31 | 72-58-06,48-80-96 | 26 | |
#32 | 81-67-16,21-91-59 | 27 | |
#33 | 13-96-57,24-96-79 | 28 | |
#34 | 57-04-32,51-18-06 | 29 | |
#35 | 88-03-52,28-41-86 | 30 | |
#36 | 78-04-61,13-86-49 | 31 | |
#37 | 58-12-20,27-50-85 | 32 | |
#38 | 61-19-05,71-68-15 | 33 | |
#39 | 63-29-16,18-31-83 | 34 | |
#40 | 16-50-91,32-98-79 | 35 | |
#41 | 82-17-03,38-40-81 | 36 | |
#42 | 72-48-04,11-98-39 | 37 | |
#43 | 77-05-10,28-50-62 | 38 | |
#44 | 38-50-91,11-86-57 | 39 | |
#45 | 87-05-10,13-97-69 | 40 | |
#46 | 11-86-49,22-98-89 | 44 | |
#47 | 11-97-69,12-86-67 | 46 | |
#48 | 11-95-69,71-49-05 | 47 | |
#49 | 28-31-92,13-98-79 | 48 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "answer.hpp" | |
#include <string> | |
#include <iostream> | |
#include <fstream> | |
struct Pattern | |
{ | |
Pattern(const std::string& s) : name(), input(), expected() | |
{ | |
std::size_t d1 = s.find('\t'); | |
std::size_t d2 = s.find('\t', d1 + 1); | |
if((d1 != std::string::npos) && (d2 != std::string::npos)) | |
{ | |
name = s.substr(0, d1); | |
input = s.substr(d1 + 1, d2 - d1 - 1); | |
expected = s.substr(d2 + 1); | |
} | |
} | |
std::string name; | |
std::string input; | |
std::string expected; | |
}; | |
int main(int, char* []) | |
{ | |
std::ifstream patterns("patterns.tsv"); | |
int count_cases = 0; | |
int count_failures = 0; | |
std::string s; | |
while(std::getline(patterns, s).good()) | |
{ | |
++count_cases; | |
Pattern pattern(s); | |
std::string actual = solve(pattern.input); | |
if(actual != pattern.expected) | |
{ | |
++count_failures; | |
std::cout << "Failure in " << pattern.name << "\n" | |
<< "expected: \"" << pattern.expected << "\"\n" | |
<< " actual: \"" << actual << "\"\n"; | |
} | |
} | |
std::cout << "\nCases: " << count_cases << " Failures: " << count_failures << "\n"; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Test.HUnit | |
import Answer | |
-- referring to source of words | |
-- http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.0/src/Data-List.html#words | |
split :: (a -> Bool) -> [a] -> [[a]] | |
split f s = | |
case dropWhile f s of | |
[] -> [] | |
s' -> w : split f s'' | |
where (w, s'') = break f s' | |
doAssert :: [String] -> Assertion | |
doAssert (name:input:expected:_) = | |
assertEqual name expected (solve input) | |
doAssert _ = error "Specify a list which contains more than 3 items." | |
main :: IO () | |
main = | |
readFile "patterns.tsv" | |
>>= runTestTT . test . map (doAssert . split (== '\t')) . lines | |
>> return () | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment