2020/12/29 に開催した ESM どう書く Returns の解答です。
-
-
Save mattsan/7f48dd6c84a30d6c1357f4d4be6021a9 to your computer and use it in GitHub Desktop.
esmどう書く (2020/12/29) の解答
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
// 実行方法 | |
// $ g++ solve.cpp -o solve --std=c++11 | |
// $ ./solve < data.txt | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <iostream> | |
#include <sstream> | |
#include <iterator> | |
using namespace std; | |
int char_to_number(char c) { | |
return ((c - 64) % 10 + 8) % 9; | |
} | |
bool is_bingo(int n) { | |
static const int pattern[] = { 0x007, 0x038, 0x1c0, 0x49, 0x92, 0x124, 0x111, 0x054 }; | |
return find(begin(pattern), end(pattern), n) != end(pattern); | |
} | |
template<typename Iterator> | |
bool is_wbingo(Iterator i1, Iterator i2, Iterator i3) { | |
Iterator itrs[] = { i1, i2, i3 }; | |
int alphs[3]; | |
int nums[3]; | |
transform(begin(itrs), end(itrs), begin(alphs), [](Iterator itr){ return 1 << char_to_number(*itr); }); | |
transform(begin(itrs), end(itrs), begin(nums), [](Iterator itr){ return 1 << (*++itr - '1'); }); | |
int alph = alphs[0] | alphs[1] | alphs[2]; | |
int num = nums[0] | nums[1] | nums[2]; | |
return is_bingo(alph) && is_bingo(num); | |
} | |
template<typename Iterator> | |
string gen_triple(Iterator i1, Iterator i2, Iterator i3) { | |
string cards[] = { string(i1, i1 + 2), string(i2, i2 + 2), string(i3, i3 + 2) }; | |
sort(begin(cards), end(cards)); | |
ostringstream oss; | |
oss << cards[0] << cards[1] << cards[2]; | |
return oss.str(); | |
} | |
template<typename Iterator> | |
string gen_result(Iterator begin, Iterator end) { | |
ostringstream oss; | |
copy(begin, end, ostream_iterator<string>(oss, ";")); | |
string result = oss.str(); | |
result.pop_back(); | |
return result; | |
} | |
string solve(const string& input) { | |
vector<string> triples; | |
for (auto i1 = input.begin(); i1 < input.end(); i1 += 2) { | |
for (auto i2 = i1 + 2; i2 < input.end(); i2 += 2) { | |
for (auto i3 = i2 + 2; i3 < input.end(); i3 += 2) { | |
if (is_wbingo(i1, i2, i3)) { | |
triples.push_back(gen_triple(i1, i2, i3)); | |
} | |
} | |
} | |
} | |
sort(triples.begin(), triples.end()); | |
string result = gen_result(triples.begin(), triples.end()); | |
if (result.empty()) { | |
return "none"; | |
} else { | |
return result; | |
} | |
} | |
void test(const string& input, const string& expected) { | |
string actual = solve(input); | |
if (actual == expected) { | |
cout << "passed\n"; | |
} else { | |
cout | |
<< "failed\n" | |
<< " input : " << input << "\n" | |
<< " expected: " << expected << "\n" | |
<< " actual : " << actual << endl; | |
} | |
} | |
int main(int, char*[]) { | |
string input; | |
string expected; | |
while(cin >> input) { | |
cin >> expected; | |
test(input, expected); | |
} | |
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
# 実行方法 | |
# $ $ elixir solve.exs < data.txt | |
defmodule WBingo do | |
@aset MapSet.new(['ABC', 'DEF', 'GHJ', 'ADG', 'BEH', 'CFJ', 'AEJ', 'CEG']) | |
@nset MapSet.new(['123', '456', '789', '147', '258', '369', '159', '357']) | |
def wbingo?([[a1, n1], [a2, n2], [a3, n3]]) do | |
MapSet.member?(@aset, Enum.sort([a1, a2, a3])) && MapSet.member?(@nset, Enum.sort([n1, n2, n3])) | |
end | |
def combination(_, 0), do: [] | |
def combination([], _), do: [] | |
def combination(list, 1), do: Enum.map(list, &[&1]) | |
def combination([el | rest], n) do | |
Enum.map(combination(rest, n - 1), &[el | &1]) ++ combination(rest, n) | |
end | |
def solve(input) do | |
input | |
|> String.to_charlist() | |
|> Enum.chunk_every(2) | |
|> combination(3) | |
|> Enum.filter(&wbingo?/1) | |
|> Enum.map(&(&1 |> Enum.sort() |> Enum.join())) | |
|> Enum.sort() | |
|> Enum.join(";") | |
|> case do | |
"" -> "none" | |
result -> result | |
end | |
end | |
def test(input, expected) do | |
case solve(input) do | |
^expected -> | |
:passed | |
actual -> | |
{:failed, actual} | |
end | |
end | |
def test_all(io, passed_count, failed_count) do | |
case IO.read(io, :line) do | |
:eof -> | |
IO.puts(""" | |
passed: #{passed_count} | |
failed: #{failed_count} | |
""") | |
line -> | |
[input, expected] = String.split(line) | |
case test(input, expected) do | |
:passed -> | |
IO.puts("passed") | |
test_all(io, passed_count + 1, failed_count) | |
{:failed, actual} -> | |
IO.puts(""" | |
failed | |
input : #{input} | |
expected: #{expected} | |
actual : #{actual} | |
""") | |
test_all(io, passed_count, failed_count + 1) | |
end | |
end | |
end | |
def main(io) do | |
test_all(io, 0, 0) | |
end | |
end | |
WBingo.main(:stdio) |
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
# 実行方法 | |
# $ ruby solve.rb < data.txt | |
def wbingo?(triple) | |
marks, numbers = triple.transpose | |
%w(ABC DEF GHJ ADG BEH CFJ AEJ CEG).include?(marks.sort.join) && | |
%w(123 456 789 147 258 369 159 357).include?(numbers.sort.join) | |
end | |
def solve(input) | |
result = input.chars | |
.each_slice(2) | |
.to_a | |
.combination(3) | |
.select {|cards| wbingo?(cards) } | |
.map {|cards| cards.sort.join } | |
.sort | |
.join(';') | |
if result.empty? | |
'none' | |
else | |
result | |
end | |
end | |
def test(index, input, expected) | |
actual = solve(input) | |
if expected == actual | |
print '.' | |
true | |
else | |
puts | |
puts <<~FAILED | |
failed #{index} | |
expected: #{expected} | |
actual: #{actual} | |
FAILED | |
false | |
end | |
end | |
def main(io) | |
results = | |
io.map.with_index(1) do |line, index| | |
test(index, *line.split) | |
end | |
puts | |
puts <<~RESULT | |
passed: #{results.count {|result| result }} | |
failed: #{results.count {|result| !result }} | |
RESULT | |
end | |
main(STDIN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment