#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/ITP1_11_A"
#include"../../utilities/dice.hpp"#include<array>
#include<cassert>
#include"../../utilities/fast_io.hpp"
#include<set>
#include<string>namespace{usingm1une::utilities::Dice;usingm1une::utilities::DiceDirection;usingm1une::utilities::DiceFace;voidtest_rolls(){Dice<int>original(1,6,5,2,3,4);Dice<int>north=original;north.roll_north();std::array<int,6>expected_north{2,5,1,6,3,4};assert(north.faces()==expected_north);north.roll_south();assert(north==original);Dice<int>east=original;east.roll_east();std::array<int,6>expected_east{4,3,5,2,1,6};assert(east.faces()==expected_east);east.roll_west();assert(east==original);Dice<int>cycle=original;for(intstep=0;step<4;++step)cycle.roll(DiceDirection::north);assert(cycle==original);for(intstep=0;step<4;++step)cycle.rotate_clockwise();assert(cycle==original);cycle.rotate_clockwise().rotate_counterclockwise();assert(cycle==original);}voidtest_orientations(){Dice<int>die(0,1,2,3,4,5);std::array<Dice<int>,24>orientations=die.orientations();std::set<std::array<int,6>>distinct;std::array<int,6>top_count{};for(constDice<int>&oriented:orientations){distinct.insert(oriented.faces());++top_count[static_cast<std::size_t>(oriented.top())];assert(oriented[DiceFace::bottom]==oriented.bottom());}assert(distinct.size()==24);for(intcount:top_count)assert(count==4);for(inttop=0;top<6;++top){for(intsouth=0;south<6;++south){boolare_opposite=(top==0&&south==1)||(top==1&&south==0)||(top==2&&south==3)||(top==3&&south==2)||(top==4&&south==5)||(top==5&&south==4);autooriented=die.orientation(top,south);assert(oriented.has_value()==(top!=south&&!are_opposite));if(oriented.has_value()){assert(oriented->top()==top);assert(oriented->south()==south);}}}}voidtest_equivalence(){Dice<std::string>first("T","B","N","S","E","W");Dice<std::string>rotated=first;rotated.roll_north().roll_east().rotate_clockwise();assert(first!=rotated);assert(first.equivalent(rotated));Dice<std::string>reflected("T","B","N","S","W","E");assert(!first.equivalent(reflected));Dice<int>repeated(1,1,2,2,3,3);assert(repeated.orientations().size()==24);assert(repeated.equivalent(repeated));}voidtest_opposites(){usingm1une::utilities::opposite;for(DiceFaceface:{DiceFace::top,DiceFace::bottom,DiceFace::north,DiceFace::south,DiceFace::east,DiceFace::west}){assert(opposite(opposite(face))==face);}}}// namespaceintmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;test_rolls();test_orientations();test_equivalence();test_opposites();std::array<int,6>input;for(int&value:input)fast_input>>value;Dice<int>die(input[0],input[5],input[4],input[1],input[2],input[3]);std::stringcommands;fast_input>>commands;for(charcommand:commands){if(command=='N')die.roll_north();if(command=='S')die.roll_south();if(command=='E')die.roll_east();if(command=='W')die.roll_west();}fast_output<<die.top()<<'\n';}
#line 1 "verify/utilities/dice.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/ITP1_11_A"
#line 1 "utilities/dice.hpp"
#include<array>
#include<cstddef>
#include<optional>
#include<stdexcept>
#include<string>
#include<utility>
#include<vector>namespacem1une{namespaceutilities{enumclassDiceFace:std::size_t{top,bottom,north,south,east,west};enumclassDiceDirection{north,south,east,west};constexprDiceFaceopposite(DiceFaceface)noexcept{switch(face){caseDiceFace::top:returnDiceFace::bottom;caseDiceFace::bottom:returnDiceFace::top;caseDiceFace::north:returnDiceFace::south;caseDiceFace::south:returnDiceFace::north;caseDiceFace::east:returnDiceFace::west;caseDiceFace::west:returnDiceFace::east;}returnDiceFace::top;}constexprDiceFacerotate_direction(DiceFacedirection,DiceDirectionrotation)noexcept{switch(rotation){caseDiceDirection::north:switch(direction){caseDiceFace::top:returnDiceFace::north;caseDiceFace::north:returnDiceFace::bottom;caseDiceFace::bottom:returnDiceFace::south;caseDiceFace::south:returnDiceFace::top;default:returndirection;}caseDiceDirection::south:switch(direction){caseDiceFace::top:returnDiceFace::south;caseDiceFace::south:returnDiceFace::bottom;caseDiceFace::bottom:returnDiceFace::north;caseDiceFace::north:returnDiceFace::top;default:returndirection;}caseDiceDirection::east:switch(direction){caseDiceFace::top:returnDiceFace::east;caseDiceFace::east:returnDiceFace::bottom;caseDiceFace::bottom:returnDiceFace::west;caseDiceFace::west:returnDiceFace::top;default:returndirection;}caseDiceDirection::west:switch(direction){caseDiceFace::top:returnDiceFace::west;caseDiceFace::west:returnDiceFace::bottom;caseDiceFace::bottom:returnDiceFace::east;caseDiceFace::east:returnDiceFace::top;default:returndirection;}}returndirection;}constexprDiceFacerotate_direction_clockwise(DiceFacedirection)noexcept{switch(direction){caseDiceFace::north:returnDiceFace::east;caseDiceFace::east:returnDiceFace::south;caseDiceFace::south:returnDiceFace::west;caseDiceFace::west:returnDiceFace::north;default:returndirection;}}constexprDiceFacerotate_direction_counterclockwise(DiceFacedirection)noexcept{switch(direction){caseDiceFace::north:returnDiceFace::west;caseDiceFace::west:returnDiceFace::south;caseDiceFace::south:returnDiceFace::east;caseDiceFace::east:returnDiceFace::north;default:returndirection;}}template<typenameT>classDice{private:staticconstexprstd::size_tface_count=6;std::array<T,face_count>faces_;staticconstexprstd::size_tindex(DiceFaceface)noexcept{returnstatic_cast<std::size_t>(face);}public:constexprDice(Ttop,Tbottom,Tnorth,Tsouth,Teast,Twest):faces_{std::move(top),std::move(bottom),std::move(north),std::move(south),std::move(east),std::move(west)}{}explicitconstexprDice(std::array<T,face_count>faces):faces_(std::move(faces)){}[[nodiscard]]constexprconstT&operator[](DiceFaceface)constnoexcept{returnfaces_[index(face)];}[[nodiscard]]constexprT&operator[](DiceFaceface)noexcept{returnfaces_[index(face)];}[[nodiscard]]constexprconstT&top()constnoexcept{return(*this)[DiceFace::top];}[[nodiscard]]constexprconstT&bottom()constnoexcept{return(*this)[DiceFace::bottom];}[[nodiscard]]constexprconstT&north()constnoexcept{return(*this)[DiceFace::north];}[[nodiscard]]constexprconstT&south()constnoexcept{return(*this)[DiceFace::south];}[[nodiscard]]constexprconstT&east()constnoexcept{return(*this)[DiceFace::east];}[[nodiscard]]constexprconstT&west()constnoexcept{return(*this)[DiceFace::west];}[[nodiscard]]constexprconststd::array<T,face_count>&faces()constnoexcept{returnfaces_;}constexprDice&roll_north(){Told_top=std::move((*this)[DiceFace::top]);(*this)[DiceFace::top]=std::move((*this)[DiceFace::south]);(*this)[DiceFace::south]=std::move((*this)[DiceFace::bottom]);(*this)[DiceFace::bottom]=std::move((*this)[DiceFace::north]);(*this)[DiceFace::north]=std::move(old_top);return*this;}constexprDice&roll_south(){Told_top=std::move((*this)[DiceFace::top]);(*this)[DiceFace::top]=std::move((*this)[DiceFace::north]);(*this)[DiceFace::north]=std::move((*this)[DiceFace::bottom]);(*this)[DiceFace::bottom]=std::move((*this)[DiceFace::south]);(*this)[DiceFace::south]=std::move(old_top);return*this;}constexprDice&roll_east(){Told_top=std::move((*this)[DiceFace::top]);(*this)[DiceFace::top]=std::move((*this)[DiceFace::west]);(*this)[DiceFace::west]=std::move((*this)[DiceFace::bottom]);(*this)[DiceFace::bottom]=std::move((*this)[DiceFace::east]);(*this)[DiceFace::east]=std::move(old_top);return*this;}constexprDice&roll_west(){Told_top=std::move((*this)[DiceFace::top]);(*this)[DiceFace::top]=std::move((*this)[DiceFace::east]);(*this)[DiceFace::east]=std::move((*this)[DiceFace::bottom]);(*this)[DiceFace::bottom]=std::move((*this)[DiceFace::west]);(*this)[DiceFace::west]=std::move(old_top);return*this;}constexprDice&roll(DiceDirectiondirection){switch(direction){caseDiceDirection::north:returnroll_north();caseDiceDirection::south:returnroll_south();caseDiceDirection::east:returnroll_east();caseDiceDirection::west:returnroll_west();}return*this;}constexprDice&rotate_clockwise(){Told_north=std::move((*this)[DiceFace::north]);(*this)[DiceFace::north]=std::move((*this)[DiceFace::west]);(*this)[DiceFace::west]=std::move((*this)[DiceFace::south]);(*this)[DiceFace::south]=std::move((*this)[DiceFace::east]);(*this)[DiceFace::east]=std::move(old_north);return*this;}constexprDice&rotate_counterclockwise(){Told_north=std::move((*this)[DiceFace::north]);(*this)[DiceFace::north]=std::move((*this)[DiceFace::east]);(*this)[DiceFace::east]=std::move((*this)[DiceFace::south]);(*this)[DiceFace::south]=std::move((*this)[DiceFace::west]);(*this)[DiceFace::west]=std::move(old_north);return*this;}[[nodiscard]]constexprstd::array<Dice,24>orientations()const{Dicetop_top=*this;Dicenorth_top=*this;north_top.roll_south();Dicesouth_top=*this;south_top.roll_north();Diceeast_top=*this;east_top.roll_west();Dicewest_top=*this;west_top.roll_east();Dicebottom_top=*this;bottom_top.roll_north().roll_north();std::array<Dice,6>top_orientations{std::move(top_top),std::move(north_top),std::move(south_top),std::move(east_top),std::move(west_top),std::move(bottom_top)};std::array<Dice,24>result{*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this};std::size_tresult_index=0;for(Diceoriented:top_orientations){for(introtation=0;rotation<4;++rotation){result[result_index++]=oriented;oriented.rotate_clockwise();}}returnresult;}[[nodiscard]]constexprstd::optional<Dice>orientation(constT&top_value,constT&south_value)const{for(constDice&oriented:orientations()){if(oriented.top()==top_value&&oriented.south()==south_value){returnoriented;}}returnstd::nullopt;}[[nodiscard]]constexprboolequivalent(constDice&other)const{for(constDice&oriented:orientations()){if(oriented==other){returntrue;}}returnfalse;}friendconstexprbooloperator==(constDice&,constDice&)=default;};classArrowDice{private:structVector{intx;inty;intz;friendconstexprbooloperator==(constVector&,constVector&)=default;};structFrame{Vectornormal;Vectorup;Vectorright;friendconstexprbooloperator==(constFrame&,constFrame&)=default;};Dice<DiceFace>arrows_;staticconstexprVectornegate(Vectorvector)noexcept{returnVector{-vector.x,-vector.y,-vector.z};}staticconstexprDiceFacevector_to_face(Vectorvector){if(vector==Vector{0,0,1})returnDiceFace::top;if(vector==Vector{0,0,-1})returnDiceFace::bottom;if(vector==Vector{0,1,0})returnDiceFace::north;if(vector==Vector{0,-1,0})returnDiceFace::south;if(vector==Vector{1,0,0})returnDiceFace::east;if(vector==Vector{-1,0,0})returnDiceFace::west;throwstd::invalid_argument("vector is not a cube direction");}staticconstexprFrameadjacent_frame(constFrame&frame,DiceDirectiondirection)noexcept{switch(direction){caseDiceDirection::north:returnFrame{frame.up,negate(frame.normal),frame.right};caseDiceDirection::south:returnFrame{negate(frame.up),frame.normal,frame.right};caseDiceDirection::east:returnFrame{frame.right,frame.up,negate(frame.normal)};caseDiceDirection::west:returnFrame{negate(frame.right),frame.up,frame.normal};}returnframe;}staticconstexprDiceFacearrow_from_character(chararrow,constFrame&frame){if(arrow=='^')returnvector_to_face(frame.up);if(arrow=='v')returnvector_to_face(negate(frame.up));if(arrow=='>')returnvector_to_face(frame.right);if(arrow=='<')returnvector_to_face(negate(frame.right));throwstd::invalid_argument("arrow dice net contains an invalid character");}constexprvoidrotate_arrows(DiceDirectiondirection)noexcept{for(DiceFaceface:all_faces()){arrows_[face]=rotate_direction(arrows_[face],direction);}}staticconstexprstd::array<DiceFace,6>all_faces()noexcept{returnstd::array<DiceFace,6>{DiceFace::top,DiceFace::bottom,DiceFace::north,DiceFace::south,DiceFace::east,DiceFace::west};}public:constexprArrowDice(DiceFacetop,DiceFacebottom,DiceFacenorth,DiceFacesouth,DiceFaceeast,DiceFacewest):arrows_(top,bottom,north,south,east,west){}explicitconstexprArrowDice(Dice<DiceFace>arrows):arrows_(std::move(arrows)){}[[nodiscard]]constexprDiceFacearrow(DiceFaceface)constnoexcept{returnarrows_[face];}[[nodiscard]]constexprconstDice<DiceFace>&arrows()constnoexcept{returnarrows_;}constexprArrowDice&roll(DiceDirectiondirection){arrows_.roll(direction);rotate_arrows(direction);return*this;}constexprArrowDice&roll_north(){returnroll(DiceDirection::north);}constexprArrowDice&roll_south(){returnroll(DiceDirection::south);}constexprArrowDice&roll_east(){returnroll(DiceDirection::east);}constexprArrowDice&roll_west(){returnroll(DiceDirection::west);}constexprArrowDice&rotate_clockwise(){arrows_.rotate_clockwise();for(DiceFaceface:all_faces()){arrows_[face]=rotate_direction_clockwise(arrows_[face]);}return*this;}constexprArrowDice&rotate_counterclockwise(){arrows_.rotate_counterclockwise();for(DiceFaceface:all_faces()){arrows_[face]=rotate_direction_counterclockwise(arrows_[face]);}return*this;}[[nodiscard]]constexprstd::array<ArrowDice,24>orientations()const{ArrowDicetop_top=*this;ArrowDicenorth_top=*this;north_top.roll_south();ArrowDicesouth_top=*this;south_top.roll_north();ArrowDiceeast_top=*this;east_top.roll_west();ArrowDicewest_top=*this;west_top.roll_east();ArrowDicebottom_top=*this;bottom_top.roll_north().roll_north();std::array<ArrowDice,6>top_orientations{top_top,north_top,south_top,east_top,west_top,bottom_top};std::array<ArrowDice,24>result{*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this,*this};std::size_tresult_index=0;for(ArrowDiceoriented:top_orientations){for(introtation=0;rotation<4;++rotation){result[result_index++]=oriented;oriented.rotate_clockwise();}}returnresult;}[[nodiscard]]constexprintdifference(constArrowDice&other)constnoexcept{intresult=0;for(DiceFaceface:all_faces()){result+=arrow(face)!=other.arrow(face);}returnresult;}[[nodiscard]]constexprintdistance(constArrowDice&other)const{intresult=6;for(constArrowDice&oriented:orientations()){intcurrent=oriented.difference(other);if(current<result)result=current;}returnresult;}[[nodiscard]]staticArrowDicefrom_net(conststd::vector<std::string>&net,charempty='.'){if(net.empty()){throwstd::invalid_argument("arrow dice net is empty");}std::size_twidth=net.front().size();if(width==0){throwstd::invalid_argument("arrow dice net is empty");}for(conststd::string&row:net){if(row.size()!=width){throwstd::invalid_argument("arrow dice net must be rectangular");}}intheight=static_cast<int>(net.size());intinteger_width=static_cast<int>(width);std::vector<std::vector<std::optional<Frame>>>frames(net.size(),std::vector<std::optional<Frame>>(width));autocharacter_at=[&](introw,intcolumn)->char{returnnet[static_cast<std::size_t>(row)][static_cast<std::size_t>(column)];};autoframe_at=[&](introw,intcolumn)->std::optional<Frame>&{returnframes[static_cast<std::size_t>(row)][static_cast<std::size_t>(column)];};std::vector<std::pair<int,int>>queue;intface_count=0;for(introw=0;row<height;++row){for(intcolumn=0;column<integer_width;++column){if(character_at(row,column)!=empty){++face_count;if(queue.empty())queue.emplace_back(row,column);}}}if(face_count!=6){throwstd::invalid_argument("arrow dice net must contain six faces");}Frameinitial{Vector{0,0,1},Vector{0,1,0},Vector{1,0,0}};frame_at(queue.front().first,queue.front().second)=initial;constexprstd::array<int,4>row_delta{-1,1,0,0};constexprstd::array<int,4>column_delta{0,0,1,-1};constexprstd::array<DiceDirection,4>directions{DiceDirection::north,DiceDirection::south,DiceDirection::east,DiceDirection::west};for(std::size_thead=0;head<queue.size();++head){introw=queue[head].first;intcolumn=queue[head].second;Frameframe=*frame_at(row,column);for(std::size_tedge=0;edge<directions.size();++edge){intnext_row=row+row_delta[edge];intnext_column=column+column_delta[edge];if(next_row<0||next_row>=height||next_column<0||next_column>=integer_width||character_at(next_row,next_column)==empty){continue;}Framenext_frame=adjacent_frame(frame,directions[edge]);std::optional<Frame>&known=frame_at(next_row,next_column);if(!known.has_value()){known=next_frame;queue.emplace_back(next_row,next_column);}elseif(*known!=next_frame){throwstd::invalid_argument("arrow dice net folds inconsistently");}}}if(queue.size()!=6){throwstd::invalid_argument("arrow dice net is disconnected");}std::array<std::optional<DiceFace>,6>arrows;for(conststd::pair<int,int>&cell:queue){introw=cell.first;intcolumn=cell.second;Frameframe=*frame_at(row,column);DiceFaceface=vector_to_face(frame.normal);std::optional<DiceFace>&slot=arrows[static_cast<std::size_t>(face)];if(slot.has_value()){throwstd::invalid_argument("arrow dice net overlaps when folded");}slot=arrow_from_character(character_at(row,column),frame);}for(conststd::optional<DiceFace>&arrow:arrows){if(!arrow.has_value()){throwstd::invalid_argument("arrow dice net does not form a cube");}}returnArrowDice(*arrows[static_cast<std::size_t>(DiceFace::top)],*arrows[static_cast<std::size_t>(DiceFace::bottom)],*arrows[static_cast<std::size_t>(DiceFace::north)],*arrows[static_cast<std::size_t>(DiceFace::south)],*arrows[static_cast<std::size_t>(DiceFace::east)],*arrows[static_cast<std::size_t>(DiceFace::west)]);}friendconstexprbooloperator==(constArrowDice&,constArrowDice&)=default;};}// namespace utilities}// namespace m1une#line 4 "verify/utilities/dice.test.cpp"
#line 6 "verify/utilities/dice.test.cpp"
#include<cassert>
#line 1 "utilities/fast_io.hpp"
#include<algorithm>
#line 6 "utilities/fast_io.hpp"
#include<cerrno>
#include<charconv>
#line 9 "utilities/fast_io.hpp"
#include<cstdio>
#include<cstdlib>
#include<cstdint>
#include<cstring>
#include<iterator>
#line 15 "utilities/fast_io.hpp"
#include<sys/stat.h>
#include<type_traits>
#line 18 "utilities/fast_io.hpp"
#include<unistd.h>
#line 20 "utilities/fast_io.hpp"
namespacem1une{namespaceutilities{structFastOutput;namespaceinternal{// Shared with the convenience helpers in template.hpp.inlineFastOutput*standard_output_instance=nullptr;// Detect std::begin(x), std::end(x).template<classT,class=void>structis_range:std::false_type{};template<classT>structis_range<T,std::void_t<decltype(std::begin(std::declval<T&>())),decltype(std::end(std::declval<T&>()))>>:std::true_type{};template<classT>inlineconstexprboolis_range_v=is_range<T>::value;template<classT>usingrange_reference_t=decltype(*std::begin(std::declval<T&>()));template<classT>usingrange_value_t=std::remove_cv_t<std::remove_reference_t<range_reference_t<T>>>;template<classT,class=void>structrange_stored_value{usingtype=range_value_t<T>;};template<classT>structrange_stored_value<T,std::void_t<typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type>>{usingtype=typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type;};template<classT>usingrange_stored_value_t=typenamerange_stored_value<T>::type;// Treat strings and C strings as scalar output objects, not as ranges.template<classT>structis_char_array:std::false_type{};template<classT,std::size_tN>structis_char_array<T[N]>:std::bool_constant<std::is_same_v<std::remove_cv_t<T>,char>>{};template<classT>structis_string_like:std::bool_constant<std::is_same_v<std::decay_t<T>,std::string>||std::is_same_v<std::decay_t<T>,constchar*>||std::is_same_v<std::decay_t<T>,char*>||is_char_array<std::remove_reference_t<T>>::value>{};template<classT>inlineconstexprboolis_string_like_v=is_string_like<T>::value;// ModInt-like type: x.val() is printable, and x can be assigned from long long.template<classT,class=void>structhas_val_method:std::false_type{};template<classT>structhas_val_method<T,std::void_t<decltype(std::declval<constT&>().val())>>:std::true_type{};template<classT>inlineconstexprboolhas_val_method_v=has_val_method<T>::value;template<classT,class=void>structhas_static_mod_raw:std::false_type{};template<classT>structhas_static_mod_raw<T,std::void_t<decltype(T::mod()),decltype(T::raw(std::declval<uint32_t>()))>>:std::true_type{};template<classT>inlineconstexprboolhas_static_mod_raw_v=has_static_mod_raw<T>::value;// libstdc++ before GCC 16 does not classify __int128 as an integral type in// strict ISO modes such as -std=c++23. Keep the fast-I/O interface independent// of that implementation detail.template<classT>inlineconstexprboolis_integral_v=std::is_integral_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>||std::is_same_v<std::remove_cv_t<T>,__uint128_t>;template<classT>inlineconstexprboolis_signed_v=std::is_signed_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>;template<classT>structmake_unsigned{usingtype=std::make_unsigned_t<T>;};template<>structmake_unsigned<__int128_t>{usingtype=__uint128_t;};template<>structmake_unsigned<__uint128_t>{usingtype=__uint128_t;};template<classT>usingmake_unsigned_t=typenamemake_unsigned<std::remove_cv_t<T>>::type;}// namespace internalstructFastInput{staticconstexprintbuffer_size=1<<20;private:std::FILE*_stream;char_buffer[buffer_size];int_position;int_length;int_file_descriptor;bool_streaming;boolrefill(){_position=0;if(_streaming){ssize_tlength;do{length=::read(_file_descriptor,_buffer,buffer_size);}while(length<0&&errno==EINTR);if(length<=0){_length=0;returnfalse;}_length=int(length);}else{_length=int(std::fread(_buffer,1,buffer_size,_stream));}return_length!=0;}template<classT>boolread_integer_from_stream(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'){negative=true;c=read_char_raw();}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){result=negative?result*10-(c-'0'):result*10+(c-'0');c=read_char_raw();}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){result=result*10+T(c-'0');c=read_char_raw();}value=negative?T(0)-result:result;}returntrue;}boolprepare_number(){if(_length-_position>=64)returntrue;constintremaining=_length-_position;if(remaining>0)std::memmove(_buffer,_buffer+_position,remaining);constintadded=int(std::fread(_buffer+remaining,1,buffer_size-remaining,_stream));_position=0;_length=remaining+added;if(_length<buffer_size)_buffer[_length]='\0';return_length!=0;}public:explicitFastInput(std::FILE*stream=stdin):_stream(stream),_position(0),_length(0),_file_descriptor(::fileno(stream)),_streaming([&]{structstatstatus;return_file_descriptor>=0&&::fstat(_file_descriptor,&status)==0&&!S_ISREG(status.st_mode);}()){}FastInput(constFastInput&)=delete;FastInput&operator=(constFastInput&)=delete;intread_char_raw(){if(_position==_length&&!refill())returnEOF;return_buffer[_position++];}boolskip_spaces(){intc=read_char_raw();while(c!=EOF&&c<=' ')c=read_char_raw();if(c==EOF)returnfalse;--_position;returntrue;}boolread(char&value){if(!skip_spaces())returnfalse;value=char(read_char_raw());returntrue;}boolread(std::string&value){if(!skip_spaces())returnfalse;value.clear();while(true){constintbegin=_position;while(_position<_length&&static_cast<unsignedchar>(_buffer[_position])>' '){++_position;}value.append(_buffer+begin,_position-begin);if(_position<_length){++_position;returntrue;}if(!refill())returntrue;}}boolread(bool&value){intx;if(!read(x))returnfalse;value=x!=0;returntrue;}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>,bool>read(T&value){if(_streaming)returnread_integer_from_stream(value);if(!prepare_number())returnfalse;intc=static_cast<unsignedchar>(_buffer[_position++]);while(c<=' ')c=static_cast<unsignedchar>(_buffer[_position++]);boolnegative=false;if(c=='-'){negative=true;c=static_cast<unsignedchar>(_buffer[_position++]);}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){constintfirst=c-'0';constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=negative?result*100-(first*10+second):result*100+(first*10+second);++_position;}else{result=negative?result*10-first:result*10+first;}c=static_cast<unsignedchar>(_buffer[_position++]);}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){constunsignedfirst=unsigned(c-'0');constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=result*100+T(first*10+unsigned(second));++_position;}else{result=result*10+T(first);}c=static_cast<unsignedchar>(_buffer[_position++]);}value=negative?T(0)-result:result;}if(_position>_length)_position=_length;returntrue;}template<classT>std::enable_if_t<std::is_floating_point_v<T>,bool>read(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'||c=='+'){negative=c=='-';c=read_char_raw();}longdoubleresult=0;while('0'<=c&&c<='9'){result=result*10+(c-'0');c=read_char_raw();}if(c=='.'){longdoubleplace=0.1L;c=read_char_raw();while('0'<=c&&c<='9'){result+=(c-'0')*place;place*=0.1L;c=read_char_raw();}}if(c=='e'||c=='E'){c=read_char_raw();boolexponent_negative=false;if(c=='-'||c=='+'){exponent_negative=c=='-';c=read_char_raw();}intexponent=0;while('0'<=c&&c<='9'){exponent=exponent*10+(c-'0');c=read_char_raw();}longdoublescale=1;longdoublepower=10;while(exponent>0){if(exponent&1)scale*=power;power*=power;exponent>>=1;}result=exponent_negative?result/scale:result*scale;}value=static_cast<T>(negative?-result:result);returntrue;}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>,bool>read(T&value){longlongx;if(!read(x))returnfalse;ifconstexpr(internal::has_static_mod_raw_v<T>){if(x>=0&&uint64_t(x)<uint64_t(T::mod())){value=T::raw(uint32_t(x));}else{value=T(x);}}else{value=T(x);}returntrue;}template<classFirst,classSecond>boolread(std::pair<First,Second>&value){if(!read(value.first))returnfalse;returnread(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>,bool>read(Range&range){usingStoredValue=internal::range_stored_value_t<Range>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;for(auto&&value:range){ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){boolx;if(!read(x))returnfalse;value=x;}else{if(!read(value))returnfalse;}}returntrue;}template<classFirst,classSecond,class...Rest>boolread(First&first,Second&second,Rest&...rest){if(!read(first))returnfalse;returnread(second,rest...);}template<classT>FastInput&operator>>(T&value){if(!read(value))std::abort();return*this;}};structFastOutput{staticconstexprintbuffer_size=1<<20;private:inlinestaticconstautodigit_quads=[]{std::array<char,40000>result{};for(inti=0;i<10000;i++){intvalue=i;for(intj=3;j>=0;j--){result[4*i+j]=char('0'+value%10);value/=10;}}returnresult;}();std::FILE*_stream;char_buffer[buffer_size];int_position;int_precision;std::chars_format_float_format;char_range_separator;std::string*_capture=nullptr;template<classT>std::stringformat_cell(constT&value){std::stringresult;structCaptureGuard{std::string*⌖std::string*previous;~CaptureGuard(){target=previous;}}guard{_capture,_capture};_capture=&result;write(value);returnresult;}template<classMatrix>voidwrite_aligned_matrix(constMatrix&matrix){std::vector<std::vector<std::string>>rows;std::vector<std::size_t>widths;for(constauto&row:matrix){auto&cells=rows.emplace_back();std::size_tcolumn=0;for(constauto&value:row){cells.push_back(format_cell(value));if(column==widths.size())widths.push_back(0);widths[column]=std::max(widths[column],cells.back().size());++column;}}boolfirst=true;for(constauto&row:rows){if(!first)write_char('\n');first=false;for(std::size_tcolumn=0;column<row.size();++column){if(column!=0)write_char(_range_separator);for(std::size_tpadding=row[column].size();padding<widths[column];++padding){write_char(' ');}write(row[column]);}}}public:explicitFastOutput(std::FILE*stream=stdout):_stream(stream),_position(0),_precision(6),_float_format(std::chars_format::general),_range_separator(' '){if(_stream==stdout&&internal::standard_output_instance==nullptr){internal::standard_output_instance=this;}}FastOutput(constFastOutput&)=delete;FastOutput&operator=(constFastOutput&)=delete;~FastOutput(){flush();if(internal::standard_output_instance==this){internal::standard_output_instance=nullptr;}}voidflush(){if(_position!=0){std::fwrite(_buffer,1,_position,_stream);_position=0;}std::fflush(_stream);}voidwrite_char(charc){if(_capture!=nullptr){_capture->push_back(c);return;}if(_position==buffer_size)flush();_buffer[_position++]=c;}voidwrite(constchar*s){while(*s!='\0')write_char(*s++);}voidwrite(conststd::string&s){if(_capture!=nullptr){_capture->append(s);return;}std::size_tposition=0;while(position<s.size()){if(_position==buffer_size)flush();conststd::size_tcopied=std::min<std::size_t>(buffer_size-_position,s.size()-position);std::memcpy(_buffer+_position,s.data()+position,copied);_position+=int(copied);position+=copied;}}voidwrite(charc){write_char(c);}voidwrite(boolvalue){write_char(value?'1':'0');}template<classT>std::enable_if_t<std::is_floating_point_v<T>>write(Tvalue){chardigits[128];auto[end,error]=std::to_chars(digits,digits+sizeof(digits),value,_float_format,_precision);if(error!=std::errc())std::abort();for(constchar*pointer=digits;pointer!=end;pointer++){write_char(*pointer);}}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>>write(Tvalue){usingRaw=std::remove_cv_t<T>;usingUnsigned=internal::make_unsigned_t<Raw>;Unsignedmagnitude;ifconstexpr(internal::is_signed_v<Raw>){if(value<0){write_char('-');magnitude=Unsigned(0)-Unsigned(value);}else{magnitude=Unsigned(value);}}else{magnitude=value;}if(magnitude==0){write_char('0');return;}unsignedchunks[16];intcount=0;while(magnitude>=10000){constUnsignedquotient=magnitude/10000;chunks[count++]=unsigned(magnitude-quotient*10000);magnitude=quotient;}if(_capture==nullptr&&_position>buffer_size-64)flush();charcaptured[64];char*constbegin=_capture!=nullptr?captured:_buffer+_position;char*destination=begin;constunsignedleading=unsigned(magnitude);constchar*first=digit_quads.data()+4*leading;intskip=leading<10?3:leading<100?2:leading<1000?1:0;for(;skip<4;skip++)*destination++=first[skip];while(count--){constchar*digits=digit_quads.data()+4*chunks[count];std::memcpy(destination,digits,4);destination+=4;}if(_capture!=nullptr){_capture->append(begin,destination-begin);}else{_position+=int(destination-begin);}}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>>write(constT&value){write(value.val());}template<classFirst,classSecond>voidwrite(conststd::pair<First,Second>&value){write(value.first);write_char(' ');write(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>>write(constRange&range){usingStoredValue=internal::range_stored_value_t<constRange>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;boolfirst=true;for(constauto&value:range){if(!first)write_char(nested?'\n':_range_separator);first=false;ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){write(static_cast<bool>(value));}else{write(value);}}}template<classFirst,class...Rest>voidprint(constFirst&first,constRest&...rest){write(first);((write_char(' '),write(rest)),...);}voidprintln(){write_char('\n');}voidset_precision(intprecision){_precision=precision;}voidset_fixed(intprecision=6){_float_format=std::chars_format::fixed;_precision=precision;}voidset_general(intprecision=6){_float_format=std::chars_format::general;_precision=precision;}voidset_range_separator(charseparator){_range_separator=separator;}template<classMatrix>voidwrite_aligned(constMatrix&matrix){usingRow=internal::range_stored_value_t<constMatrix>;usingCell=internal::range_stored_value_t<constRow>;static_assert(internal::is_range_v<Row>&&!internal::is_string_like_v<Row>,"write_aligned requires a two-dimensional range");static_assert(!internal::is_range_v<Cell>||internal::is_string_like_v<Cell>,"write_aligned requires scalar cells");write_aligned_matrix(matrix);}template<classMatrix>voidprintln_aligned(constMatrix&matrix){write_aligned(matrix);write_char('\n');}template<class...Args>voidprintln(constArgs&...args){print(args...);write_char('\n');}template<classT>FastOutput&operator<<(constT&value){write(value);return*this;}};}// namespace utilities}// namespace m1une#line 8 "verify/utilities/dice.test.cpp"
#include<set>
#line 10 "verify/utilities/dice.test.cpp"
namespace{usingm1une::utilities::Dice;usingm1une::utilities::DiceDirection;usingm1une::utilities::DiceFace;voidtest_rolls(){Dice<int>original(1,6,5,2,3,4);Dice<int>north=original;north.roll_north();std::array<int,6>expected_north{2,5,1,6,3,4};assert(north.faces()==expected_north);north.roll_south();assert(north==original);Dice<int>east=original;east.roll_east();std::array<int,6>expected_east{4,3,5,2,1,6};assert(east.faces()==expected_east);east.roll_west();assert(east==original);Dice<int>cycle=original;for(intstep=0;step<4;++step)cycle.roll(DiceDirection::north);assert(cycle==original);for(intstep=0;step<4;++step)cycle.rotate_clockwise();assert(cycle==original);cycle.rotate_clockwise().rotate_counterclockwise();assert(cycle==original);}voidtest_orientations(){Dice<int>die(0,1,2,3,4,5);std::array<Dice<int>,24>orientations=die.orientations();std::set<std::array<int,6>>distinct;std::array<int,6>top_count{};for(constDice<int>&oriented:orientations){distinct.insert(oriented.faces());++top_count[static_cast<std::size_t>(oriented.top())];assert(oriented[DiceFace::bottom]==oriented.bottom());}assert(distinct.size()==24);for(intcount:top_count)assert(count==4);for(inttop=0;top<6;++top){for(intsouth=0;south<6;++south){boolare_opposite=(top==0&&south==1)||(top==1&&south==0)||(top==2&&south==3)||(top==3&&south==2)||(top==4&&south==5)||(top==5&&south==4);autooriented=die.orientation(top,south);assert(oriented.has_value()==(top!=south&&!are_opposite));if(oriented.has_value()){assert(oriented->top()==top);assert(oriented->south()==south);}}}}voidtest_equivalence(){Dice<std::string>first("T","B","N","S","E","W");Dice<std::string>rotated=first;rotated.roll_north().roll_east().rotate_clockwise();assert(first!=rotated);assert(first.equivalent(rotated));Dice<std::string>reflected("T","B","N","S","W","E");assert(!first.equivalent(reflected));Dice<int>repeated(1,1,2,2,3,3);assert(repeated.orientations().size()==24);assert(repeated.equivalent(repeated));}voidtest_opposites(){usingm1une::utilities::opposite;for(DiceFaceface:{DiceFace::top,DiceFace::bottom,DiceFace::north,DiceFace::south,DiceFace::east,DiceFace::west}){assert(opposite(opposite(face))==face);}}}// namespaceintmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;test_rolls();test_orientations();test_equivalence();test_opposites();std::array<int,6>input;for(int&value:input)fast_input>>value;Dice<int>die(input[0],input[5],input[4],input[1],input[2],input[3]);std::stringcommands;fast_input>>commands;for(charcommand:commands){if(command=='N')die.roll_north();if(command=='S')die.roll_south();if(command=='E')die.roll_east();if(command=='W')die.roll_west();}fast_output<<die.top()<<'\n';}