Score: 8066 8065 walls
I found an 8066 wall solution using dynamic programming over solutions that can be recursively decomposed by splitting on a row or column consisting entirely of floors or entirely of walls.  The implementation of this apparently simple idea was rather more complicated than its description, since there are 17 different types of subproblems depending on the borders and the connectivity between them.
I then removed one wall by hand-editing seven cells in rows 119–122, columns 60–63.
This is an optimal solution.  Proof:
Add a one-cell border using \$516\$ extra walls. Consider the planar graph whose vertices are the \$w\$ centers of walls, and whose edges are the \$a\$ wall-wall adjacencies. This graph is connected (because any component of walls not connected to the border would be encircled by a forbidden cycle of floors), and has \$f \ge 2\$ faces (at least an inside and outside). By Euler’s formula, \$w - a + f = 2\$, so \$w - a \le 0\$.
Now consider the different planar graph whose vertices are the \$131^2\$ corner points of walls, and whose edges are the \$4w - a\$ sides of walls. None of these \$131^2\$ vertices are missing (because an isolated one would again be encircled by a forbidden cycle of floors). This graph is again connected and has \$f' \ge w + 2\$ faces. By Euler’s formula, \$131^2 - (4w - a) + f' = 2\$, so \$131^2 - 3w + a \le 0\$.
Adding these two inequalities gives \$131^2 - 2w \le 0\$, so \$w \ge \frac{131^2}{2} = 8064.5 + 516\$.
Validate
.#.#.#.#...#...#.#..#..#.#.#.#...#.#.#.#.#.#.#.#................................................................................
.#.#.#.##.##.###...##.##.#.#.###.....#.#.#.#...#.##########.###########.########.##################.#################.#.#.#.#.#.
.#.#.#.#...#...#.#..#..#.#.#...#.##.##.#.#.#.###...#.#.#.#..#.#.#.#.#.###.#.#..#.#.#.#.#.#.#.#.#.#..#.#.#.#..#.#.#.#..##########
.#.#.#.##.###.##.####.##.#.#.###.#...#.#.#.#.#...###.#.#.####.....#.#.#.#.#.##.###.#.#.#.#.#.#.#.####.#.#.#.##.#.#.####.#.#.#.#.
.#.#.#.#...#...#....#..#.#.#.#...#####.#.#.#.###...#.#.#.#..##.####.............................................................
.#.#.#.##.###.##.####.##.#.#.###.....#.#.#.#.#.#.###.#.#.#.##.....####.####################################################.###.
.#.#.#.#...#...#....#..#.#.#.#...#####.#.#.#.#.....#.#.#.#..###.###.#...#..#.#.#.#.#.#.#.#.#.#.#.#..#.#.#.#.#.#.#.#.#.#.#.###.#.
.#.#.#.##.###.##.####.##.#.#.###.#.#.#.#.#.#.###.###.#.#.#.##.#.#.#.##.###.#.#.#.#.#.#.#.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.#.##
................................................................................................................................
##.##.###.#############.#############################################################.#######################################.##
.###...#...#.....#.#..#..#..#.#.#.#.#..#.#.#.#..#..#..#....#..#.#.#..#.#..#.#..#.#.#..#.#.#...#.#..#.#.#.#.#.#.#................
.#.##.##.#.####.##.#.######.#.#.#.#.##.#.#.#.##.#.##.####.##.##.#...##.##.#.##.#.#.####.#.##.##.##.#.#.#.#.....##########.######
.#.#...#####.....#.#..#..#..#.#.#...#..#.#.#.#..#..#..#....#..#.#.#..#.#..#.#..#.#.#..#.#.#...#.#..#.#.#.###.###................
.#.##.##.#.####.##.#.##.###.#.#.##.###.#.#.#.##.#.##.####.##.##.#.####.##.#.##.#.#.##.#.#.##.##.##.#.#.#.#...#.####.############
.#.#...#...#..#..#.#..#..#..#.#.#...#..#.#.#.#..#..#..#....#..#.#....#.#..#.#..#.#.#..#.#.#...#.#..#.#.#.###...#..###...........
.#.##.###.###...##.#.##.###.#.#.##.###.#.#.#.##.#.##.###.###.##.###.##.##.#.##.#.#.##.#.#.##.##.##.#.#.#.#.#.###.##.#.##########
.#.#...#...#..#..#.#..#..#..#.#.#...#..#.#.#.#..#..#..#..#.#....................................................................
.#.##.###.#####.##.#.##.###.#.#.##.###.#.#.#.##.#.##.###...######.#############.#####.#################.########################
.#.#...#...#.....#.#..#..#..#.#.#...#..#.#.#.#..#..#..#..###.#.#...#..#...#.#.###..#..#.#...#.#.#...............................
.#.##.###.#####.##.#.##.###.#.#.##.###.#.#.#.##.#.##.###.#.#.#.##.###.##.##.#.#.##.####.##.##.#.################################
................................................................................................................................
#.#######.##################.######################.#.####.###################.#####################.##.####.##################.
#.......#.#.#.#.#.#.#.#.#.......#.............#.#.#.#.#.#...#.#.#..#.#..#.#..###.#.#.#.#.#.#.#.#.#.#.#..#.#....#.#.#.#.#.#.#..##
..####.####...#.#.#.#.#.####################.##.#.#.#...##.##.#.##.#.#.##.#.##.#.#.#.#.#.#.#.#.#.#.######.###.##.#.#.#.#.#.##.#.
#.#.....#.#.###.#.#.#.#.#.......#.............#.#.#.#.#.#...#...................................................................
..#######.#...#.#.#.#.#.#####.#####.###########.#.############.#####################.##.##.#.#.#.##.##.######.##################
#.......#.#.###.#.#.#.#.#.......#...#.#.#.#.#.#.#.#...#.#.#.#..#.#.#.#..#.#.#.#.#..#..#..#.#.#.#.#..#..#........................
..#######.#...#.#.#.#.#.######.####...........#.#.#.###.....####.#.#.#.##.#.#.#.##.#############################################
#.......#.#.###.#.#.#.#.#.......#.#.###########.#.#...###.###..#.#.#.#..#.#.#.#....#.#.#.#.#.#.#.#..#..#..#.#...................
..#######.#.#.#.#.#.#.#.######.##.............#.#.#.###.#.#.#.##.#.#.#.##.#.#.###.##.#.#.#.#.#.#.##.##.#.##.######.#############
#.#.#.#.#.#.#.#.#.#.#.#.#.......####.##########.................................................................................
........#.#...#.#.#.#.#.#####.###.#..#.#.#....#######.#####.#############################.#.#.#############.#############.######
#.##.####.##.##.#.#.#.#.#.#.#.#.#.##.#.#.###.##.#.#.###.#.###.#.#.#.#.#...#.#.#.#.#.#.#.#####...#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.
..#.....#.#...#.#.#.#.#.#.......#.#..#...#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#..#..#.#...............................
#.#######.##.##.#.#.#.#.######.##.##.##.##.#.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.##.##################################
................................................................................................................................
########.#############.##################.#.##.##.#######.#.#.###########.######################################################
.....#......#.#..#..#..#...#.#.#.#..#...#.#.#..#..#.#...#######.#...#..#...#.#...#.#...#.#..#.#.#..#.....#.#.#.#................
###.####.##.#.#.###.##.#.###.#.#.#.##.#############.#.###.#.#.#.#.###.##.#.#.#.###.###.#.##.#.#.#.######.........###############
.....#...#..#.#..#..#..#...#.#.#.#..#...#.........#.#...#...#.#.#...#..#####.#...#.#.#.#..#.#.#.#..#...#.#.#.#.#................
###.####.####.#.###.##.#.###.#.#.#.##.#########.###.#.####.##.#.#.###.##...#.#.###...#.#.##.#.#.#.####.#.#######################
.....#......#.#..#..#..#...#.#.#.#..#...#.#.#.#.#.#.#...#...#.#.#...#..#.###.#...#.###.#.#..#.#.#..#.#..........................
##.#.###.####.#.###.##.#.###.#.#.#.##.###.........#.#.####.##.#.#.#.#.##...#.##.##.#...#.##.#.#.#.##...#.#################.#####
.#.###...#..#.#..#..#..#...#.#.#.#..#...#####.#####.#...#...#.#.#.###..#.###.#...#.###.#.#..#.#.#..#####...#....................
.....###...##.#.###.##.#.###.#.#.#.##.###.#.#.....#.#.####.##.#.#...#.##.#.#.##.##.#...#.##.#.#.#.##.....###.#.##############.##
##.###...#..#.#..#..#..#.#.#.#.#.#..#.#.#.....#.###.#...#...#.#.#.###..#...#.#...#.###.#.#..#.#.#..#####.#.#.#.#................
.#.#.###.####.#.###.####.#.#.#.#.#.##...#####.#...#.##.###.##.#.#...#.###.##.##.##.#.#.#.##.#.#.#.##.#.#.#.#####################
.....#...#..#.#..#..#..#...#.#.#.#..#.###..#..#####.#...#...#.#.#.###...........................................................
###.####.#.##.#.###.#.###.##.#.#.#.##.#.##.##.#.#.#.##.###.##.#.#.#.##########################################################.#
................................................................................................................................
#.###.###.################################.####################.################.###########.#######.###################.##.####
....#..#..#.#..#.#.#.#.#.#..#.#.#.#.#.#.#...#.#.#..#.#.#.#.#.#..#.#.#.#..#.#.#.###.#.#..#.#..#.#.#.###.#.#.#.#.#.#.#.#.#.#......
#.#########.##.#.#.#.#.#.#.##.#...#.#.#.#.#.#.#.#.##.#.#.#.#.####.#.#.#.##.#.#.#.#.#.##.#.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#########
....#..#..#.#..#.#.#.#.#.#..#.#.###.#.#.#####...................................................................................
#.####.#.##.##.#.#.#.#.#.#.##.#...#.#.#.#...##########.######.##################################.########.#.##############.#####
....#..#..#.#..#.#.#.#.#.#..#.#.###.#.#.#.###.#.#..#.###.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#..#.#.#.###.#.#.#..###.#.#...............
#.#.##.#.##.##.#.#.#.#.#.#.##.#...#.#.#.#.#.#.#.##.#.#.#.#.####.#.#.#.#.#.#.#.#.#...#.##.#.#.#.#.#.#.#.####.#.#.#########.######
..###..#..#.#..#.#.#.#.#.#..#.#.###.#.#.#...#.#.#..#.#.#.#.#..#.#.#.#.#.#.#.#.#.#.###.#..#......................................
#.#.##.#.##.##.#.#.#.#.#.#.##.#.#.#.#.#.##.##.#.##.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.##.#######################################
................................................................................................................................
###.#################################.##########.######.#.#.####################.###############################################
.#....#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#...#.#.#######.#.#.#.#.#.#.#.#.#.....#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.##.###.#.#.#.#.#.#.#.#...#.#.#.#.#.#.#.#...#.#.#.#.#.#..#..#.#.#.#.#.#.#.#...#######...........................................
.#..#.#.#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.##.##.#####.#.##.#.##.#.#.#.#.#.#.##.##.#.#.####.#######################################
.##.............................................................................................................................
.#..########################################.###################################################################################
.##...#.......#.#.#.......#.#.#.#.#.#...#.#...#.#.#.#...........................................................................
.#..#.#####.###.#.######.##.#.#.#.#.#.###.#####.#...###############.############.#.##.#.#.#.#.#.#.#.#.#.#.###.#.#.##.#####.#####
.##.###.#.#.#.#.#.#.......#.#.#.#.#.#.#.#.#.#.#...###.#.#.#...#.#.....#...#..#.###..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.........
.#....#.......#.#.###.#.###.#.#.#.#.#...#.#...#.#...#.#.#.#.###.###.###.###.##.#.####.#.#.#.#.#.#.#.#.#.#...#.#.#.#.#.#.#.#.#.#.
.##.######.####.#.#.###.#.#.#.#.#.#.##.##.##.##.#####.#.#.#.#.#.#.....#.#.#.........############################################
.#....#.......#.#.#.......#.#.#.#.#.#...#.#...#.....#.#.#.#...#.###.###...#######.###.#.#.#.#.#.#.#.#.#.#.......................
.##.#######.###.#.######.##.#.#.#.#.##.##.##.##.#####.#.#.##.##.#.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.################.#######
................................................................................................................................
##.########.#.#.#.#.##.#.#.############################################.#############################.######.#.#.#.######.###.##
....#.#..#..#######..#.#.#.#.......#.#.#..#..#.#..#.#.#.#.#.#....#.#.#..#.#..#.#.#.#.#.#..#..#..#.#.###..#.#.#.#.#..#.#.###.#.#.
.#.##.##.####.....###############.##...#.##.##.##.#.#.#.#.#.#.####.#.####.#.##.#.#.#.#.#.##.###.#.#.#.##.#.#.#.#.#.##.#.........
.#..#.#..#..####.##..#.....#.......#.###..#..#.#..#.#.#.#.#.#....#.#.#..#.#..#.#.#.#.#.#..#..#..#.#.#.#..#.#.#.#.#....####.#####
##.##.##.#.##.....#.#####.#######.##.#.#.##.##.##.#.#.#.#.#.#.####.#.#.##.#.##.#.#.#.#.#.##.###.#.#.#.##.#.#.#.#.#.#.##.........
....#.#..#..###.###..#.....#.......#...#..#..#.#..#.#.#.#.#.#.#..#.#.#..#.#..#.#.#.#.#.#..#..#..#.#.#.#..#.#.#.#.#.#..#.#.#.#.#.
##.##.##.#.##.#.#.#.###.######.#.####.##.##.##.##.#.#.#.#.#.#.##.#.#.#.##.#.##.#.#.#.#.#.##.###.#.#.#.##.#.#####################
.....................#.....#.###.#.#............................................................................................
##.######################.##.......#####.############.######.########.#.#################################################.######
.....................#.....######.##.#......#...#.#.###.........#.#.#####.#.#.#.#.#.............................................
##.######################.##.......#.###.####.###.#.#.#.##.##.###.#.#...#.#.#.#...##############################################
.....................#.....######.##.#......#.#.#.#.#.#.#..#..#.#.#.##.##.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.....
##.###################.#.###.......#.#.#.####...#.#.#.#######...#.#.#...#.#.#.#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.#...###.##
.....................###.#.######.##.###....##.##.#.#.#...#.#.###.#.##.##.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.
##.########.#.#.##.#.#.....#.......#.#...##.#...#.#.#.#.###.....#...............................................................
....#.#.#.#.#.#..#.#.####.#######.##.###.#..##.##.#.#.#.#.###.######.#######.###################.###.#.##.######################
##.##.#.#.############.....#.......#.#...####...#.#.#.#.........#.#..#.#.#....#.#.#.#.#.#.#.#.#..#.###.#..#.#..#.#..............
.....................####.#######.##.###....##.##.#.#.###.#######.####.#.#.#.##.#.#.#.#.#.#.#.####.#.######.##.#...#.#.#.###.###
##.###################.....#.......#.#...####...#.#.#.#.........#.#..#.#.###..#.#.#.#.#.#.#.#.#..#.#.#.#..#.#..#.#.#.#.#.#...#..
.....................####.#######.##.###....##.##.#.#.###########.#.##.#.#.#.##.#.#.#.#.#.#.#.##.#.#.#.##.#.##.#.#.#.#.#.###...#
.#.####.##############.....#.......#.#...#.##...#.#.#.#.........#.#..#.#.#....#.#.#.#.#.#.#.#.#..#.#.#.#..#.#..#.#.#.#.#.#...#..
##.#.................####.###.######.###.#..##.##.#.#.########.##.#.##.#.###.##.#.#.#.#.#.#.#.##.#.#.#.##.#.##.#.###############
...###################.....#.......#.#.#.####...................................................................................
##...................####.######.#.#.#......##.##########################.######################################.#####.#########
...###################.....#.#.#.###.###.####...#.#..#.#.#.#.#..#.#.#.#.###.#...#.#.#.#.#.#..#.#.#.#.#.#.#.#.#.###.#.###........
##.#.#.#.#.#.#.#.#.#.####.##.......#.#......##.##.#.##.#.#.#.#.##.#.#.#.#.#.##.##.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.#.###.#####
.....................#.....######.##.###.####...................................................................................
##.###########.#.#.#.###.###.......#.#......####.####.###############################.###.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.......#.#.#...#######.....###.#.###.###.##.#.#...#.....#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#..#.#.#.#.###.################
##.#####...#####.....####.##.###.#.#.#....###.#######.###.###...#.#.......#.#.#.#...#...#.#.#.#####.#.#.#.#.#...#.#.#.#.#.#.#.#.
.......##.##.#.##.####.....#.#.#.#.#.###.##.#.#...#.....#.#.##.##.##.###.##.#.#.##.##.###.#.#.#.....#.#.#.#.###.................
##.#.#.#.............###.###.......#.#....#.#.#.#####.###...#...#.#..#....#.#.#.#...#...#.#.#.#.#.#.#.#.#.#..#..#.#.#.#.#.#.#.#.
.#.################.##.#.#.######.##.###.##.#.#.#.#.#.#.##.###.##.##.######.#.#.##.#############################################
................................................................................................................................
.###.####.##########.####################################################.#######################.#.#.#.#.#.#.#.#.#.##.#########
...#..#.#...#.....#.......#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...........
.###.##...#####.###.##.####.#.#.#.#.#.#.#...#.#.#.#.#.#.#.#.#.#...#.#.#.#.#.#.#.###.#.#...#.#...################################
...#..###...#.#.#.#.#..#..#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.#.#.#.##.##.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.###.##...###.....#.##...##.....................................................................................................
...#..###...####.##.#..#..##########.#################.################.######################################.######.#.#.#.#.#.
.###.##...###.....#########.#.#.#.#...#.#.#.#.#.#.#.#..#.#.#.#.#.#.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..###.#..############
...#..###.#.####.##.#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.####.#.#.#.#.#.#.####.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.#.##.#.#.#.#.#.#.
.###.##.........................................................................................................................
...#..#.#.###.#####.##.##.#.#.#.#.#.#.#.#.#.#.#################################.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.#.##.
.###.##.#.#.#.....#.#...#.#.###.#.#####.#.###..#.#.#.#....#.#..#.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#.#..
.#.#..#.#...#.#####.##.##.#.#...#.#.....#.#...##.#.#.##.###...##.#.##.#.#.#.#.##################################################
...#.##.#.###.....#.#...#.#.###.#.#####.#.###..#.#.#.#..#.#.#..#.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#..............
.###..#.#.#.#.#.#.#.##.##.#.#...#.#.....#..#..##.#.#.##.#.####.#.#.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.############.##
...#.##.#...#.#####.#...#.#.###.#.##.##.#.###...................................................................................
.###..#.#.###.#.#.#.##.##.#.#...#.#..#..#..#..###.#.#.####.###.##.#.#.#.#.##.##.##############.#################################
...#.##.#...#.....#.#...#.#.###.#.#####.#.###.#.#.#.#.#.....#...#.#.#.#.#.#...#...#.............................................
.###..#.#.###.##.##.##.##.#.#.#.#.#.....#.#.....#.#.#.###########.#.#.#.#.##.##.#######################.########################
...#.##.#.#.#.#...#.#...#.#...#.#.#####.#.#.#.###.#.#.......#.#.#.#.#.#.#.#...#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#..#.#...#.#.#.#.#.#.#.#.#.#.#.#.....#.#.#...#.#.#.#.#.#.#.....#.#.#.#.#.#.#.#.#.............................................

