let input = [
"nwesesesesewswenwswsesesenesenwnesenwse",
"nwnenwnwnenenwnenwnenewnwenenwwnenesesenw",
"neneswnenwenwseeneweswsenesewnenenee",
"senwewnwnenenwnwnwwesenenwswnenwwnwnw",
"swseseeseswseseeswseneseswsesesenwsesew",
"weeneeneswsewnwnesweseneswenwneswne",
"swseseswswneswswsesewswswseswse",
"swswseeswswwswnweenewswswesenwswwse",
"swswswswsweswseeswseseseseeswwsewnw",
"eneeseenenweeneenenee",
"eesesenwsesweeseeese",
"neenenenewnenenenenenwnenenenwnwne",
"nenenwnwnwnenwnenwnwswnenesenenwnw",
"neneweweneneenenenenesewneeneenee",
"nwweswswewneenenwneneneeswneneneswne",
"eeseeneseesesesewneswseeeseese",
"swseswsenwswnewswseswswswseswswse",
"senenenwnwnenwnwnwewnwwnwswnenenwnwnwenw",
"senwnenenwnwnenwnwwnwswnwnwnenwnwenenwnw",
"neweseneswswnwswnwswseneseenwseeswee",
"esesweeneeneswsenwsweeeeseeseee",
"nenenwewseswseseswsewseneewwwnww",
"neeswswenwnewnwnwwswwwneswswnwwwnwnw",
"wwweswwwwwwswwwwww",
"eeseenweenwseneeeeeeweeenee",
"eeeeesenenenwesweeeswenwswseswee",
"neswenenesenenenewnwenesweneneeswne",
"swswswenwswwswswswswswwwswweswnwsww",
"seseswseseseeswneeeeesewesesenenw",
"swwswwwswwwswwswsweneswwwsesww",
"eneeswenweewenwseeeseeeseswwnw",
"swnenwswswswseseswswswwseswswswswswswsw",
"seeseseeseeeesesesenwsenwseweseese",
"swswswswnwnesweswewseseneswswwnenwsw",
"eewnenweneswwseeeeneneeeeeene",
"esenweswwnwnwnwnwnwnwnwnwnwnwnwnwwnw",
"seeeeeseeneeswweeeeeeneenw",
"weneswswenenenenwneswneswneneneesene",
"wnwsesesenwnwnenwnwnesweneenwseswwsw",
"sewsesesesesesesesewsenesesesesesenesese",
"swswswwnwswswwweswswswnewwseswsww",
"nwneneswnwnwnenenenwsenenwnwnenwwnene",
"neenwenenwsweseeswsesweeseseswneswene",
"eneeenewewneeneeneweneeesee",
"nwnwwwnwnewsenwsenw",
"sesesewswswwneneneeseeewswnwswnwsenw",
"sewwswwnwwewwwneswswswwwneew",
"nwsenwwnenenenesenwsenenenenenenenenenwne",
"sewsewnesenwsenesenwsesweswswsesenenw",
"eseeeeeeeenweeeeseesee",
"eseenwseesweswenweseenweeeeswee",
"neseseseswwneswsesesesewseseseswse",
"sesweewseseeesenwseeeseeswsweneenw",
"wnwneseeeseseeseenwwenwseseesese",
"enwneswnwneneneneneneneenenwnenwwnene",
"wnwneneneneneneewnwwnenweneesw",
"nwnenenenwnenenwenenwneneseswnewnenene",
"nwwsenwnwnenwnenwnenwneneneenwnwsenenww",
"wwwsewwnwwwnewwneswwewww",
"swseswwswseswswswswsenwseneeneewsenwsw",
"nwnesenenwenwnwnwnenwnwnwneswnwnwnenene",
"seeeweswnenenwsenewenenwewneseee",
"nwwnwneseswsesweenweswsese",
"seeseseswsesenesesesenesewseseseeese",
"swwswneswwnwswneswwewsesewswswsww",
"seswneswswseswswseseswnwswswswswseswew",
"wwwwwswwewseswwwwwswwnenesw",
"nwnwwsenwnenwnwnwnwneenwnwnwnwnenwnww",
"nwnwneswwswseswswnwnwenwnenesenenenwswenw",
"neneneneeneeseneneneeneneneswnwnenee",
"neeeswswnweenwsweseneeseswnwnewe",
"neswesenwneneneenenweeneene",
"swseeneewnwseeeenwesenweseseeswnw",
"eweneeneseeneneneneeeseeeneewene",
"eeneneewneneeeswneneeneeenwsenenew",
"nwnwswsweswswnenwswseswswswsweswswnesw",
"neeeeseeeswewenenwswnene",
"nwenwnwenwswnwnwwnwswnwnwneswnwneswswese",
"neswseweeneneeeseenwwnenesenenwnee",
"wswwseewwwewweewwswnewwwsww",
"swswswwswswwwswswswswswnweeswswswsw",
"enenenenenenenenenenwnenenwsenenenewnw",
"seseswseseseeswseneseseseseseneseesee",
"neewneeweeeeeneese",
"enewneseeweneneeneneewenesenene",
"enwswneeswnwswsewenwwnesewneswseswe",
"senwswnwnenwnwwnenwnwswnwnwnwnwnwnwnwnwe",
"sewswneswswswseseseseswswseneswseswswsw",
"nwnenwwsewneneswnweenwnwnenwnwnwsenene",
"eswwewswswnwswsw",
"wwwwwwwwswsenw",
"nwnwnwnwnwnwnwwwnwnwwnwnwwnwnenwsese",
"seswnewnenwnweswnwsesenwseeseesesewnw",
"neneseneeswneneneswwsenwnw",
"nwnwnwnewnwnesenwnwnenwnwswnwneseenwnw",
"wwneneeneswneneeewwnesesenenenese",
"eseeswswsesenwneeewswnenwnwnewnw",
"nwnesesenewwwswnwewsenwwsewnwwww",
"eneeenewneneeneneneneswnenwewnesee",
"neneenewenenenenew",
"nwsenweewnwwwwenwnwswnwnenwswnwnwse",
"seseeeeeeswwsenwseeseeseseeese",
"wwwnwwwwwwwewewwwwwww",
"swswseseneseswswsewseseseswenwneseseswsw",
"seswsesesenweseeseswwseseneswsesesesese",
"swswseswswswswswswswswswnwswswseswsw",
"nweeneewneeseseesenwsenwseweswnwnw",
"eeeeeseseeeeewsweenweeeese",
"nwesesesesenenewwwneeeeweeee",
"eenenwneneeswewneeeeneenee",
"seseseswseswseswsenwseeswsesenwseseswne",
"eseseseswseeneseeseseewnwswsesenese",
"nwnenwnwseewsenwenewsenwsweswswenenee",
"wsenwnwwnwwnwneeenwnwne",
"seswswswswseneneseseswswswswswswswwnesww",
"wswseswnwsweswseseswesesenwswseseseswsw",
"sewweseseneseneswsesesenesesesesesese",
"nwwnwneewwewwwwnwwwwwswwwswsw",
"nwsweswwneeeeeenwseeenwnwswswesw",
"wwwwwwsenwwnewwnwwwenwwwew",
"swneneneswneneswenenwnwnewnwnwsenenenwnene",
"eswnweseweseeseenwsene",
"esewewneneneneseneneneneneneneneewwne",
"eneeeenesenwnenwseneneenenenweesw",
"nwnwnwseenwnweswnwnwnwnenewnwnwnwswenw",
"neenenesewsewneeswseseenwweeeesw",
"eewneeeneeesweeeeeeenenee",
"nenenenenenwwsenenenenenenenwswneneneene",
"nwwwwswwwnewwwwwwnwwww",
"enwswseswenesenwenwseseeswesesenewse",
"swesweneeenwenenweeneneesweeee",
"wnwnwnesenwnwwsenewswnwwnwsenwseneswse",
"neseswseneenwsweneswwnwsenwnesewsenwsw",
"swswswseseneswweenwswswsesenewseswnesenw",
"weseseeseseswseseseneeeesesewnese",
"seeneswnewsesewnwwwwnw",
"sewseeseeeeesewneeese",
"seseseenwewsesewneseeeeesweenw",
"ewswwwswswwswwwswswswsw",
"nwnwwwsenewswnwwwenwsenwnenwnwnwnw",
"esenwseseweeneneneswwsewsesewneese",
"wnwnwswnenwnewwenenesewnenenwnesenesene",
"wnwnenwwnwnwnwnwnwnwswnwnwnwnwwnenwnwse",
"eewnesenwsesesweeneeewesweeesee",
"swenwesweseenwseeseseseenenwesee",
"nwnwneswnwenwwnwnwnenenwnwnweneswsenw",
"swwwewwnewseewswwswnewwwww",
"swwwswewwwsewwsewnwwswwwwnww",
"wsenwewnwwsewwwnwsewnwnwwsenwnwnw",
"neswswnenenwneneneenewneneneswnwsw",
"wwwswwwwnwewwwsewwwwwwwnw",
"seseseswseswswnweswwswswswnwsesesesesee",
"nwneneenesenenenwwnwneneswnwnwnenenenwnw",
"neeenwneeneesweenweeeesw",
"eweeeeeeeeeeeeneswneeeswe",
"wseewesesesesesesesesewseenesesesesee",
"eeeeeseeeeweeee",
"wsewneseeewseswnewnenenwnenewnesenw",
"wswenwnwnwnwwnwnwnwwewnwwnwwnww",
"wwwwwwnewwwsewwwswwneseww",
"wwwwewwwwwwnw",
"nwnwnenenwnwnwseenwwsenwenwnwwwnw",
"seeswswsewnewnwwsweswwswnwswswnwnw",
"eweseseneeseese",
"sweeeeeswenenesweseeeeseneee",
"wnwswewnenewsewwnewwwswwsww",
"nweenwwwneswnwsenwsewewnwwnwnwww",
"eswneeneneneneeeneeneeneneeeswne",
"eeseseneeeeeeeweeneeeswee",
"enweeseneswnenwnwnwswswswnw",
"swseseseseseeseeneeese",
"swswswswswswswswwseswswswswswseeneswnwsesw",
"senwseenwwsweswseseswse",
"wnwwnwwnwnwnwwsenwnwnwnewnwnwnwsenwnwse",
"seseenwesesenwseseseseseseseseseseswswsw",
"nesewnenenwnwneneswneneswsenwnenwnenw",
"eswnwweenweseeneeswneeeeeee",
"seesweneewenenweswseseweseneswsenwse",
"wsesesesenesesesesesesesesesesese",
"neneneneenenenenenewneneneneneneswne",
"eseseeseeseeenweeeswseesenwse",
"neneseeeeweseewwseseeenweseee",
"senewnwwwswwewnwwnwwsewenewse",
"seseseeweseseseneseeeseweseseseee",
"wswseswseenwwswneswswnwswsww",
"nwnwnwnwwswnwnwnwnwenenwenwnwnwnwne",
"sewnwsenweswswswneenwwsenewnwnewnwnw",
"swseeseseswseneswwsesewwwswnenesesese",
"eeeneneesweeeeeeneee",
"nwnweswnwnwenwnwnwnwnwswnwsenwnwnwsenw",
"swswswswnwenwswswswswweswswesw",
"nenwnwneswnwneswnenwnenenenwnwnenenenene",
"wneneneneneneneneseswneneesenewnenwe",
"enwsenenweneeswswsesesweseseseseswsese",
"swwseseseeseewnesewnewswseseswseswse",
"enwneeneneswneneneenenenene",
"eswweeeeseeneeeeeesesenweee",
"nenwsenwnwnenwneswnwnwnwnwenwnwnenwnenw",
"esenwswwnwnwenwsenwnwseseenwswnwwew",
"nwswnewwwnwswnwwnenwnenwswnwwwwnwnw",
"eeweseeseseeeeeeesesesewese",
"nwseeeeenwseeseeeeseeeseeeew",
"senenwswnweswnwwwwwnwnenwwseswwnwe",
"ewswnwnewewsenwswseneswswswswseswsw",
"nwseswnenwwenwwswsesenwnwneewwnwse",
"seeneseseneweseseseeseseswseseseese",
"wwsewwswswwswswneswweswswswswwsww",
"swswswsweswswswswswnwwswswsesesweswsw",
"seeeeweweeweeeseneenewene",
"nwseseesewseesewnwneewseesesenenwee",
"swewwnwnwswswwwweswswswswswneswe",
"eeneeeneneneneeweneneenesenenenew",
"swsewwwsewnewwwnwwwwwwnewww",
"seneswwweswswswwsweswswswswswwswswwnw",
"seneseseseswsesesewsesesesesw",
"seswswseswswwewswswswswseswswswswnenw",
"eseseseesesenenweseesweseeewseseese",
"swesenenwswnesesenwwwnwse",
"nenewswnenenenenenesenenenenenenenenesw",
"senwneseneeneenenw",
"wseseseeseseeseseseeseseseeenesewe",
"neeeneweenenee",
"nwsenenwnenwneeneeeneneneneeswnesene",
"nwswseeneseenwswnweseneswswnweesesese",
"nwseseseswsesesesewseeesesesese",
"eswenesewnenwnwwwnwnwnwneswesenwswsene",
"sewswwswwswswwswwswswwwneneswnwsww",
"nenwswenenenenesesesenwwneswnenenewew",
"senenwswseswsewwsewseseseneeswneswswsw",
"nwwwnwswswseswseswswwnwweswwwew",
"eswswswswseseseswswseswswnwswsweswwswse",
"nenesenenewnenenwnenenenesenenenenenenesw",
"wnwnwnwwwwwewwwswwwnewnwsenwsw",
"enwnewnwneswewnewwswwneeseswesew",
"nwnwnenenwsenwewnenwnenenenwnenwnwnwnwsw",
"nesenewneenwnwnwnwnwneneneswneswnewnee",
"ewenewswwsewenwwsenenwwswnwsenwnw",
"nesenwsenwseseeswswnwese",
"wnwsenwnwsenwnwswwnwwnenwnwseswnwnwne",
"newnenwneneenwesenesenenwseseweswswe",
"senwsesesenwsweseswswsenwnesesesww",
"sweswseswswwseseseswswsesesenwneseseswnw",
"nwwenwnwnwsenwnweswnwswnwwswnwnwnenw",
"enesenenwsewesewsweeneeeeweeee",
"nwnwnwnwwnwnwnwnwnwwwewnwenwnwnwnw",
"wseswseseswneeseeseenwseenwseswnwse",
"seesenwnwwwewseswswnwnwnwe",
"sewwwwwwwweswwswwwwnewww",
"neneeswnenwneneswsenweneswneseswseeww",
"nwnwswenwnwnwnwnwneseswnwsweneswenwnwsw",
"wwwswwneswwwwwnewswww",
"senwnwnenwenenenenwnwnewswnwnwnwesw",
"wswwwwnwswwswwewswnweswswwswew",
"swseseseswseseswswseseswsesesesenesenwse",
"nwwnenenenwswsweneenenenenesweneeene",
"wwnwswwswwswewwwsewwwswswswe",
"ewwnwwnenesweseenwswswseeswwneenww",
"eseswsesenwwnwseseseseseseseseswesesesw",
"wwwwwsewwwwnenwsewnwnenwwwww",
"nenenenwneneeneswnenenenenwwnenwneenw",
"seeeeeeeswwseeeeeweneeeenw",
"senweeneeneswwneeneesweeeswenenesw",
"nwwnenwnwnwenwnenwnwnwswswnwnwnwenwse",
"sesenwseseseeseseseseseseese",
"swswneswsewseseswseswseswseswseeswsewse",
"seseeseseseeeesesesesewee",
"seneneeswnenenenenenenenwsenwnenenwnenenew",
"eeeneeneneweeewneeneneneeseene",
"swneneneneeneneneenenenenwneenewnene",
"seswnwseswnwnewneswswnesenewswwwswswsw",
"enweseeweewewesweeenw",
"wwwswewneswwwsesewwwnwwswswww",
"nwwwswwneswwsewswse",
"swwneseswswnewwswnewwwwse",
"nwwnwwwnwewwnwswwwwnwwnwwewnw",
"seseseswneseswseeseswswseswseswwesesenwsw",
"nweseseseseseseswewseseswsesesesesesese",
"seseswesesenwseenwsenwseseseseseseseswse",
"swsenwwnwnwnwnwneswewnenwnweweeswne",
"eeeenweeeweeeeeeeeesee",
"nwwwwswnewswewwenwnwwwewswwnw",
"nwnwnwnwwnwswenwnwnwnwnwnwnwnwnwnwnw",
"nwwsewewnesewswewnwswwnwwneewse",
"wnwnwwnwwwnwwnwnewwswwswwwne",
"enenesweeeeeeneenweeneeneeesw",
"neeeenweneeneneneneeeeeswsweee",
"sweswwewseswwwseneneswsewnwwsww",
"neesesenweweseneseeesesewseeseenwe",
"sweeeneeeswnene",
"nwwwswwnwseeweswwwnw",
"ewswswswwwwneswswnwswswwswswswwww",
"neewseenwneeswseeneweneweenwesw",
"seneweseeseseseewseseswweeeese",
"eneswswswnwswwwswswswswswswswswneswsw",
"sewswswswswnwswseswswswswnewswwwsww",
"wwsesenenwnewwwsenw",
"swnewweswwenenwneseenenenenenenewne",
"nwwnwnwnwswnwnenwwwnwnwnw",
"sesesesenwseswnwseseseseseeseswswswswse",
"swnwswenenwswswneweswwsewsw",
"nwnwnwnwnenenewewnenenenesenwnwswnwnw",
"seseswsewswswsenwseseesesenenwesenesww",
"neneeneeseeeewwwneeenweeeswe",
"enenwewwswswsenewswsenwewseeneenee",
"nwwenwswwwnwwnwnwwnwwwwewenwww",
"sweswnwswesenwsweswseswswnwnwswsweswnwsw",
"seseseneseseweseewseseswsesewsesese",
"eweneeneeeeeeseeeeeeeeesw",
"ewwwnwwwwswwwswswwwwwswwnesw",
"swnwnenwnwnwwnwnewnwswnwenwnwnwsenwnw",
"swneeswseseneswwnesesenwsesesenwswnww",
"seswneseseesesewseseene",
"wnwwsewnenwnwwsesesenwnwsesesewwwne",
"eswswwwswseswewwswwswnwswswwwnw",
"enwnwnenenwnwnwswnwnenwnenwnwswnwnw",
"newsesenwnenenwnwenwnewnwwnwnwswnwnwnwnw",
"swneneenesenwwsenwnewnesesenenenwnenw",
"neneenwnwswswswweeeeeeeenenee",
"swswseswnwswswswenwswsesenwswseswnewswse",
"newnenwnenewsenewnesenewneesenwnene",
"neseseseseswsesewseswneseseswsesesesese",
"ewseeeeseeesesesesenwseeeswse",
"wseeeseeeseseesewseenwswseneeee",
"neseneseswswsesenesewswsesenewsesesenwse",
"swneneneneneenwneeswneneneeneneneswne",
"eenewnenenesweeenenenenene",
"nenenesenenenwneeneeneewseeeene",
"nwsenenwnenewnwnwnwnwnwnenwnenwnwsenenenw",
"swnwenwnwwnwnwnwswswewnwnwnwnenwnwnwnww",
"wwswnwwewwwwsewnwwwww",
"wseseswnenewwwwwswwwsw",
"swswwswswwnewswwwseeswwwswwswsw",
"enesesewewsesweeeseeseseseseesese",
"nwwnewenwnenwnwnweneswnwneswnenwneenw",
"eweeeeeseeeweweneeeesesese",
"wswswswswswswnwseswneswswswswwwswsww",
"swsenwnwwnwseseseswweeneenwnesenwnee",
"neeswneneneneewneneneneneneneneneene",
"nwnewnwnwnwwnwnenwwswnwnwnwwsenwsenwnenw",
"nwnwnwnwnwnwnwnwsenwnwnwnw",
"nwnewnwswwwswneewsewnewwswwwww",
"eeswwesesesenwseeeeeeseeenwe",
"nenwneswswenwsweneeswneneneneneeswnenw",
"neneseeneneesewneswnenenwnw",
"nwnwwwenwenwnwnwwnwnwnwnwwnwswnwnwnw",
"seseseswswseswsenwswnenwseswwweseswnese",
"wwewwwseswwswsenwwnwweswnwnee",
"neenwnwseeneewwneneenenenesewseenese",
"nwwswswenesewwwwswwswenwneswnewse",
"seswwnenwnwnenwwneeswsewewsewsesw",
"eseeseseeeesweneee",
"eenesesweeeeenwswnwneenenwswnenene",
"seneseseeeseeswswseswsenwsenwnwsesesese",
"seseseseseeeeseseeenw",
"wnwwwweewwwwwswnewwswwww",
"swswnewnwswseswswswswswwwswswswswwsw",
"nwnwenwnwnwnwewnwnwnwnwnwnwnwswnwnwnw",
"seswswswseseseswseswswnwneseneswswseswne",
"swseswseswswswsewseswsesenenwsesenwseseese",
"swswsweseswneneswwnewswswswswswswswnew",
"nwwnwnwnwswnwnwnwnwsenwnwneeeswnwnwnenw",
"wneneneneseneneew",
"weeeseweneenewseesesewesesesese",
"swnweswswseeseswswswseswswswswnwseswew",
"eneseenweeeswneeeenweswneeee",
"neneenewneweneneeneseneneswneenenwnene",
"nesenewneneneeseswneneeneenenwenewnw",
"neneneweeneneewneeneeneneneene",
"senenewnesewwwwswswneswwneswsenwse",
"eenweneseeswnenweswnwsee",
"nwnwnwsenwnwnwnenwneneneswnwew",
"sweswneseenwesweeswnwewseneneneeenw",
"swnwneswwswseswswswswseswseseswswsesw",
"nwnwnwnwnewnwnwnwsewewsenw",
"swseseseseswseswswswsenwse",
"nenwnwesenwnwsenwwnwsenwneswneeneswnw",
"ewwwewnwwsewnw",
"nwwnwwnwnwswwnwnwnwnwwnenw",
"wneeneneneenwswswwneneeneneenesene",
"nenwnenwnenwnwnenwnwneesenwnenenenwwnew",
"eenewnenwswwseeenwsenwweneneswne",
"nwnwnenenwnwwnwnwnwnwswnwswsenw",
"eeseneenenenenwwseswneneewneenenenee",
"nenwnenewnwswnenewnwseswneenwnenesene",
"wnwswwnwwenwenwnwwnwswnwnwnwnwnwnww",
"neeneneenesewneseenenewnwenwswenese",
"nwewneeswnwnwseseneswneneswnenwswnwnw",
"nwnesesewseswsewsewnenenesesenewsesese",
"seneseswswswswswsenwseseseswseswswswsee",
"nwwswnwsewwwnewsewnwewesewnwnwnw",
"eswwenwnenwnwnenwswnwnwnwnwsenenwwne",
"senweesenwwsewseeneeeenesewseee",
"nenwnenwnwnwswnwswnenwnenweeswnenwnenene",
"wnwnwwewsewnewwswwwnwnwwnwwwww",
"nwwnwswnwnwwnwnenwnwswseewnwnwnwnwe",
"nenwnenenenwnwnwnenwnenenweenenenwwnesw",
"wnwnenweseneswwswnwneeseswnenenwswwe",
"eeeweeeeeeweeeseeee",
"wwwswwnwwnweswweneswnenwwwnwww",
"swswwswswseswswswsweseswswseneswswse",
"seswnwewswwwwswwsewnwneswswewww",
"seswwwwnewwnwwewwnewwwwww",
"seneswnenwsweewnwnwenwswswswnesenew",
"eswnweeesweeseneeeeeeeeeee",
"wnwswswswswwswswswswwswwwewewsww",
"nwnwnenwnwnweeswsenweesewswswnwnwswnw",
"seswwnwsewwwwswsenenw",
"wwswwwwswswseswwwwewswwswnww",
"seswseseswseseswseseseswswseseswswnw",
"swnesewwnwwneswne",
"wswswsesweswswswseswwswswsweswswnwnwe",
"seenwsenweseseseesesewseseseseesese",
"esenwnwnwneswnwnenwwsenwnenwwsenenww",
"eeeeeneseswseseeseenwseeesw",
"swseseeneseneseeswwnwese",
"eeeenweswseeeesee",
"seseswweenwswnewwwwnew",
"wswswswnwswswwswswswseneneswseseseeswse",
"nwswwnwsewewswwswwwenenwwnwww",
"seneseweseseeneesesesesesenweseseswse",
"nwnenwnwnwnwsewwenwnenwsenesenwnwnenwne",
"senwenewsesesewnwwseeweseswsesesenwe",
"wenwewnwnwnwwnwewnwwwnwwwwnw",
"seeeeseseeseseesenwseenweesesese",
"swswswwnwswwwwswnewswswwwswswwew",
"nenwnenenwnwnwsenenwneneneswnwnwnwsesene",
"wnewswsenesewswwwswnwwswswnewwseew",
"wsesenwenwseswsenwwseeseenesenenwwnw",
"senewewswwswwewwwwnewswwwswsw",
"swneenwseweseeenwweseseeesenwnwse"];
// input = [
// "sesenwnenenewseeswwswswwnenewsewsw",
// "neeenesenwnwwswnenewnwwsewnenwseswesw",
// "seswneswswsenwwnwse",
// "nwnwneseeswswnenewneswwnewseswneseene",
// "swweswneswnenwsewnwneneseenw",
// "eesenwseswswnenwswnwnwsewwnwsene",
// "sewnenenenesenwsewnenwwwse",
// "wenwwweseeeweswwwnwwe",
// "wsweesenenewnwwnwsenewsenwwsesesenwne",
// "neeswseenwwswnwswswnw",
// "nenwswwsewswnenenewsenwsenwnesesenew",
// "enewnwewneswsewnwswenweswnenwsenwsw",
// "sweneswneswneneenwnewenewwneswswnese",
// "swwesenesewenwneswnwwneseswwne",
// "enesenwswwswneneswsenwnewswseenwsese",
// "wnwnesenesenenwwnenwsewesewsesesew",
// "nenewswnwewswnenesenwnesewesw",
// "eneswnwswnwsenenwnwnwwseeswneewsenese",
// "neswnwewnwnwseenwseesewsenwsweewe",
// "wseweeenwnesenwwwswnew"];
/*
^
/ \
/ \
| |
| |
| |
| |
\ /
\ /
v
*/
let tilesPositions = [];
let count = 0;
const size = 136;
for (let stepsStr of input) {
let x = 0;
let y = 0;
let prev = '';
let dir = '';
let dirs = [];
for (let ch of stepsStr) {
if (prev == '') {
if (ch == 'e') {
dir = 'e';
} else if (ch == 'w') {
dir = 'w';
} else {
prev = ch;
}
} else {
if (prev == 's' && ch == 'e') {
dir = 'se';
} else if (prev == 's' && ch == 'w') {
dir = 'sw';
} else if (prev == 'n' && ch == 'e') {
dir = 'ne';
} else if (prev == 'n' && ch == 'w') {
dir = 'nw';
} else {
console.error('unknown direction', prev, ch);
}
prev = '';
}
if (dir != '') {
dirs.push(dir);
prev = '';
dir = '';
}
}
for (let dir of dirs) {
let s = y % 2 == 0 ? 0 : 1;
switch (dir) {
case 'e' : ++x; break;
case 'w' : --x; break;
case 'ne': ++y; break;
case 'sw': --y; break;
case 'nw': ++y, --x; break;
case 'se': --y, ++x; break;
}
}
let xy = (size / 2 + x) * size + y + size / 2;
if (tilesPositions.includes(xy)) {
//console.log('reverting', xy);
--count;
let idx = tilesPositions.indexOf(xy);
tilesPositions.splice(idx, 1);
} else {
//console.log('flipping', xy);
tilesPositions.push(xy);
++count;
}
}
let grid = new Uint8Array(size ** 2);
for (let pos of tilesPositions) {
grid[pos] = 1;
// 1 is black, 0 is white
}
let grid2 = new Uint8Array(size ** 2);
function next(arr, res) {
for (let x = 1; x < size-1; ++x) {
for (let y = 1; y < size-1; ++y) {
let cur = arr[x * size + y];
let neighbours = arr[(x+1) * size + y] + arr[(x-1) * size + y] + arr[x * size + y+1] + arr[x * size + y-1] + arr[(x-1) * size + y+1] + arr[(x+1) * size + y-1];
if (cur == 1) {
// black tile
if (neighbours > 2 || neighbours == 0) {
res[x * size + y] = 0;
} else {
res[x * size + y] = 1;
}
} else {
// white tile
if (neighbours == 2) {
res[x * size + y] = 1;
} else {
res[x * size + y] = 0;
}
}
}
}
}
for (let i = 0; i < 50; ++i) {
next(grid, grid2);
next(grid2, grid);
}
let final = 0;
for (let x = 1; x < size - 1; ++x) {
for (let y = 1; y < size - 1; ++y) {
if (grid[x * size + y]) {
final++;
}
}
}
console.log(count);
console.log(final);