Score: 8066
This is the unedited solution from the dynamic programming algorithm.
Validate
.#.#.#.#...#...#.#..#..#.#.#.#...#.#.#.#.#.#.#.#................................................................................
.#.#.#.##.##.###...##.##.#.#.###.....#.#.#.#...#.##########.###########.########.##################.#################.#.#.#.#.#.
.#.#.#.#...#...#.#..#..#.#.#...#.##.##.#.#.#.###...#.#.#.#..#.#.#.#.#.###.#.#..#.#.#.#.#.#.#.#.#.#..#.#.#.#..#.#.#.#..##########
.#.#.#.##.###.##.####.##.#.#.###.#...#.#.#.#.#...###.#.#.####.....#.#.#.#.#.##.###.#.#.#.#.#.#.#.####.#.#.#.##.#.#.####.#.#.#.#.
.#.#.#.#...#...#....#..#.#.#.#...#####.#.#.#.###...#.#.#.#..##.####.............................................................
.#.#.#.##.###.##.####.##.#.#.###.....#.#.#.#.#.#.###.#.#.#.##.....####.####################################################.###.
.#.#.#.#...#...#....#..#.#.#.#...#####.#.#.#.#.....#.#.#.#..###.###.#...#..#.#.#.#.#.#.#.#.#.#.#.#..#.#.#.#.#.#.#.#.#.#.#.###.#.
.#.#.#.##.###.##.####.##.#.#.###.#.#.#.#.#.#.###.###.#.#.#.##.#.#.#.##.###.#.#.#.#.#.#.#.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.#.##
................................................................................................................................
##.##.###.#############.#############################################################.#######################################.##
.###...#...#.....#.#..#..#..#.#.#.#.#..#.#.#.#..#..#..#....#..#.#.#..#.#..#.#..#.#.#..#.#.#...#.#..#.#.#.#.#.#.#................
.#.##.##.#.####.##.#.######.#.#.#.#.##.#.#.#.##.#.##.####.##.##.#...##.##.#.##.#.#.####.#.##.##.##.#.#.#.#.....##########.######
.#.#...#####.....#.#..#..#..#.#.#...#..#.#.#.#..#..#..#....#..#.#.#..#.#..#.#..#.#.#..#.#.#...#.#..#.#.#.###.###................
.#.##.##.#.####.##.#.##.###.#.#.##.###.#.#.#.##.#.##.####.##.##.#.####.##.#.##.#.#.##.#.#.##.##.##.#.#.#.#...#.####.############
.#.#...#...#..#..#.#..#..#..#.#.#...#..#.#.#.#..#..#..#....#..#.#....#.#..#.#..#.#.#..#.#.#...#.#..#.#.#.###...#..###...........
.#.##.###.###...##.#.##.###.#.#.##.###.#.#.#.##.#.##.###.###.##.###.##.##.#.##.#.#.##.#.#.##.##.##.#.#.#.#.#.###.##.#.##########
.#.#...#...#..#..#.#..#..#..#.#.#...#..#.#.#.#..#..#..#..#.#....................................................................
.#.##.###.#####.##.#.##.###.#.#.##.###.#.#.#.##.#.##.###...######.#############.#####.#################.########################
.#.#...#...#.....#.#..#..#..#.#.#...#..#.#.#.#..#..#..#..###.#.#...#..#...#.#.###..#..#.#...#.#.#...............................
.#.##.###.#####.##.#.##.###.#.#.##.###.#.#.#.##.#.##.###.#.#.#.##.###.##.##.#.#.##.####.##.##.#.################################
................................................................................................................................
#.#######.##################.######################.#.####.###################.#####################.##.####.##################.
#.......#.#.#.#.#.#.#.#.#.......#.............#.#.#.#.#.#...#.#.#..#.#..#.#..###.#.#.#.#.#.#.#.#.#.#.#..#.#....#.#.#.#.#.#.#..##
..####.####...#.#.#.#.#.####################.##.#.#.#...##.##.#.##.#.#.##.#.##.#.#.#.#.#.#.#.#.#.#.######.###.##.#.#.#.#.#.##.#.
#.#.....#.#.###.#.#.#.#.#.......#.............#.#.#.#.#.#...#...................................................................
..#######.#...#.#.#.#.#.#####.#####.###########.#.############.#####################.##.##.#.#.#.##.##.######.##################
#.......#.#.###.#.#.#.#.#.......#...#.#.#.#.#.#.#.#...#.#.#.#..#.#.#.#..#.#.#.#.#..#..#..#.#.#.#.#..#..#........................
..#######.#...#.#.#.#.#.######.####...........#.#.#.###.....####.#.#.#.##.#.#.#.##.#############################################
#.......#.#.###.#.#.#.#.#.......#.#.###########.#.#...###.###..#.#.#.#..#.#.#.#....#.#.#.#.#.#.#.#..#..#..#.#...................
..#######.#.#.#.#.#.#.#.######.##.............#.#.#.###.#.#.#.##.#.#.#.##.#.#.###.##.#.#.#.#.#.#.##.##.#.##.######.#############
#.#.#.#.#.#.#.#.#.#.#.#.#.......####.##########.................................................................................
........#.#...#.#.#.#.#.#####.###.#..#.#.#....#######.#####.#############################.#.#.#############.#############.######
#.##.####.##.##.#.#.#.#.#.#.#.#.#.##.#.#.###.##.#.#.###.#.###.#.#.#.#.#...#.#.#.#.#.#.#.#####...#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.
..#.....#.#...#.#.#.#.#.#.......#.#..#...#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#..#..#.#...............................
#.#######.##.##.#.#.#.#.######.##.##.##.##.#.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.##.##################################
................................................................................................................................
########.#############.##################.#.##.##.#######.#.#.###########.######################################################
.....#......#.#..#..#..#...#.#.#.#..#...#.#.#..#..#.#...#######.#...#..#...#.#...#.#...#.#..#.#.#..#.....#.#.#.#................
###.####.##.#.#.###.##.#.###.#.#.#.##.#############.#.###.#.#.#.#.###.##.#.#.#.###.###.#.##.#.#.#.######.........###############
.....#...#..#.#..#..#..#...#.#.#.#..#...#.........#.#...#...#.#.#...#..#####.#...#.#.#.#..#.#.#.#..#...#.#.#.#.#................
###.####.####.#.###.##.#.###.#.#.#.##.#########.###.#.####.##.#.#.###.##...#.#.###...#.#.##.#.#.#.####.#.#######################
.....#......#.#..#..#..#...#.#.#.#..#...#.#.#.#.#.#.#...#...#.#.#...#..#.###.#...#.###.#.#..#.#.#..#.#..........................
##.#.###.####.#.###.##.#.###.#.#.#.##.###.........#.#.####.##.#.#.#.#.##...#.##.##.#...#.##.#.#.#.##...#.#################.#####
.#.###...#..#.#..#..#..#...#.#.#.#..#...#####.#####.#...#...#.#.#.###..#.###.#...#.###.#.#..#.#.#..#####...#....................
.....###...##.#.###.##.#.###.#.#.#.##.###.#.#.....#.#.####.##.#.#...#.##.#.#.##.##.#...#.##.#.#.#.##.....###.#.##############.##
##.###...#..#.#..#..#..#.#.#.#.#.#..#.#.#.....#.###.#...#...#.#.#.###..#...#.#...#.###.#.#..#.#.#..#####.#.#.#.#................
.#.#.###.####.#.###.####.#.#.#.#.#.##...#####.#...#.##.###.##.#.#...#.###.##.##.##.#.#.#.##.#.#.#.##.#.#.#.#####################
.....#...#..#.#..#..#..#...#.#.#.#..#.###..#..#####.#...#...#.#.#.###...........................................................
###.####.#.##.#.###.#.###.##.#.#.#.##.#.##.##.#.#.#.##.###.##.#.#.#.##########################################################.#
................................................................................................................................
#.###.###.################################.####################.################.###########.#######.###################.##.####
....#..#..#.#..#.#.#.#.#.#..#.#.#.#.#.#.#...#.#.#..#.#.#.#.#.#..#.#.#.#..#.#.#.###.#.#..#.#..#.#.#.###.#.#.#.#.#.#.#.#.#.#......
#.#########.##.#.#.#.#.#.#.##.#...#.#.#.#.#.#.#.#.##.#.#.#.#.####.#.#.#.##.#.#.#.#.#.##.#.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#########
....#..#..#.#..#.#.#.#.#.#..#.#.###.#.#.#####...................................................................................
#.####.#.##.##.#.#.#.#.#.#.##.#...#.#.#.#...##########.######.##################################.########.#.##############.#####
....#..#..#.#..#.#.#.#.#.#..#.#.###.#.#.#.###.#.#..#.###.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#..#.#.#.###.#.#.#..###.#.#...............
#.#.##.#.##.##.#.#.#.#.#.#.##.#...#.#.#.#.#.#.#.##.#.#.#.#.####.#.#.#.#.#.#.#.#.#...#.##.#.#.#.#.#.#.#.####.#.#.#########.######
..###..#..#.#..#.#.#.#.#.#..#.#.###.#.#.#...#.#.#..#.#.#.#.#..#.#.#.#.#.#.#.#.#.#.###.#..#......................................
#.#.##.#.##.##.#.#.#.#.#.#.##.#.#.#.#.#.##.##.#.##.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.##.#######################################
................................................................................................................................
###.#################################.##########.######.#.#.####################.###############################################
.#....#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#...#.#.#######.#.#.#.#.#.#.#.#.#.....#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.##.###.#.#.#.#.#.#.#.#...#.#.#.#.#.#.#.#...#.#.#.#.#.#..#..#.#.#.#.#.#.#.#...#######...........................................
.#..#.#.#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.##.##.#####.#.##.#.##.#.#.#.#.#.#.##.##.#.#.####.#######################################
.##.............................................................................................................................
.#..########################################.###################################################################################
.##...#.......#.#.#.......#.#.#.#.#.#...#.#...#.#.#.#...........................................................................
.#..#.#####.###.#.######.##.#.#.#.#.#.###.#####.#...###############.############.#.##.#.#.#.#.#.#.#.#.#.#.###.#.#.##.#####.#####
.##.###.#.#.#.#.#.#.......#.#.#.#.#.#.#.#.#.#.#...###.#.#.#...#.#.....#...#..#.###..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.........
.#....#.......#.#.###.#.###.#.#.#.#.#...#.#...#.#...#.#.#.#.###.###.###.###.##.#.####.#.#.#.#.#.#.#.#.#.#...#.#.#.#.#.#.#.#.#.#.
.##.######.####.#.#.###.#.#.#.#.#.#.##.##.##.##.#####.#.#.#.#.#.#.....#.#.#.........############################################
.#....#.......#.#.#.......#.#.#.#.#.#...#.#...#.....#.#.#.#...#.###.###...#######.###.#.#.#.#.#.#.#.#.#.#.......................
.##.#######.###.#.######.##.#.#.#.#.##.##.##.##.#####.#.#.##.##.#.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.################.#######
................................................................................................................................
##.########.#.#.#.#.##.#.#.############################################.#############################.######.#.#.#.######.###.##
....#.#..#..#######..#.#.#.#.......#.#.#..#..#.#..#.#.#.#.#.#....#.#.#..#.#..#.#.#.#.#.#..#..#..#.#.###..#.#.#.#.#..#.#.###.#.#.
.#.##.##.####.....###############.##...#.##.##.##.#.#.#.#.#.#.####.#.####.#.##.#.#.#.#.#.##.###.#.#.#.##.#.#.#.#.#.##.#.........
.#..#.#..#..####.##..#.....#.......#.###..#..#.#..#.#.#.#.#.#....#.#.#..#.#..#.#.#.#.#.#..#..#..#.#.#.#..#.#.#.#.#....####.#####
##.##.##.#.##.....#.#####.#######.##.#.#.##.##.##.#.#.#.#.#.#.####.#.#.##.#.##.#.#.#.#.#.##.###.#.#.#.##.#.#.#.#.#.#.##.........
....#.#..#..###.###..#.....#.......#...#..#..#.#..#.#.#.#.#.#.#..#.#.#..#.#..#.#.#.#.#.#..#..#..#.#.#.#..#.#.#.#.#.#..#.#.#.#.#.
##.##.##.#.##.#.#.#.###.######.#.####.##.##.##.##.#.#.#.#.#.#.##.#.#.#.##.#.##.#.#.#.#.#.##.###.#.#.#.##.#.#####################
.....................#.....#.###.#.#............................................................................................
##.######################.##.......#####.############.######.########.#.#################################################.######
.....................#.....######.##.#......#...#.#.###.........#.#.#####.#.#.#.#.#.............................................
##.######################.##.......#.###.####.###.#.#.#.##.##.###.#.#...#.#.#.#...##############################################
.....................#.....######.##.#......#.#.#.#.#.#.#..#..#.#.#.##.##.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.....
##.###################.#.###.......#.#.#.####...#.#.#.#######...#.#.#...#.#.#.#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.#...###.##
.....................###.#.######.##.###....##.##.#.#.#...#.#.###.#.##.##.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.
##.########.#.#.##.#.#.....#.......#.#...##.#...#.#.#.#.###.....#...............................................................
....#.#.#.#.#.#..#.#.####.#######.##.###.#..##.##.#.#.#.#.###.######.#######.###################.###.#.##.######################
##.##.#.#.############.....#.......#.#...####...#.#.#.#.........#.#..#.#.#....#.#.#.#.#.#.#.#.#..#.###.#..#.#..#.#..............
.....................####.#######.##.###....##.##.#.#.###.#######.####.#.#.#.##.#.#.#.#.#.#.#.####.#.######.##.#...#.#.#.###.###
##.###################.....#.......#.#...####...#.#.#.#.........#.#..#.#.###..#.#.#.#.#.#.#.#.#..#.#.#.#..#.#..#.#.#.#.#.#...#..
.....................####.#######.##.###....##.##.#.#.###########.#.##.#.#.#.##.#.#.#.#.#.#.#.##.#.#.#.##.#.##.#.#.#.#.#.###...#
.#.####.##############.....#.......#.#...#.##...#.#.#.#.........#.#..#.#.#....#.#.#.#.#.#.#.#.#..#.#.#.#..#.#..#.#.#.#.#.#...#..
##.#.................####.###.######.###.#..##.##.#.#.########.##.#.##.#.###.##.#.#.#.#.#.#.#.##.#.#.#.##.#.##.#.###############
...###################.....#.......#.#.#.####...................................................................................
##...................####.######.#.#.#......##.##########################.######################################.#####.#########
...###################.....#.#.#.###.###.####...#.#..#.#.#.#.#..#.#.#.#.###.#...#.#.#.#.#.#..#.#.#.#.#.#.#.#.#.###.#.###........
##.#.#.#.#.#.#.#.#.#.####.##.......#.#......##.##.#.##.#.#.#.#.##.#.#.#.#.#.##.##.#.#.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.#.###.#####
.....................#.....######.##.###.####...................................................................................
##.###########.#.#.#.###.###.......#.#......####.####.###############################.###.#.#.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.......#.#.#...#######.....###.#.###.###.##.#.#...#.....#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#..#.#.#.#.###.################
##.#####...#####.....####.##.###.#.#.#....###.#######.###.###...#.#.......#.#.#.#...#...#.#.#.#####.#.#.#.#.#...#.#.#.#.#.#.#.#.
.......##.##.#.##.####.....#.#.#.#.#.###.##.#.#...#.....#.#.##.##.##.###.##.#.#.##.##.###.#.#.#.....#.#.#.#.###.................
##.#.#.#.............###.###.......#.#....#.#.#.#####.###...#...#.#..#....#.#.#.#...#...#.#.#.#.#.#.#.#.#.#..#..#.#.#.#.#.#.#.#.
.#.################.##.#.#.######.##.###.##.#.#.#.#.#.#.##.###.##.##.######.#.#.##.#############################################
................................................................................................................................
.###.####.##########.####################################################.#######################.#.#.#.#.#.#.#.#.#.##.#########
...#..#.#...#.....#.......#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...........
.###.##...#####.###.##.####.#.#.#.#.#.#.#...#.#.#.#.#.#.#.#.#.#...#.#.#.#.#.#.#.###.#.#...#.#...################################
...#..###...#.#.#.#.#..#..#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.#.#.#.##.##.##.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.###.##...###.....#.##...##.....................................................................................................
...#..###...####.##.#..#..##########.#################.################.######################################.######.#.#.#.#.#.
.###.##...###.....#########.#.#.#.#...#.#.#.#.#.#.#.#..#.#.#.#.#.#.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..###.#..############
...#..###.#.####.##.#.#.#.#.#.#.#.##.##.#.#.#.#.#.#.####.#.#.#.#.#.#.####.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.#.##.#.#.#.#.#.#.
.###.##.........................................................................................................................
...#..#.#.###.#####.##.##.#.#.#.#.#.#.#.#.#.#.#################################.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.#.##.
.###.##.#.#.#.....#.#...#.#.###.#.#####.#.###..#.#.#.#....#.#.##.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#.#..
.#.#..#.#...#.#####.##.##.#.#...#.#.....#.#...##.#.#.##.###....#.#.##.#.#.#.#.##################################################
...#.##.#.###.....#.#...#.#.###.#.#####.#.###..#.#.#.#..#.##.###.#.#..#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#..#..............
.###..#.#.#.#.#.#.#.##.##.#.#...#.#.....#..#..##.#.#.##.#.##.#.#.#.##.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.############.##
...#.##.#...#.#####.#...#.#.###.#.##.##.#.###...................................................................................
.###..#.#.###.#.#.#.##.##.#.#...#.#..#..#..#..###.#.#.####.###.##.#.#.#.#.##.##.##############.#################################
...#.##.#...#.....#.#...#.#.###.#.#####.#.###.#.#.#.#.#.....#...#.#.#.#.#.#...#...#.............................................
.###..#.#.###.##.##.##.##.#.#.#.#.#.....#.#.....#.#.#.###########.#.#.#.#.##.##.#######################.########################
...#.##.#.#.#.#...#.#...#.#...#.#.#####.#.#.#.###.#.#.......#.#.#.#.#.#.#.#...#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.
.#.#..#.#...#.#.#.#.#.#.#.#.#.#.#.#.....#.#.#...#.#.#.#.#.#.#.....#.#.#.#.#.#.#.#.#.............................................


Husk, 43 41 40 bytes
`JD;"¦$"ṁ?Iȯ:'^`J"{}"ṁos€"¹²³⁴⁵⁶⁷⁸⁹"Λ√ġ√

Try it online!
The superscripts are 1 byte each since they're in Husk's codepage.
I wanted to replace +₁+₁ with S+`+"¦$" but it didn't work for some reason.. :(
-2 bytes borrowing the indexing idea from hyper-neutrino.
-1 byte from Leo.
Explanation
`JD;"¦$"ṁ?Iȯ`:'}+"^{"ṁos€"¹..⁹"Λ√ġ√
                              ġ√ group on alphabets
     ṁ                           map and concatenate with:
      ?                            if:
                            Λ√      the group is alphabetical
       I                             leave as is
        ȯ`:'}+"^{"ṁos€"¹..⁹"        otherwise:
                  ṁȯ                  map and concat each char to:
                     €"¹..⁹"           index in ¹..⁹(0 if not present)
                    s                  cast to string
             +"^{"                    prepend "^{"
         `:'}                         append '{'
`JD;"¦$"                            surround the final string with "\$"


VBA Excel, 168 bytes
Instruction:
I find Excel with the help of VBA is an effective and a sufficient tool for this challenge. Set the worksheet of Excel like following

Yes, we use the small, classic square-shaped pixels like the old times by using the cells in a worksheet as the pixels. Ha-ha...
Here I use cell A1 as the input and I change its font color to red. Why red? Because red is three-letter-color so it fits for golfing. Write and run the following code in the Immediate Window:
N=[A1]:Range("A1",Cells(N,N)).Interior.Color=vbRed:Range("B2",Cells(N-1,N-1)).Clear:For i=1To N:Cells(i,i).Interior.Color=vbRed:Cells(i,N+1-i).Interior.Color=vbRed:Next

Ungolfed the code:
Sub A()
    N = [A1]
    Range("A1", Cells(N, N)).Interior.Color = vbRed
    Range("B2", Cells(N - 1, N - 1)).Clear

    For i = 1 To N
        Cells(i, i).Interior.Color = vbRed
        Cells(i, N + 1 - i).Interior.Color = vbRed
    Next
End Sub

Step-by-step Explanation:
N = [A1]: Range("A1", Cells(N, N)).Interior.Color = vbRed

Range("B2", Cells(N - 1, N - 1)).Clear

Looping through the diagonal of range cells: Cells(i, i).Interior.Color = vbRed

Final step and output: Cells(i, N + 1 - i).Interior.Color = vbRed


Neim, 9 bytes

>ͻ:D+6S÷

Explanation:

>         Increment input
 ͻ        Start infinite loop
  :        Previous prime
   D       Duplicate
    +6     Push 666
      S    Swap
          See if 666 is a substring of the top of the stack
        ÷  If true, break

Try it online!


Lua, 292

Lua might not have been the best language to do this in, but it works. It pretty much flows like the question asked. The rules are mainly in order with some optimizations: #2 is skipped (it isn't needed unless there is a one vowel word with "ck" at the beginning), and the ck and y rules are gone through before the rest of #4 and #6, which are combined. Since some vowels in the word need to be captured twice (after one hyphen and before another), that search is performed twice.

i=io.read()v="([aeiou])"for s in i:gfind("%l+ ?")do
if s:len()~=4 then
s=s:gsub(v..v,function(x,y)if x==y then return x..y;end;return x.."-"..y;end)s=s:gsub("ck"..v,"ck-%1")s=s:gsub(v.."y"..v,"%1y-%2")for b=1,2 do
s=s:gsub(v.."([^aeiou\-]?)([^aeiou\-]+)"..v,"%1%2-%3%4")end
end
io.write(s)end

Ungolfed

function checkEquals(x,y)
    if x==y then
        return x..y
    end
    return x.."-"..y
end
i=io.read()
v="([aeiou])"
for s in i:gfind("%l+ ?") do
    if s:len()~=4 then
        s=s:gsub(v..v,checkEquals)
        s=s:gsub("ck"..v,"ck-%1")
        s=s:gsub(v.."y"..v,"%1y-%2")
        for b=1,2 do
            s=s:gsub(v.."([^aeiou\-]?)([^aeiou\-]+)"..v,"%1%2-%3%4")
        end
    end
    io.write(s)
end

Test it here: http://ideone.com/g57TzA


Python 3.8 (pre-release), 207 194 181 bytes

-7 bytes thanks to @ovs

Input

The width \$ w \$, the height \$ h \$, the starting point \$ s \$, and the ending point \$ e \$. \$ s \$ and \$ e \$ are complex numbers which represent the coordinates of the endpoints.

Output

A list of points, in the form of complex numbers.

C={1,-1,1j,-1j}
def f(w,h,s,e):P=[[s]];exec("P=[j+[t+(t==e==(A:=j+[t]))]for j in P for k in C if w>(t:=j[-1]+k).real>-1<t.imag<h>0<all({t+l}-{*j}for l in{0}|C-{-k})];"*w*h);return A

Try it online!

Explanation

We use a breadth-first search to search for all possible snakes, starting at \$ s \$, making sure that the next tile is not adjacent to any previous tile, and that we do not go past the boundary. After each iteration, we check for all paths whose last element is \$ e \$, meaning that we have found a complete path from \$ s \$ to \$ e \$. Instead of storing all complete paths and returning the one with maximum length, we can simply return the last path found, since breadth-first search already searches for paths by length.


CJam, 25 bytes

{z{_el_eu&\__:^32&f^?}%z}

This is an anonymous function that pops an array of strings from the stack and leaves one in return.

In supported browsers, you can verify all test cases at once in the CJam interpreter.

Test cases

Code

qN/2/                     e# Read input and split into arrays of two strings.

{z{_el_eu&\__:^32&f^?}%z}

%                         e# Map the block over all string arrays.
:+N*                      e# Separate the strings by linefeeds.

Input

ABCDEfghijKlMnOpqrstuvwxyz
aaaaaaaaaaaaaaaa----------
PRogrammiNG puzZLes & CODe golf
SdlkhkfaladlKsdlalksdg7ldklDgsl
AAAbbb111
Cc2Dd3Ee4

Output

abcdefghijklmnopqrstuvwxyz
AAAAAaaaaaAaAaAa----------
Programming Puzzles & Code Golf
SDlkhkfalADlksdLAlksdg7LDkldgsl
AaABbb111
CC2dd3Ee4

How it works

z                       e# Zip to transform the array of strings into an array
                        e# of character pairs.
 {                  }%  e# For each character pair:
  _el                   e#   Push a copy and convert to lowercase.
     _eu                e#   Push a copy and convert to uppercase.
        &               e#   Intersect. The result will be an empty string if
                        e#   and only if both characters are letters.
         \              e#   Swap the character pair on top of the stack.
          __            e#   Push two copies.
            :^          e#   XOR both characters.
              32&       e#   Logical AND with 32. This pushes 32 for letters of
                        e#   different cases and 0 for letters of the same case.
                 f^     e#   XOR each character with the result.
                   ?    e#   Select the original copy for non-empty intersection
                        e#   and the modified one otherwise.
                      z e# Zip to turn the characters pairs back into two strings.


Python 2, 41 bytes

s="print input()in's=%r;exec s'%s";exec s

Try it online!


Python, 28 bytes

lambda x:"{{}"*x+x*"}"or"{}"

Try it online!

This is a pretty bland solution to the problem.  For numbers greater than zero you can get the representation with the string formula "{{}"*x+"}"*x.  However this doesn't work for zero where this is the empty string.  We can use this fact to short circuit an or to return the empty set.

I wanted to use python's builtin set objects to solve this problem but unfortunately:

TypeError: unhashable type: 'set'

You cannot put sets inside of sets in python.


JavaScript (ES6),  162  158 bytes
Returns 0 if the grid is invalid, or a positive integer otherwise.

m=>(o=0,g=u=>m.some((r,y)=>r.some((v,x)=>v&&[0,1].map(d=>(r=(w,X=x+!d*w,R=m[y+d*w]||0)=>R[X]&&R[R[X]--,u&(k=1<<w*3)*7&&g(u-k),r(-~w),X]++)``)))|u||++o)(668)*o

Try it online!
Commented
m => (                       // m[] = input matrix
  o = 0,                     // o = result
  g = u =>                   // g is a recursive function taking a bit mask u
                             // holding the ship counters on 4 x 3 bits
  m.some((r, y) =>           //   for each row r[] at position y in m[]:
    r.some((v, x) =>         //     for each value v at position x in r[]:
      v &&                   //       abort if v is not set
      [0, 1].map(d =>        //       otherwise, look for a horizontal ship (d = 0)
        (                    //       or a vertical ship (d = 1):
          r = (              //         r is a recursive function taking:
            w,               //           w = ship width - 1
            X = x + !d * w,  //           X = position of the cell in ...
            R = m[y + d * w] //           ... R[] = row of the cell
                || 0         //           (force it to 0 if undefined)
          ) =>               //
          R[X] &&            //           abort if R[X] is not set
          R[                 //
            R[X]--,          //           otherwise, set the cell to 0
            u & 7 *          //           if there's still at least one ship of
            (k = 1 << w * 3) //           width w to be found in the grid:
            && g(u - k),     //             do a recursive call to g with u updated
            r(-~w),          //           do a recursive call to r with w + 1
            X                //
          ]++                //           restore the cell afterwards
        )``                  //         initial call to r with u zero'ish
      )                      //       end of map()
    )                        //     end of some()
  )                          //   end of some()
  | u || ++o                 //   increment o if the grid has been cleared and all
                             //   boats have been found
)(668)                       // initial call to g with u = 668 (001 010 011 100)
* o                          // multiply by o


PowerShell v3+, 450 bytes

param($n)function f{param($a)for($i=2;$a-gt1){if(!($a%$i)){$i;$a/=$i}else{$i++}}}
$y=($x=@((f $n)-split'(.)'-ne''|sort))|?{$_-eq(f $_)}
$a,$b=$x
$a=,$a
while($b){$z,$b=$b;$a=$a+($a+$y|%{$c="$_";0..$c.Length|%{-join($c[0..$_]+$z+$c[++$_..$c.Length])};"$z$c";"$c$z"})|select -u}
$x=-join($x|sort -des)
$l=@();$a|?{$_-eq(f $_)}|%{$j=$_;for($i=0;$i-le$x;$i+=$j){if(0-notin($l|%{$i%$_})){if(-join((f $i)-split'(.)'|sort -des)-eq$x){$i}}}$l+=$j}|?{$_-ne$n}

Finally!

PowerShell doesn't have any built-ins for primality checking, factorization, or permutations, so this is completely rolled by hand. I worked through a bunch of optimization tricks to try and reduce the time complexity down to something that will fit in the challenge restrictions, and I'm happy to say that I finally succeeded --

PS C:\Tools\Scripts\golfing> Measure-Command {.\prime-factors-buddies.ps1 204}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 27
Milliseconds      : 114
Ticks             : 271149810
TotalDays         : 0.000313830798611111
TotalHours        : 0.00753193916666667
TotalMinutes      : 0.45191635
TotalSeconds      : 27.114981
TotalMilliseconds : 27114.981

Explanation

There's a lot going on here, so I'll try to break it down.

The first line takes input $n and defines a function, f. This function uses accumulative trial division to come up with a list of the prime factors. It's pretty speedy for small inputs, but obviously bogs down if the input is large. Thankfully all the test cases are small, so this is sufficient.

The next line gets the factors of $n, -splits them on every digit ignoring any empty results (this is needed due to how PowerShell does regex matching and how it moves the pointer through the input and is kinda annoying for golfing purposes), then sorts the results in ascending order. We store that array of digits into $x, and use that as the input to a |?{...} filter to pull out only those that are themselves prime. Those prime digits are stored into $y for use later.

We then split $x into two components. The first (i.e., smallest) digit is stored into $a, while the rest are passed into $b. If $x only has one digit, then $b will be empty/null. We then need to re-cast $a as an array, so we use the comma operator quick-like to do so.

Next, we need to construct all possible permutations of the digits. This is necessary so our division tests later skip a bunch of numbers and make things faster overall.

So long as there's element left in $b, we peel off the first digit into $z and leave the remaining in $b. Then, we need to accumulate into $a the result of some string slicing and dicing. We take $a+$y as array concatenation, and for each element we construct a new string $c, then loop through $c's .length and insert $z into every position, including prepending $z$c and appending $c$z, then selecting only the -unique elements. That's again array-concatenated with $a and re-stored back into $a. Yes, this does wind up having goofy things happen, like you can get 3333 for input 117, which isn't actually a permutation, but this is much shorter than attempting to explicitly filter them out, ensures that we get every permutation, and is only very marginally slower.

So, now $a has an array of all possible (and then some) permutations of the factor's digits. We need to re-set $x to be our upper-bound of possible results by |sorting the digits in -descending order and -joining them back together. Obviously, no output value can be larger than this number.

We set our helper array $l to be an array of values that we've previously seen. Next, we're pulling out every value from $a (i.e., those permutations) that are prime, and enter a loop that is the biggest time sink of the whole program...

Every iteration, we're looping from 0 to our upper bound $x, incrementing by the current element $j. So long as the $i value we're considering is not a multiple of a previous value (that's the 0-notin($l|%{$i%$_}) section), it's a potential candidate for output. If we take the factors of $i, sort them, and they -equal $x, then add the value to the pipeline. At the end of the loop, we add our current element $j into our $l array for use next time, as we've already considered all those values.

Finally, we tack on |?{$_-ne$n} to pull out those that are not the input element. They're all left on the pipeline and output is implicit.

Examples

PS C:\Tools\Scripts\golfing> 2,4,8,15,16,23,42,117,126,204|%{"$_ --> "+(.\prime-factors-buddies $_)}
2 -->
4 -->
8 -->
15 --> 53
16 -->
23 --> 6
42 --> 74 146 161
117 --> 279 939 993 3313 3331
126 --> 222 438 674 746 1466 483 851 1679 1631
204 --> 782 2921 3266 6233 3791 15833 2951 7037 364 868 8561 15491 22547 852 762 1626 692 548 1268 2654 3446 2474 5462 4742 5426 4274 14426 6542 6434 14642


count of numbers between A and B (inclusive) that have sum of digits equal to S

GolfScript, 35 bytes

~:S;),>{.10base{+}*S=!{;}*}%(\,)]n*

How it works

~         # Interpret the input string.
:S;       # Save the last integer in variable “S” and pop it from the stack.
),        # Increment it by 1 and create the array “[0 ... B]”
>         # Remove the first “A” elements. This leaves the array “[A ... B]”
{         # For each of those integers:
  .10base # Push its digits in base 10.
  {+}*    # Add the digits.
  S=!{;}* # If the sum is not equal to “S”, pop the integer from the stack.
}%        # Collect the results into an array.
(\        # Extract the first and swap it below the array.
,)        # Count the elements of the array of all remaining integers and increment by one.
n\        # Separate the results by a newline.


CJam, 51 49 47 41 34 bytes
WqN/:Q0=,,{Qf{_,,\f>\)f<$}~=},+W=)

Just to get something started. This can surely be golfed a lot. Looking at a different algorithm too.
Input goes as the two strings without quotes on separate lines.
Example:
aaabbaabb
aabbaaabb

Output:
3

Try it online here


Convert a 32 bit binary IPv4 address to its quad-dotted notation

x86-16 machine code, IBM PC DOS, 54 47 45 bytes
Binary:
00000000: be82 00b3 04b1 08ac d0d8 d0d4 e2f9 8ac4  ................
00000010: 41d4 0a50 8ac4 84c0 75f6 580c 30b4 0ecd  A..P....u.X.0...
00000020: 10e2 f74b 7406 b02e cd10 ebd9 c3         ...Kt........

Build and test BIN2IP.COM using xxd -r from above.
Unassembled listing:
BE 0082     MOV  SI, 82H        ; command line input address
B3 04       MOV  BL, 4          ; loop 4 bytes
        BYTE_LOOP:
B1 08       MOV  CL, 8          ; loop 8 bits
        BIT_LOOP:
AC          LODSB               ; load next bit char into AL
D0 D8       RCR  AL, 1          ; put LSB of char into CF
D0 D4       RCL  AH, 1          ; put CF into LSB of byte value, then shift left
E2 F9       LOOP BIT_LOOP       ; continue bit loop
8A C4       MOV  AL, AH         ; put byte result into AL
        GET_DIGIT:
D4 0A       AAM                 ; byte divide by 10, AH = AL / 10, AL = AL % 10
50          PUSH AX             ; save remainder in AL on stack
8A C4       MOV  AL, AH         ; put quotient back into AL
41          INC  CX             ; increment decimal digit count
D4 C0       TEST AL, AL         ; quotient = 0?
75 F6       JNZ  GET_DIGIT      ; if not, continue looping
        PRINT_DIGIT:
58          POP  AX             ; restore digit in AL
0C 30       OR   AL, '0'        ; ASCII convert
B4 0E       MOV  AH, 0EH        ; BIOS write char function
CD 10       INT  10H            ; write to console
E2 F7       LOOP PRINT_DIGIT    ; loop until done
4B          DEC  BX             ; is last byte?
74 06       JZ   END_LOOP       ; if so, don't display a '.'
B0 2E       MOV  AL, '.'        ; otherwise display '.'
CD 10       INT  10H            ; write to console
        END_LOOP:
75 D7       JNZ  BYTE_LOOP      ; continue byte loop
C3          RET                 ; exit to DOS

Output:

A standalone PC DOS executable.  Input is command line, output to console.
Notes:
The "interesting part" (converting binary string to bytes) is about 15 bytes, whereas the rest of code is writing the itoa() function to convert binary bytes into a decimal string representation for display.

-2 bytes eliminating unnecessary PUSH/POP thx to @PeterCordes!


Pyth, 9 8 bytes
Thanks to @isaacg for -1 byte!
sXz.HM16

Subtracting a certain color's value from 255 is equivalent to subtracting each of its hexadecimal digits from 15. Say a number is 16a+b. Then the value of the number created by subtracting its digits from 15 is 16(15-a) + (15-b) = 255 - (16a+b).
sXz.HM16     implicit: z=input()
      16
   .HM        map hex representation over range
   .HM16     '0123456789abcdef'
  z           the input string
 X            Translate characters in x1 present in x2 to reversed x2
              that is, '0' becomes f, '1' becomes 'e', and so on.
              The initial '#' is unchanged.
s             That produced a list, so join into a string by reducing +

Try it here. Test suite.


Make me a loaf of bread before I'm fired!

Charcoal, 35 33 30 bytes
Saved 2 bytes thanks to @ASCII-only
Saved 3 bytes thanks to @Neil by rearranging the order in which the lines where drawn
ＮωＮηＮδ↓η↗→×_ω↑η←\←ω↗δ→/ω↓\↓η↙δ

Try it online!
This is my first try at Charcoal. I heard it was good at ascii-art, so I tried it out.
Explanation (outdated)
ＮωＮηＮδ                  # Take the width, height and depth as input
↗¹                        # Write 1 /
↗δ                        # Write depth times /
¶ω                        # Move the cursor down and write - width times
\                         # Write \
↙↓η                       # Move the cursor one down and one left, and then write | height times
↙δ                        # Write / depth times

Now the back part of the loaf is complete. Now comes the front face.
↑↑η                       # Go up and write | height times
←\                        # Write a \ and move 1 to the left
←ω                        # Write - width times
↓↓η                       # Go down and write | height times
↗Ｐ×_ω                    # Go northeast and write _ width times


JavaScript (ES6), 525 492 454 453 bytes

-71 bytes thanks to @Guedes
-1 bit thanks to @Jake Cobb

P=(K,p)=>{y='charCodeAt';n=(C)=>C[y](0)%3>0;N=(C)=>C[y](0)%3<1;E=(C)=>C[y](0)%2>0;O=(C)=>C[y](0)%2<1;f=(U)=>U%2<1;F=(U)=>U%2;M=(U,C)=>C%s==S;s=S=0;a=new Array();G='filter';e=p[G](f);o=p[G](F);J=K[G](n);r=J[G](O);L='length';i=J[G](E);s=r[L];for(S=0;S<r[L];S++){a.push([r[S],e[G](M)]);}s=i[L];for(S=0;S<i[L];S++){a.push([i[S],o[G](M)]);}K=K[G](N);for(S=0;S<K[L];S++){a.push(K[S],[]);}return(e[L]<r[L]||o[L]<i[L]||(r[L]+i[L])<1)?"PPCGmas is Cancelled!":a;}

Try it online !

Non-golfed version

Can be more golfed I think. I just did a litteral translation of the non-golfed version.

This is now shorter than the sum of the CharCode composing "santa" (115 + 97 + 110 + 116 + 97 = 535). Yeay


There are a number of subtleties to squeezing out the last few characters in J. For the following, assume that each capital letters is a primitive verb (i.e. I am removing the spaces that would otherwise be required to delimit names).

When you have a train going, and you need to apply a function atop another partway through, ([:FLGR) and (LF@:GR) have the same number of characters, but (LF@GR) saves one. If the frame of G is greater than or equal to the monad rank of F, this is a valid transformation. Notably, all trains have infinite rank, as do , ,. ,: ~. /: \: [ ] and most uses of # and |..
If you have to pick strings out of a list, and these strings have no spaces, use >i{ab`cd`ef. It's dirty, but it saves characters for each new string you have to deal with, unless you're just pulling single characters, and even then the charlist has to be length 4 to be shorter. What's happening is that undefined names are treated as references to verbs, and when you take the gerunds of those verbs you get a boxed string of the name. Any names that are already defined as having type noun, adverb, or conjunction cannot be used in this fashion, because those names get resolved before ` can have at them.
If you're lucky enough to have an expression to work with and not just a tacit verb, it's almost always worth it to assign any bits you reuse to variables, be they nouns, verbs, or adverbs. The parens will sometimes pay themselves back by fitting right into where you had spaces before, and most such definitions are worth it if they're reused even once more.
Conjunctions like (FGH)^:(u`v`w) can be rewritten u`v`w(FGH^:). This works for any length of train, even 1, though you only save anything if this trick removes parens from the right argument. This trick only works when you preload the left operand. (Have no idea what just happened? Look up 'tacit adverbs', and study the Parsing and Execution section of the J Dictionary.)
Don't use a.&i., use u:! {&a. and 3&u: are equivalent on length, though, and the former might be more useful in a conjunction (depending on the conjunction).
Things like (2%~F) and (F%2:) are equivalent in length. This is useful because sometimes, depending on what the rest of your train looks like, you can restructure it with @ tricks as written in the first point, to save some desperate characters. (And of course, if F is ] and the train is a monad, using %&2 saves a char, duh.)
Hook-like trains with ] or [ as the leftmost verb, e.g. (]FGH).

] lets you break up a dyadic application and use the right argument only. (Swap to left with (]FGH)~, at a penalty of at least 1 character, maybe more.) Saves a char over (FGH)@], and is very handy in gerunds!
[ in a hook applied monadically allows you to do something for the side effects on the right side, then return the argument again. Most common usage is with 1!:2, possibly with formatting junk.

I/O sucks. Speed up the process by making loops out of everything you can. 1!:1 has rank 0, and both of 1!:2 3 have rank _ 0, for example, so utilise this by making arrays of 1s and run 1!:1 directly over them. Note that ". also has rank 1, so you can usually just put that directly after 1!:1, too, and not have to attach it via @ or rank shenanigans.
It's not easy to find places to put this, but :: can be useful.

::]^:_ is a particularly powerful combination, for example, that lets you do something dangerous until you can't do it anymore. (Subject to the usual ^:_-as-a-loop caveats.)
This also lets you use { on lists that don't have the desired index, because it throws a domain error when that happens. Useful to e.g. take the head of a list only if it exists (try using ::] to return the empty list, or ::_1: to return an error code, and so on).

]`($:@u)@.v can usually be made shorter than u^:v^:_, especially on definitions of u and v that can be played around with. A similar case holds for the conditional-like u^:(1-v) vs. ]`u@.v. Consider your options, especially when you have lots of named verbs floating about. It's also a little more flexible, but remember, if using $:, there is a recursion depth that it's easy to bump up against. (Usually something like 1800 iterations?)


Convert a decimal to a fraction, approximately

Scratch 3.0, 45 blocks/254 bytes

As SB Syntax:
define(n)(d)
set[A v]to(0
set[D v]to(d
set[i v]to(0
repeat((n)+(1
set[j v]to(0
repeat((n)+(1
set[r v]to((i)/(j
if<(D)>([abs v]of((r)-(D)))>then
set[A v]to(join(i)(join(/)(j
set[D v]to([abs v]of((r)-(D
end
set[j v]to((j)+(1
end
set[i v]to((i)+(1
end
say(A

Try it on Scratch!
Just wouldn't be right if I didn't do this in Scratch.
Explained
define(n)(d) // Define an empty name function with parameters `n` (the denominator) and `d` (the decimal to fractionify)
set[D v]to(d // This will store the current absolute difference between potential fraction and actual decimal
set[i v]to(0 // outer-cartesian-loop variable
repeat((n)+(1
set[j v]to(0 // inner-cartesian-loop variable
repeat((n)+(1 // simulate cartesian product of range(0, n+1) and range(0, n+1)
set[r v]to((i)/(j // divide i / j
if<(D)>([abs v]of((r)-(D)))>then // if the difference between (i/j) and d is smaller than the current smallest difference
set[A v]to(join(i)(join(/)(j // update answer and difference
set[D v]to([abs v]of((r)-(D
end
set[j v]to((j)+(1
end
set[i v]to((i)+(1
end
say(A // output answer


How many mesas begin with a given string?

Ruby, 142 characters
m=->l{[*l[0].chars].repeated_permutation(l[-1].size+1).reduce(1){|s,x|l.any?{|y|x*''=~/#{[*y.chars]*'.*'}/}?s:s+m[l+[x*'']]}}
p m[[gets.chop]]

This algorithm is constructive, i.e. it builds all possible mesas for the input string and counts them. It makes the program really, really slow - but hey, it is codegolf.
Example runs:
> a
1
> aa
1
> ab
43


Give me your tired, your poor, Your huddled masses yearning to breathe free

Jelly, 19 bytes

Ẇ“©ØḌKAƑ⁶2ɼU’b8ȷ¤ịY

Try it online! or try it with some other text.

How?

Indexes into the list of all non-empty contiguous slices of the input string and joins with line feeds.

Ẇ“©ØḌKAƑ⁶2ɼU’b8ȷ¤ịY - Main link: s
                ¤   - nilad followed by link(s) as a nilad
 “©ØḌKAƑ⁶2ɼU’       - base 250 number, 27003436588466956354336
              8ȷ    - 8 * 1e3 = 8000
             b      - convert to base, [824,635,7086,6796,1544,2336]
                 ị  - index into
Ẇ                   - all non-empty contiguous slices of s
                  Y - join with line feeds

Previous code, 22 bytes:

“ÇŒȷœ%LRw⁹ƊƓɠ‘ṬœṗµḊm2Y

Partitions the input string, takes every second element and joins with line feeds. “ÇŒȷœ%LRw⁹ƊƓɠ‘ is a list of code page indexes, Ṭ makes a list of zeros with ones at those indexes, œṗ partitions the input at the truthy indexes of that list, Ḋ removes the first element, m2 takes every second element, and Y joins with line feeds.


Java (JDK 10), 295 bytes

int f(Long a,long b,long c,long d){return a>c?-f(c,d,a,b):a<c?b>d?d==1?(a==2&((b==2&c==4)|(b==3&c==16)))|(a==3&b==2&c==27)?0:1:b-d>1?1:f(t(a,b),1,c,d):-1:a<2?0:a.signum(b-d);}long t(long a,long b){long t=1;for(;b-->0;)t=p(a,t);return t;}long p(long a,long b){long p=1;for(;b-->0;)p*=a;return p;}

Try it online!

Codes:

1 if a^^b > c^^d
-1 if a^^b < c^^d
0 is a^^b == c^^d


Ruby, 107 103 99 94 90 characters
$><<[$/,(gets||Time.now.to_s[11,8]).tr(z=" .':","").bytes.map{|a|$><<z[a/4-12];z[a%4]}]*""

Note that the input shouldn't contain a linebreak, so to test the solution use something like echo -n "01:08:01" | ruby1.9 bcd.rb.
To test the default behavior, run it with ruby1.9 bcd.rb and enter ^D (i.e. a literal EOF).


Create a black window with as little memory as possible

C, 96KiB
Never seen restricted-memory but i guess code length doesn't matter here. RAM usage was taken from gnome-system-monitor (i hope it's accurate).
#include <stdlib.h>
#include <stdio.h>

#include <xcb/xcb.h>

int main ()
{
  xcb_connection_t    *c;
  xcb_screen_t        *screen;
  xcb_drawable_t       win;
  xcb_generic_event_t *e;
  uint32_t             values[2];

  char string[] = " ";

  c = xcb_connect (NULL, NULL);
  screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
  win = screen->root;

  win = xcb_generate_id(c);
  values[0] = screen->black_pixel;
  values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
  xcb_create_window (c,XCB_COPY_FROM_PARENT,win,screen->root,
  0, 0,640,480,10,XCB_WINDOW_CLASS_INPUT_OUTPUT,screen->root_visual,XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, values);

  xcb_map_window (c, win);
  xcb_flush (c);

  while ((e = xcb_wait_for_event (c))) {
    switch (e->response_type & ~0x80) {
    case XCB_EXPOSE:
      xcb_flush (c);
      break;
    }
    free (e);
  }
  return 0;
}

This code works on pretty much every linux with a running xserver. To compile add -lxcb for the xcb libs.


Python, 33 bytes

lambda n,f:n*~-n*5-f(range(n))*10

Puts 0 coins from supplier 0, 1 coin from supplier 1, ... n-1 coins from supplier n-1. The deficit is then 0.1 for each coin sent to the cheating supplier, so i/10. We therefore subtract the oracle value from the fair sum n*(n-1)/2 and multiply by 10. Distributing the *10 saved on parens for a shorter expression.


Python 3, 249 246 bytes

Shaved off 3 bytes, thanks DLosc.

Input and output are single strings, with '.', '@', and 'X' representing water, huts, and land, respectively.

A='@'
def f(s):
 w=s.find('\n')+1;d=u={(k,k):0for k,c in enumerate(s)if A<c}
 while u:d.update(u);u={(k,j):d[(k,i)]+1for k,i in d for j in{i+1,i+w,i-1,i-w}if A<s[j]and(k,j)not in d}
 k,j=sorted(max(d,key=d.get))
 return s[:k]+A+s[k+1:j]+A+s[j+1:]

Prior version:

Input is a single string, with '.' and '#' representing water and land, respectively.  'X' represents the huts in the output.

def f(s):
 w=s.find('\n')+1;d=u={(k,k):0for k,c in enumerate(s)if'#'==c}
 while u:d.update(u);u={(k,j):d[(k,i)]+1 for k,i in d for j in{i+1,i+w,i-1,i-w}if'#'==s[j]and(k,j)not in d}
 k,j=sorted(max(d,key=d.get))
 return s[:k]+'X'+s[k+1:j]+'X'+s[j+1:]

Explanation:

It's basically doing a breadth first search from every possible starting point at the same time.  Keep a dictionary, d, of path lengths keyed by the start and end of the path, e.g., d[(k,i)] is the distance from k to i.  Then iterate over the keys in the dictionary, d, and create a new dictionary, u, with paths that are 1 unit longer by moving the end point 1 unit to the N,S,E,W, e.g., u[(k, i+1)] = d[(k,i)] + 1.  Don't include paths that are already in d.  If u is not empty, then add the new longer paths to d and repeat.  When u is empty, that means no more paths could be made.  Now d contains all the possible paths and their lengths.  So its just a matter of getting the key with the longest path.

Less golfed, commented version:

def f(s):
  w=s.find('\n')+1                    # width of a row, or a move N or S

  d = {}                              # dictionary of all the paths.
                                      # The key is a tuple (k,j) and the
                                      # value is the distance from k to j.
  for k,c in enumerate(s):            # Initialize. Distance from k to k is 0
    if'#'==c:                         # Only do land.
      d[(k,k)] = 0

  u = d                               # dictionary of new paths. initialize it to d
                                      # so loop is entered. first d.update is
                                      # basically a NOP

  while u:                            # while there are new paths
    d.update(u)                       # add the new paths to the dict of old paths
    u={}                              #
    for k,i in d:                     # iterate over the known paths. k is the start, i is the end
      for j in{i+1,i+w,i-1,i-w}:      # iterate over squares 1 move to the E,S,W,N from i
        if'#'==s[j]and(k,j)not in d:  # if it's still land, and the path (k,j) isn't already in d,
          u[(k,j)] = d[(k,i)]+1       # then add the new path to u

  k,j=sorted(max(d,key=d.get))        # find the longest path

  return s[:k]+'X'+s[k+1:j]+'X'+s[j+1:]  # X marks the endpoints.


PowerShell v2+, 237 149 147 231 216 181 174 169 166 bytes

param($n)filter f($a){'1'*$a-match'^(?!(..+)\1+$)..'}for($i=$n;!((f $i)-and(f($i+2)))){$i++}for(){if(f(--$i)){if((f($i-2))-or(f($i+2))){if($i-lt$n-1){exit}}else{$i}}}

Takes input $n. Defines a new function f as the regex prime function (here returning a Boolean if the input is a prime or not).

The next part sets $i to be equal to $n, then loops upward until we find the bottom half of our twin prime pair upper bound. E.g., for input 90, this stops at $i=101.

Then, we loop from the upper bound downward. I know, it looks like an infinite loop, but it'll eventually end.

If the current number is a prime (f(--$i)), but its +/- 2 isn't a prime, we add $i to the pipeline. However, if its +/- 2 is a prime, we check whether we're lower than $n-1 (i.e., to account for the situation when it's inside a twin prime pair), at which point we exit. At program completion, the pipeline is printed to screen via implicit Write-Output.

NB - Due to the looping structure, prints the primes in descending order. OP has clarified that's OK.

Examples

Output here is space-separated, as that's the default stringification method for an array.

PS C:\Tools\Scripts\golfing> 70,71,72,73,90,201,499,982|%{"$_ --> "+(.\the-solitude-of-prime-numbers.ps1 $_)}
70 --> 67
71 --> 67
72 --> 97 89 83 79 67
73 --> 97 89 83 79
90 --> 97 89 83 79
201 --> 223 211
499 --> 509 503 499 491 487 479 467
982 --> 1013 1009 997 991 983 977 971 967 953 947 941 937 929 919 911 907 887


Python, 217 bytes

Requires the Python Imaging Library

import Image
x=p=141
i=Image.new('1',(x,11))
while~-p:x=p/2*x/p+2*10**19;p-=2
for c in str(x):[i.putpixel((j%7/5*4-~j%7/4*~j/7+p,j%7*3%14%8+j%14/10+2),1&ord('}`7gjO_a\177o'[int(c)])>>j%7)for j in range(17)];p+=7
i.show()

The byte count assumes that the escaped character \177 is replaced with its literal equivalent (char 127).

Output will appear as the following (it will open in your default *.bmp viewer):

Note that this could easily be parameterized to print any number of digits you like. The following will accept an integer input from stdin, and display that many digits:

import Image
n=input()
x=p=n*7|1
i=Image.new('1',(x,11))
while~-p:x=p/2*x/p+2*10**(n-1);p-=2
for c in str(x):[i.putpixel((j%7/5*4-~j%7/4*~j/7+p,j%7*3%14%8+j%14/10+2),1&ord('}`7gjO_a\177o'[int(c)])>>j%7)for j in range(17)];p+=7
i.show()

Output for n=80:

Pi Calculation

while~-p:x=p/2*x/p+2*10**19;p-=2

Yup, that's it. The formula used is the result of applying Euler's Transform to the Leibniz Series, and then factoring out each term from the remainder of the sum. The formula converges linearly; each digit requires log2(10) ≈ 3.32 iterations. For those interested in the derivation, see Appendix A.

Display

PIL is used for image generation, because it's the most convenient library that I know of. A blank 141×11 black and white bitmap is created, and then white lines are drawn on it in a seven-segment fashion, one pixel at a time. The positions required to draw each segment are stored in a bitmask string, with bits corresponding to the following positions:

 000
3   5
3   5
 111
4   6
4   6
 222

The bit of magic (j%7/5*4-~j%7/4*~j/7+p,j%7*3%14%8+j%14/10+2) produces the each pixel in the following order (base-18):

(2, 2), (2, 5), (2, 8), (1, 3), (1, 6), (5, 3), (5, 6),
(3, 2), (3, 5), (3, 8), (1, 4), (1, 7), (5, 4), (5, 7),
(4, 2), (4, 5), (4, 8)

 07e
3   5
a   c
 18f
4   6
b   d
 29g

Appendix A

Euler's Transform is a convergence acceleration technique that works for any series which displays absolute monotonic convergence. The resulting series will converge linearly, typically at the rate of one bit per term (note that if the original series was already super-linear, the resulting series will actually converge slower). The purely mathematical description is a bit hard to follow, so I'll be taking a procedural approach.

We'll start with the Leibniz Series:

Then split each term in half, combining neighboring terms:

Simplified:

Generalized:

Notice that the leading ½ didn't have a partner term, and thus was excluded from the rest of the sum. This is the first term of the transformed series. To find the next term, we repeat the process again:

And again:

And again:

And once more for good measure:

At this point we have the first five terms, and the sixth term is evident. This should be enough to generalize, so
we'll stop here. We'll start by factoring the numerators and denominators:

The denominators evidently contain a Double Factorial of 2n+1, so we'll patch that in:

Everything fits, except for the first two terms which have an unaccounted for 2 in the denominator. We can fix that by multiplying the entire expression by 2:

23 = 2·4, so:

The numerator can now be easily identified as n!.

Notice that the factor added to each successive term, n/(2n+1), approaches ½ as n becomes large, implying a linear convergence at the rate of one bit per term - this is in fact by design. A nice result, but it'd be even nicer without the factorials in there. What we can do here is to factor out each successive term from the rest of the sum, which will generate a nested expression:

This can be rewritten as a recurrence relation:

Where n counts backwards from ⌈log2(10)·d⌉ .. 0, where d is the number of digits required.

It might be interesting to note that the stable point of this recurrence is exactly 2 (or 4 if you've doubled it, as I have in the implmentation above), so you can save a number of iterations by initializing properly. Though, initializing to a random value you need elsewhere, and throwing a few extra iterations on the top is generally cheaper byte-wise.


Random Golf of the Day #3: Integer Partitions

Python 2, 179 bytes
from random import*
m=r=input();i=q=r+1;h=[1]+[0]*q*q;exec"h[i]=h[i+~q]+h[i-i%q*q];i+=1;"*r*q
while r:
 x=random()*sum(h[r*q:r*q-~m]);m=0
 while x>0:m+=1;x-=h[r*q+m]
 print m;r-=m

I used formula (39) from this Knuth extract, which gives the number of partitions of n that have exactly m parts. This happens to equal the number of partitions of n that have m as the maximum element, which is the interpretation I'm using. The elements of the partition are generated from greatest to least. At each stage the formula is reused with the current remainder of n and maximum allowed element.


Calculate the Delacorte Number of a square

APL (38)

{.5×+/∊∘.{(∨/Z[⍺⍵])×+/⊃×⍨⍺-⍵}⍨⊂¨⍳⍴Z←⍵}

This is a function that takes a matrix as its right argument, like so:

      sq5←↑(10 8 11 14 12)(21 4 19 7 9)(5 13 23 1 16)(18 3 17 2 15)(24 22 25 6 20)
      sq5
10  8 11 14 12
21  4 19  7  9
 5 13 23  1 16
18  3 17  2 15
24 22 25  6 20
      {.5×+/∊∘.{(∨/Z[⍺⍵])×+/⊃×⍨⍺-⍵}⍨⊂¨⍳⍴Z←⍵}sq5
5957

Explanation:

⊂¨⍳⍴Z←⍵: store the matrix in Z. Make a list of each possible pair of coordinates in Z.
∘.{...}⍨: for each pair of coordinates, combined with each pair of coordinates:

+/⊃×⍨⍺-⍵: calculate distance^2: subtract the first pair of coordinates from the second, multiply both by themselves and sum the result
∨/Z[⍺⍵]: get the number in Z for both pairs of coordinates, and find the GCD
×: multiply them by each other

+/∊: sum the elements of the result of that
.5×: multiply by 0.5 (because we counted each nonzero pair twice earlier)


Jelly, 6 bytes

j0j@ṣ0

A dyadic Link accepting the first function representation on the right and the second function representation on the left which yields the resulting function representation. Each function representation is a list of lists of characters (Jelly has no other strings).

Try it online! (the full-program arguments are given in Python notation; strings become lists. The footer shows a Python representation of the Link's output.)

Here is a test-suite which reformats the Link's output like the inputs.

How?

Takes advantage of Jelly's mixed type lists to allow the entire domain of representations (any list of lists of characters) by using the integer zero as a place-holder:

j0j@ṣ0 - Link: b, a        e.g.    b = [['a','b'],['c','d'],['e']]
       -                   ...and  a = [['w'],['x'],['y'],['z']]
                             (i.e. test-case ["w","x","y","z"] ["ab","cd","e"])
j0     - join b with zeros         ['a','b',0,'c','d',0,'e']
  j@   - join a with that          ['w','a','b',0,'c','d',0,'e','x','a','b',0,'c','d',0,'e','y','a','b',0,'c','d',0,'e','z']
    ṣ0 - split at zeros            [['w','a','b'],['c','d'],['e','x','a','b'],['c','d'],['e','y','a','b'],['c','d'],['e','z']
                             (i.e.: ["wab","cd","exab","cd","eyab","cd","ez"])

If we needed to deal with any of Jelly's mixed lists (including those of any depth or shape) we could use this eight byter: j,©⁹jœṣ® which uses the paired arguments as the place-holder.


05AB1E, 11 bytes

Executed code is the same as this answer. I was extremely lucky all the required characters were in the 05AB1E codepage.

ø«O
Ëq¶
/Öí

Try it online!  Verify it!


Perl, 592 579 540 522 510 bytes

Under a sector!

($L,$C,$b)=unpack"W/aW/aB*",q!12346677889999 _|/\)(
'<,>.` PP¨
¨@T @ôäð˜SÀ§€fÌU`à¨Àäð'€GÖf`3 X½`fÍ5 s Î|½`gËÖ— $¡))õ™˜À8Sô BÁªx~O àÔýåËþÏÃÆ~O‡ŸY¬Àf s
½`À*õŸ/X8|½`p>^€<¯åkgúºÖ·ÓÊ¸°ªÀj® kª¸•p¸å\×   ô!;@c=map{$i<<=$_-$P if$_>$P;$P=$_;sprintf"%${l}b",$i++}$L=~/./g;while($b){$b=~s/^$c[$_]// and$g.=($C=~/./sg)[$_]for 0..$#c}$_=pop;s/-/:/;y/apm-/;</d;for$a(0..5){map{$o.=substr((split$/,$g)[$a],($x=-48+ord)*12,('6356766766099'=~/./g)[$x]+3)}/./g;$o.=$/}print$o

This program expects the time in the 2nd format ("12-34-am") as a commandline argument.

Commented:

($L,$C,$b) = unpack "W/aW/aB*",             # extract code lengths, chars, and bitstream
    q!12346677889999 _|/\)(
'<,>.` PP¨
¨@T @ôäð˜SÀ§€fÌU`à¨Àäð'€GÖf`3 X½`fÍ5 s Î|½`gËÖ— $¡))õ™˜À8Sô BÁªx~O àÔýåËþÏÃÆ~O‡ŸY¬Àf s
½`À*õŸ/X8|½`p>^€<¯åkgúºÖ·ÓÊ¸°ªÀj® kª¸•p¸å\×   ô!;
@c = map {                                  # reconstruct huffman prefix codes
    $i <<= $_-$P if $_ > $P;                # increase code size
    $P = $_;                                # keep track of previous code
    sprintf "%${l}b", $i++                  # append code as bitstring
}
$L=~/./g;                                   # canonical huffman prefix lengths
while ( $b ) {                              # decompress font
        $b =~ s/^$c[$_]//                   # match and strip prefix code
        and $g .= ($C=~/./sg)[$_]           # append char for prefix code
    for 0..$#c                              # iterate prefix codes in order
}                                           # luckily we can omit checking trailing bytes

$_ = pop;                                   # get cmdline arg "12-34-am"
s/-/:/;                                     # ord(':')-ord('0')=10
y/apm-/;</d;                                # 'a'-> chr(11), 'p' -> chr(12), strip -,m
for $a (0..5) {                             # iterate 6 output lines
    map {                                   # iterate each char in input
        $o .= substr(
            ( split $/, $g )[$a],           # grab line $a
            ( $x=-48+ord ) * 12,            # $x=glyph index, 12=glyph width
            ('6356766766099'=~/./g)[$x]+3   # glyph display width
        )
    } /./g;
    $o .= $/                                # append newline
}
print $o                                    # output

Note that due to character encoding issues, when you paste the above code in a file, the output may be somewhat malformed. So here's the golfed version, base-64 encoded. Copy it, and paste it into base64 -d > 510.pl:

KCRMLCRDLCRiKT11bnBhY2siVy9hVy9hQioiLHEhDjEyMzQ2Njc3ODg5OTk5DiBffC9cKSgKJzws
Pi5gFQBQAVACqAIgCqgBQBVUAqAFQAH05PAOmANTwBqngGbMBlVgHOAaqMDk8AcngEfWZmAzAA9Y
Ar1gZs0DNQBzoAHOBny9YGfL1g+XoAUkoSkp9ZmYDMAHOANT9A1Cwap4Bn5PABzgH9T9AeV/GBAB
y/7Pw8Z+Tx+Hn1mswGYAc6AKvWAMwCr1gZ8vWAc4Bny9YA5wPl6APK/lrWtn+rrWt9PKuAawBqrA
aq4AawGquAeVcA64AeVcAdcAAAABrfQhO0BjPW1hcHskaTw8PSRfLSRQIGlmJF8+JFA7JFA9JF87
c3ByaW50ZiIlJHtsfWIiLCRpKyt9JEw9fi8uL2c7d2hpbGUoJGIpeyRiPX5zL14kY1skX10vLyBh
bmQkZy49KCRDPX4vLi9zZylbJF9dZm9yIDAuLiQjY30kXz1wb3A7cy8tLzovO3kvYXBtLS87PC9k
O2ZvciRhKDAuLjUpe21hcHskby49c3Vic3RyKChzcGxpdCQvLCRnKVskYV0sKCR4PS00OCtvcmQp
KjEyLCgnNjM1Njc2Njc2NjA5OSc9fi8uL2cpWyR4XSszKX0vLi9nOyRvLj0kL31wcmludCRv

Here's the font I'm using. I've spaced the glyphs 12 characters apart
(the size of the am/pm) for easy indexing.

   ___        __          ___         ____        _  _        _____         __        ______       ___         ___
  / _ \      /_ |        |__ \       |___ \      | || |      | ____|       / /       |____  |     / _ \       / _ \      _
 | | | |      | |           ) |        __) |     | || |_     | |__        / /_           / /     | (_) |     | (_) |    (_)          __ _ _ __   _ __ _ __
 | | | |      | |          / /        |__ <      |__   _|    |___ \      | '_ \         / /       > _ <       \__, |     _          / _` | '  \ | '_ \ '  \
 | |_| |      | |         / /_        ___) |        | |       ___) |     | (_) |       / /       | (_) |        / /     (_)         \__,_|_|_|_|| .__/_|_|_|
  \___/       |_|        |____|      |____/         |_|      |____/       \___/       /_/         \___/        /_/                              |_|

This font is 592 bytes.
The previous approach used a few variations on RLE compression to reduce this to 353 bytes, at the cost of 78 bytes decompression code.
The Huffman coding compresses the font to 221 bytes at the cost of 154 bytes for the decompression algorithm.


How to slow down a drunkard on his way home

227934 (20x20)

My third attempt. Uses the same general approach as @stokastic with two paths to the exit. When the walker reaches the end of one path, it closes, requiring him to return to get to the end of the other path to exit. My improvement is to generate the paths as the walker progresses, so that whichever path he is progressing further along in the first half of the process will end up being longer than the other path.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>

#define DEBUG 0
#define ROUNDS 10000

#define Y 20
#define X 20
#define H (Y*2+1)
#define W (X*2+1)

int maze[H][W];
int scores[ROUNDS];

int x, y;

void print_maze(){
    char line[W+2];
    line[W+1]=0;
    for(int row=0;row<H;row++) {
        for(int col=0;col<W;col++) {
            switch(maze[row][col]) {
                case 0:
                    line[col]=' ';
                    break;
                case 1:
                    line[col]=row%2?'-':'|';
                    break;
                case 8:
                    line[col]=(row==y*2+1&&col==x*2+1)?'@':'?';
                    break;
                case 9:
                    line[col]=(row==y*2+1&&col==x*2+1)?'@':'*';
                    break;
            }
        }
        line[W]='\n';
        printf("%s",line);
    }
    printf("%d %d\n",y,x);
}

int main(){
    srand (time(NULL));
    long long total_turns = 0;
    for(int round=0;round<ROUNDS;round++) {
        for (int r=0;r<H;r++) {
            for (int c=0;c<W;c++) {
                maze[r][c]=0;
            }
        }
        maze[1][1]=9;
        maze[1][2]=1;
        maze[2][1]=1;
        maze[1][3]=8;
        maze[3][1]=8;
        int progress_l = 0;
        int progress_r = 0;
        int side = 0;
        int closed_exit = 0;
        x=0;
        y=0;
        if (DEBUG) print_maze();
        long long turn = 0;
        int in = 0;
        while (x!=X-1||y!=Y-1) {
            turn++;
            int r = y*2+1;
            int c = x*2+1;
            int dx=0, dy=0;
            if (DEBUG) {
                std::cin>>in;
                switch(in) {
                    case 0:
                        dy=-1; dx=0; break;
                    case 1:
                        dy=0; dx=1; break;
                    case 2:
                        dy=1; dx=0; break;
                    case 3:
                        dy=0; dx=-1; break;
                    default:
                        dy=0; dx=0; break;
                }
            } else {
                int exits = maze[r-1][c] + maze[r][c+1] + maze[r+1][c] + maze[r][c-1];
                int exit_choice = -1;
                do {
                    if (rand()%exits == 0) {
                        exit_choice = exits;
                        break;
                    } else {
                        exits--;
                    }
                }while(exits);

                --exits;

                if (maze[r-1][c]&&!dx&&!dy) {
                    if (exits) {
                        --exits;
                    } else {
                        dy = -1;
                        dx = 0;
                    }
                }
                if (maze[r][c+1]&&!dx&&!dy) {
                    if (exits) {
                        --exits;
                    } else {
                        dy = 0;
                        dx = 1;
                    }
                }
                if (maze[r+1][c]&&!dx&&!dy) {
                    if (exits) {
                        --exits;
                    } else {
                        dy = 1;
                        dx = 0;
                    }
                }
                if (maze[r][c-1]&&!dx&&!dy) {
                    if (exits) {
                        --exits;
                    } else {
                        dy = 0;
                        dx = -1;
                    }
                }
            }

            x+=dx;
            y+=dy;

            if(x==X-1 && y==Y-1) continue;

            if (x==0&&y==1) side=-1;
            if (x==1&&y==0) side=1;
            if (maze[y*2+1][x*2+1]==8) { // room needs another exit, maybe
                if (side==-1) { // left half of maze
                    if (y==1) { // top of a column
                        if (x%2) { // going up, turn right
                            maze[y*2+1][x*2+2]=1;
                            maze[y*2+1][x*2+3]=8;
                        } else { // going right, turn down
                            maze[y*2+2][x*2+1]=1;
                            maze[y*2+3][x*2+1]=8;
                        }
                    } else if (y==Y-1) { // bottom of a column
                        if (x%2 && x<(X-progress_r-3)) { // going right, turn up if there's room
                            maze[y*2+0][x*2+1]=1;
                            maze[y*2-1][x*2+1]=8;
                            progress_l=x+1;
                        } else { // going down or exiting, go right
                            if (x!=X-2 or closed_exit==1) {
                                maze[y*2+1][x*2+2]=1;
                                maze[y*2+1][x*2+3]=8;
                            } else {
                                closed_exit = -1;
                            }
                        }
                    } else { // in a column
                        if (maze[y*2+0][x*2+1]) { // going down
                            maze[y*2+2][x*2+1]=1;
                            maze[y*2+3][x*2+1]=8;
                        } else { // going up
                            maze[y*2+0][x*2+1]=1;
                            maze[y*2-1][x*2+1]=8;
                        }
                    }
                } else { // right half of maze
                    if (y==0) { // top row
                        if (x<X-1) { // go right
                            maze[y*2+1][x*2+2]=1;
                            maze[y*2+1][x*2+3]=8;
                        } else { // go down
                            maze[y*2+2][x*2+1]=1;
                            maze[y*2+3][x*2+1]=8;
                        }
                    } else if (y==Y-2) { // heading right to the exit
                        if (x<X-1) { // go right
                            maze[y*2+1][x*2+2]=1;
                            maze[y*2+1][x*2+3]=8;
                        } else { // go down
                            if (x!=X-1 or closed_exit==-1) {
                                maze[y*2+2][x*2+1]=1;
                                maze[y*2+3][x*2+1]=8;
                            } else {
                                closed_exit = 1;
                            }
                        }
                    } else if (y==Y-3) { // bottom of a column
                        if (x>progress_l+1) { // do we have room for another column?
                            if (!(x%2)&&y>1) { // going left, turn up
                                maze[y*2+0][x*2+1]=1;
                                maze[y*2-1][x*2+1]=8;
                            } else { // going down, turn left
                                maze[y*2+1][x*2+0]=1;
                                maze[y*2+1][x*2-1]=8;
                                progress_r=X-x-1;
                            }
                        } else { // abort, move down to escape row
                            maze[y*2+2][x*2+1]=1;
                            maze[y*2+3][x*2+1]=8;
                        }
                    } else if (y==1) { // top of a column
                        if (!(x%2)) { // going up, turn left
                            maze[y*2+1][x*2+0]=1;
                            maze[y*2+1][x*2-1]=8;
                        } else { // going left, turn down
                            maze[y*2+2][x*2+1]=1;
                            maze[y*2+3][x*2+1]=8;
                        }
                    } else { // in a column
                        if (maze[y*2+0][x*2+1]) { // going down
                            maze[y*2+2][x*2+1]=1;
                            maze[y*2+3][x*2+1]=8;
                        } else { // going up
                            maze[y*2+0][x*2+1]=1;
                            maze[y*2-1][x*2+1]=8;
                        }
                    }

                }
                maze[y*2+1][x*2+1]=9;
            }

            if (DEBUG) print_maze();
        }
        // print_maze();
        printf("turns:%lld\n",turn);
        scores[round] = turn;
        total_turns += turn;
    }
    printf("%d rounds in a %d*%d maze\n",ROUNDS,X,Y);
    long long avg = total_turns/ROUNDS;
    printf("average: % 10lld\n",avg);
    long long var = 0;
    for(int r=0;r<ROUNDS;r++){
        var += (scores[r]-avg)*(scores[r]-avg);
    }
    var/=ROUNDS;
    // printf("variance: %lld\n",var);
    int stddev=sqrt(var);
    printf("stddev:  % 10d\n",stddev);

}

output (with time):

...
turns:194750
turns:506468
turns:129684
turns:200712
turns:158664
turns:156550
turns:311440
turns:137900
turns:86948
turns:107134
turns:81806
turns:310274
100000 rounds in a 20*20 maze
average:     227934
stddev:      138349
real    10m54.797s
...

example of my maze, with roughly equal length halves to the path, showing the left/lower path cut off from the exit (bottom right):

  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 |  _   _   _   _   _   _   _   _   _  |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | | | | | | | | | | |
 | | | | | | | | | | |_| |_| |_| |_| |_|
 | | | | | | | | | |_ _ _ _ _ _ _ _ _ _
 |_| |_| |_| |_| |_ _ _ _ _ _ _ _ _ _  !

PS: I am aware of a very minor improvement to this algorithm that requires more clever code to generate a different shape for the two paths, staircases instead of consistent height zig zags.


JavaScript (ES6), 316 323 347

p=>[1,2,4].some(x=>(d=D(Q=[[x&1,x&2,x&4,0,0,0],...p.map(([x,y])=>[x*x,x*y,y*y,x,y,1])]))?[a,b,c]=Q.map((v,i)=>D(Q.map((r,j)=>(r=[...r],r[i]=x*!j,r)))/d):0,D=m=>m[1]?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0):m)&&(d=b*b-4*a*c)?d<0?!b&c==a?'Circle':'Ellipse':'Hyperbola':'Parabola'

Any language better suited for handling matrix and determinant should score better (APL, J, CJAM, Jelly)

References: General form of a conic, Five points determine a conic, System of linear equations, Determinant

In the cartesian plane, the general equation of a conic is

A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0

having A or B or C not equal to 0 (otherwise it's a straight line)

A ... F are six unknowns to be found. With five pairs of (x,y) we can build a linear system with five equations, and scaling remove one dimension. That is, we can set one of A,B or C to 1 if it's not 0 (and we know that at least one is not 0).

I build and try to solve 3 systems: first trying A=1. If not solvable then B=1, then C. (There could be a better way, but that's my best at the time)

Having the values of A,B,C we can classify the conic looking at the discriminant d=B*B-4*A*C

d == 0 -> parabola
d > 0 -> hyperbola
d < 0 -> ellipse, particularly (A == C and B == 0) -> circle

Less golfed

F=p=>(
  // Recursive function to find determinant of a square matrix
  D=m=>m[1]
    ?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0)
    :m,
  // Try 3 linear systems, coefficients in Q
  // Five equation made from the paramaters in p
  // And a first equation with coefficient like k,0,0,0,0,0,1 (example for A)
  [1,2,4].some(
    x => (
      // matrix to calc the determinant, last coefficient is missing at this stage
      Q = [
        [x&1, x&2, x&4, 0,0,0] // first one is different
        // all other equations built from the params
        ,...p.map( ([x,y]) => [x*x, x*y, y*y, x, y, 1] )
      ],
      d = D(Q), // here d is the determinant
      d && ( // if solvable  then d != 0
        // add missing last coefficient to Q
        // must be != 0 for the first row, must be 0 for the other
        Q.map( r=> (r.push(x), x=0) ),
        // solve the system (Cramer's rule), I get all values for A...F but I just care of a,b,c
        [a,b,c] = Q.map((v,i)=>D(Q.map(r=>(r=[...r],r[i]=r.pop(),r))) / d),
        d = b*b - 4*a*c, // now reuse d for discriminant
        d = d<0 ? !b&c==a ? 'Circle' : 'Ellipse' // now reuse d for end result
        : d ? 'Hyperbola' : 'Parabola'
      ) // exit .some if not 0
    ), d // .some exit with true, the result is in d
  )
)

Test

F=p=>[1,2,4].some(x=>(d=D(Q=[[x&1,x&2,x&4,0,0,0],...p.map(([x,y])=>[x*x,x*y,y*y,x,y,1])]))?[a,b,c]=Q.map((v,i)=>D(Q.map((r,j)=>(r=[...r],r[i]=x*!j,r)))/d):0,D=m=>m[1]?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0):m)&&(d=b*b-4*a*c)?d<0?!b&c==a?'Circle':'Ellipse':'Hyperbola':'Parabola'

console.log=(...x)=>O.textContent+=x+'\n'

;[
 [[0, 0], [1, 5], [2, 3], [4, 8], [9, 2]]
,[[1.2, 5.3],[4.1, 5.6], [9.1, 2.5], [0, 1], [4.2, 0]]
,[[5, 0], [4, 3], [3, 4], [0, 5], [0, -5]]
,[[1, 0], [0, 1], [2, 1], [3, 4], [4, 9]]
].forEach(t=>console.log(t.join`|`+' => '+F(t)))


Analog clock, JS

Analog clock? Challenge accepted.

Golfed/Obfuscated into the zone of not-exactly-understanding-what-it-does-even-after-reading-through-a-few-times:

for(var a=new Date,b=a.getMinutes(),c=a.getHours()+b/60,d=[],e=Math.round,f=Math.PI,g=Math.sin,h=Math.cos,k=0;11>k;k++)d[k]=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(var l=0;l<f;l+=0.05){var m=e(17/11*5*h(2*l)),n=e(5*g(2*l));d[n+5][m+8]=1}for(var p=0;2.5>=p;p+=0.5){var q=e(17/11*p*h(-(c*f/6)+f/2)),r=e(p*g(-(c*f/6)+f/2));d[r+5][q+8]=2}for(p=0;4.5>=p;p+=0.5){var s=e(17/11*p*h(-(b*f/30)+f/2)),t=e(p*g(-(b*f/30)+f/2));d[t+5][s+8]=3}d[5][8]=1;a="";for(k=d.length-1;-1<k;k--)a+=d[k].join("").replace(/0/g,".").replace(/1/g,"O").replace(/2/g,"h").replace(/3/g,"m")+"\n";console.log(a);

Outputs something like:

.....OOOOOOO......
...OO.......OO....
.OO...........O...
.O...h.........O..
O.....hh.......OO.
O......hOmmmmmmmO.
OO.............OO.
.O.............O..
.OO...........O...
...OO.......OO....
.....OOOOOOO......

...yeah, guess what time it is here. (Hint hint not AM)

Human readable version:

var date = new Date();
var min = date.getMinutes();
var hrs = date.getHours()+(b/60); // set up date
var a=[];
for(var i=0;i<11;i++)a[i]=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; // set up array
function putX(x,y,i){a[y+5][x+8]=i;} // just for source neatness; this is expanded out in the obfuscated version
for(var p=0;p<Math.PI;p+=0.05){ // draw clock border
    putX(Math.round((17/11)*5*Math.cos(2*p)), Math.round(5*Math.sin(2*p)), 1);
}
for(var p=0;p<=2.5;p+=0.5){ // draw hour hand (transforming the angle to work)
    putX(Math.round((17/11)*p*Math.cos(-(hrs*2*Math.PI/12)+(Math.PI/2))), Math.round(p*Math.sin(-(hrs*2*Math.PI/12)+(Math.PI/2))), 2);
}

for(var p=0;p<=4.5;p+=0.5){ // draw the minute hand like the hour hand
    putX(Math.round((17/11)*p*Math.cos(-(min*2*Math.PI/60)+(Math.PI/2))), Math.round(p*Math.sin(-(min*2*Math.PI/60)+(Math.PI/2))), 3);
}

putX(0,0,1); // set the center point

date=""; // reuse date for maximum confusion :)
for(var i=a.length-1;i>-1;i--)date+=a[i].join("").replace(/0/g, ".").replace(/1/g, "O").replace(/2/g, "h").replace(/3/g, "m")+"\n"; // output ASCII art to string
console.log(date); // output string to console


Python 3, 389

import itertools as I
S=sorted
P=I.product
def C(n,m,k):
 Q=[((-1,)*(n-m)+(0,)*m,)]
 for i in' '*(k-1):Q=set(tuple(S(q+(v,)))for q in Q for v in P(*[(-1,0,1)]*n)if sum(map(abs,v))==n-m if not v in q and any(sum((a!=b)*(1+2*a*b)for a,b in zip(v,u))==2for u in q))
 return sum(all(S(q)<=S(zip(*r))for X in I.permutations(zip(*q))for r in P(*((p,tuple(-x for x in p)) for p in X)))for q in Q)

Try it online!

Basically just finds all connected polyominos, and discards ones which can be rotated into a lexicographically smaller polyomino, with rotations being brute-forced.

Can definitely be improved but it's my bedtime.


2 Languages: Python, Ruby; 33 29 bytes

Here is another way to do Python and Ruby, that's a bit shorter than the one in the challenge:

Python:   print'^;puts"^.*?\\42#\\73"#'
prints:   ^;puts"^.*?\42#\73"#

Ruby:     puts"^.*?\42#\73"#print'^;puts"^.*?\\42#\\73"#'
prints:   ^.*?"#;

It shouldn't be too difficult to add PHP into the mix.


anyway.. here is the 3D answer
Mathematica, 15 bytes
#~MengerMesh~3&

new built-in in Mathematica 11.1
input

3

output


Japt, 13 bytes

¸
mÎ¬
VøUÎ ©V

Try it online!


Perl 6, 17 bytes

{[>](@_)+[>=] @_}

Monotone strictly decreasing: 2
Monotone non-increasing: 1
Other: 0

Expanded:

{            # bare block lambda with implicit parameter list ｢@_｣
  [>]( @_ )  # reduce using ｢&infix:« > »｣
  +
  [>=] @_    # reduce using ｢&infix:« >= »｣
}


Convert ciphered Roman numerals to Arabic decimals

Python 2, 415 444 440 419 416 bytes

There aren't all that many Roman numerals, after all. This script creates all of them and checks all permutations of the input, then returns the smallest match.

a=raw_input()
g=range
b=list(set(a))+[' ']*9
from itertools import*
c=[]
s={}
u=1000
for i in g(10*u):
 t,f=(10*u,9*u,5*u,4*u,u,900,500,400,100,90,50,40,10,9,5,4,1),i;r=""
 for j in g(17):k=i/t[j];r+=('W MW Q MQ M CM D CD C XC L XL X IX V IV I').split()[j]*k;i-=t[j]*k
 s[r]=f
for i in permutations(b[:9]):
 r=''
 for j in a:r+='IVXLCMQWE'[i.index(j)]
 if r in s:c+=[s[r]]
print c and min(c)or'%s failed Roman numeral test'%a