14
\$\begingroup\$

Overview

Given a list of fireworks a-z and times 3-78, arrange them with fuses to make them all light up at the correct time.

A line of input is given as space separated letters and numbers:

a 3 b 6 c 6 d 8 e 9 f 9

That example shows that firework a need to light at time 3, b and c both at 6, d at 8, with e and f both at 9. Each line corresponds to one map.

Output is a fuse/firework map for each line, using the symbols |- to show fuses and the letters to show fireworks.

A - fuse connects to fuses and fireworks directly left/right of it, while a | fuse connects with those above/below. For example, the fuses || are not connected, and -| are.

For example, two possible answers to the above are:

---a        ---------f
  |         |||   ||
  |-c       |||   de
--|--d      a||
| b |        |c
f   e        b

All fuse maps should start with a single - in the upper left corner. That is the point where you light the fuse. Each character of fuse takes one second to burn. As you can see, the a is reached in three seconds in both diagrams, b in six, etc.

Now, both of the maps given above are valid for the input given, but one is clearly more efficient. The left one only uses 13 units of fuse, while the right one takes 20.

Fuses do not burn through fireworks! So, for input a 3 b 5, this is not valid:

---a--b

Challenge

Your goal is to minimize the amount of fuse used over all test cases. Scoring is very simple, the total units of fuse used.

If you cannot produce a map for a test case, whether it's an impossible case or not, the score for that case is the sum of all times (41 for the example above).

In the case of a tie, the scoring is modified so that the most compact maps win. The tiebreak score is the area of the bounding box of each map. That is, the length of the longest line times the number of lines. For "impossible" maps this is the square of the largest number (81 for the example above).

In the case that submissions tie both of these scoring methods, the tie goes to the earlier entry/edit.

Your program must be deterministic, for verification purposes.

Test Cases

There are 250 test cases, located here. Each has between 4 and 26 fireworks. The minimum fuse time for a firework is 3. The fireworks in each case are "sorted" by time and letter, meaning b will never light before a.

When posting, please include your full program, your total score, and the resulting map for (at least) the first test case given in the file:

a 6 b 8 c 11 d 11 e 11 f 11 g 12 h 15 i 18 j 18 k 21 l 23 m 26 n 28 o 28 p 30 q 32 r 33 s 33 t 34 
\$\endgroup\$
8
  • \$\begingroup\$ Can arbitrarily many fireworks go off at the same time? \$\endgroup\$
    – Ingo Bürk
    Aug 8, 2014 at 18:27
  • \$\begingroup\$ Basically, yes. I haven't looked for the largest instance of that in my test cases, but I know it's at least four. The time between two fuses is rand.nextInt(5)%4, so 40% chance of 0, and 20% for each 1,2,3. \$\endgroup\$
    – Geobits
    Aug 8, 2014 at 18:30
  • \$\begingroup\$ Just an suggestion: I'd use a '+' for where the fuses connect or change direction, that would make the output graphics IMHO much more intuitive! \$\endgroup\$
    – flawr
    Aug 8, 2014 at 19:01
  • \$\begingroup\$ @flawr I'll allow that, provided it's done in a way that doesn't change the score. For instance, -+- in place of --- would not automatically connect fireworks above/below, there still has to be a | above/below to connect it to a firework. -+- in place of -|- is okay as it is. \$\endgroup\$
    – Geobits
    Aug 8, 2014 at 19:41
  • \$\begingroup\$ Are all of the test cases solvable? E.g. if there were five or more fireworks to go off at time 3, I don't think you could fit them all near enough to the start. Likewise, you might be able to fit all of them but they might block the way to the outside for later fireworks. \$\endgroup\$ Aug 9, 2014 at 12:15

2 Answers 2

3
\$\begingroup\$

C++

Total Length: 9059, Total Area: 27469, Failures: 13.

Note: Score includes failure penalties.


Sample output:

a 6 b 8 c 11 d 11 e 11 f 11 g 12 h 15 i 18 j 18 k 21 l 23 m 26 n 28 o 28 p 30 q 32 r 33 s 33 t 34 
------ae  
     | |  
     |---c
     b||-g
      |d| 
      f | 
    i---| 
  k---| h 
   |  j   
   |---m  
   l  | t 
     o-n| 
      |s-r
      |-| 
      p q 
Length: 39, Area: 150.

a 6 b 6 c 6 d 6 e 6 f 6 g 6 h 8 i 9 j 9 k 9 l 12 m 12 n 13 o 14 p 15 q 15 r 15 s 17 t 17 u 17 v 17 w 17 x 20 y 23 z 26 
------a  n|--w 
|d-||---k|-o|  
| g|b  |--m --x
|-|c    ||--r| 
||f     l|-q | 
||--j u--|--s|-
e|-i    |p|  y|
 h      v t  z-
Length: 56, Area: 120.

Full output: http://pastebin.com/raw.php?i=spBUidBV


Don't you just love brute-force solutions? This is a little more than a simple backtracking algorithm: our tireless worker moves around the map, placing fuses and fireworks as necessary, while testing all possible moves at any point. Well, almost---we do restrict the set of moves and abandon nonoptimal states early so that it doesn't take unbearably long (and, in particular, so that it terminates.) Special care is taken not to create any cycles or unintended paths and not to go back the same way we came, so it's guaranteed that we don't visit the same state twice. Even so, finding an optimal solution can take a while, so we eventually give up on optimizing a solution if it takes too long.

This algorithm still has some headroom. For one thing, better solutions can be found by increasing the FRUSTRATION parameters. There's no competition ATM but these numbers can be cranked up if and when...

Compile with: g++ fireworks.cpp -ofireworks -std=c++11 -pthread -O3.

Run with: ./fireworks.

Reads input from STDIN and writes output to STDOUT (possibly out-of-order.)

/* Magic numbers */
#define THREAD_COUNT 2
/* When FRUSTRATION_MOVES moves have passed since the last solution was found,
 * the last (1-FRUSTRATION_STATES_BACKOFF)*100% of the backtracking states are
 * discarded and FRUSTRATION_MOVES is multiplied by FRUSTRATION_MOVES_BACKOFF.
 * The lower these values are, the faster the algorithm is going to give up on
 * searching for better solutions. */
#define FRUSTRATION_MOVES 1000000
#define FRUSTRATION_MOVES_BACKOFF 0.8
#define FRUSTRATION_STATES_BACKOFF 0.5

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <thread>
#include <mutex>
#include <string>
#include <sstream>
#include <cassert>

using namespace std;

/* A tile on the board. Either a fuse, a firework, an empty tile or an
 * out-of-boudns tile. */
struct tile {
    /* The tile's value, encoded the "obvious" way (i.e. '-', '|', 'a', etc.)
     * Empty tiles are encoded as '\0' and OOB tiles as '*'. */
    char value;
    /* For fuse tiles, the time at which the fuse is lit. */
    int time;

    operator char&() { return value; }
    operator const char&() const { return value; }

    bool is_fuse() const { return value == '-' || value == '|'; }
    /* A tile is vacant if it's empty or OOB. */
    bool is_vacant() const { return !value || value == '*'; }

    /* Prints the tile. */
    template <typename C, typename T>
    friend basic_ostream<C, T>& operator<<(basic_ostream<C, T>& os,
                                            const tile& t) {
        return os << (t.value ? t.value : ' ');
    }
};
/* Fireworks have the same encoding as tiles. */
typedef tile firework;
typedef vector<firework> fireworks;

/* The fuse map. It has physical dimensions (its bounding-box) but is
 * conceptually infinite (filled with empty tiles.) */
class board {
    /* The tiles, ordered left-to-right top-to-bottom. */
    vector<tile> p_data;
    /* The board dimensions. */
    int p_width, p_height;
    /* The total fuse length. */
    int p_length;

public:
    board(): p_width(0), p_height(0), p_length(0) {}

    /* Physical dimensions. */
    int width() const { return p_width; }
    int height() const { return p_height; }
    int area() const { return width() * height(); }
    /* Total fuse length. */
    int length() const { return p_length; }

    /* Returns the tile at (x, y). If x or y are negative, returns an OOB
     * tile. */
    tile get(int x, int y) const {
        if (x < 0 || y < 0)
            return {'*'};
        else if (x >= width() || y >= height())
            return {'\0'};
        else
            return p_data[y * width() + x];
    }
    /* Sets the tile at (x, y). x and y must be nonnegative and the tile at
     * (x, y) must be empty. */
    board& set(int x, int y, const tile& t) & {
        assert(x >= 0 && y >= 0);
        assert(!get(x, y));
        if (x >= width() || y >= height()) {
            int new_width = x >= width() ? x + 1 : width();
            int new_height = y >= height() ? y + 1 : height();
            vector<tile> temp(new_width * new_height, {'\0'});
            for (int l = 0; l < height(); ++l)
                copy(
                    p_data.begin() + l * width(),
                    p_data.begin() + (l + 1) * width(),
                    temp.begin() + l * new_width
                );
            p_data.swap(temp);
            p_width = new_width;
            p_height = new_height;
        }
        p_data[y * width() + x] = t;
        if (t.is_fuse())
            ++p_length;
        return *this;
    }
    board&& set(int x, int y, const tile& t) && { return move(set(x, y, t)); }

    /* Prints the board. */
    template <typename C, typename T>
    friend basic_ostream<C, T>& operator<<(basic_ostream<C, T>& os,
                                            const board& b) {
        for (int y = 0; y < b.height(); ++y) {
            for (int x = 0; x < b.width(); ++x)
                os << b.get(x, y);
            os << endl;
        }
        return os;
    }
};

/* A state of the tiling algorithm. */
struct state {
    /* The current board. */
    board b;
    /* The next firework to tile. */
    fireworks::const_iterator fw;
    /* The current location. */
    int x, y;
    /* The current movement direction. 'N'orth 'S'outh 'E'ast, 'W'est or
     * 'A'ny. */
    char dir;
};

/* Adds a state to the state-stack if its total fuse length and bounding-box
 * area are not worse than the current best ones. */
void add_state(vector<state>& states, int max_length, int max_area,
                state&& new_s) {
    if (new_s.b.length() < max_length ||
        (new_s.b.length() == max_length && new_s.b.area() <= max_area)
    )
        states.push_back(move(new_s));
}
/* Adds the state after moving in a given direction, if it's a valid move. */
void add_movement(vector<state>& states, int max_length, int max_area,
                    const state& s, char dir) {
    int x = s.x, y = s.y;
    char parallel_fuse;
    switch (dir) {
    case 'E': if (s.dir == 'W') return; ++x; parallel_fuse = '|'; break;
    case 'W': if (s.dir == 'E') return; --x; parallel_fuse = '|'; break;
    case 'S': if (s.dir == 'N') return; ++y; parallel_fuse = '-'; break;
    case 'N': if (s.dir == 'S') return; --y; parallel_fuse = '-'; break;
    }
    const tile t = s.b.get(s.x, s.y), nt = s.b.get(x, y);
    assert(t.is_fuse());
    if (nt.is_fuse() && !(t == parallel_fuse && nt == parallel_fuse))
        add_state(states, max_length, max_area, {s.b, s.fw, x, y, dir});
}
/* Adds the state after moving in a given direction and tiling a fuse, if it's a
 * valid move. */
void add_fuse(vector<state>& states, int max_length, int max_area,
                const state& s, char dir, char fuse) {
    int x = s.x, y = s.y;
    int sgn;
    bool horz;
    switch (dir) {
    case 'E': ++x; sgn = 1; horz = true; break;
    case 'W': --x; sgn = -1; horz = true; break;
    case 'S': ++y; sgn = 1; horz = false; break;
    case 'N': --y; sgn = -1; horz = false; break;
    }
    if (s.b.get(x, y))
        /* Tile is not empty. */
        return;
    /* Make sure we don't create cycles or reconnect a firework. */
    const tile t = s.b.get(s.x, s.y);
    assert(t.is_fuse());
    if (t == '-') {
        if (horz) {
            if (fuse == '-') {
                if (!s.b.get(x + sgn, y).is_vacant() ||
                    s.b.get(x, y - 1) == '|' ||
                    s.b.get(x, y + 1) == '|')
                    return;
            } else {
                if (s.b.get(x + sgn, y) == '-' ||
                    !s.b.get(x, y - 1).is_vacant() ||
                    !s.b.get(x, y + 1).is_vacant())
                    return;
            }
        } else {
            if (!s.b.get(x, y + sgn).is_vacant() ||
                s.b.get(x - 1, y) == '-' ||
                s.b.get(x + 1, y) == '-')
                return;
        }
    } else {
        if (!horz) {
            if (fuse == '|') {
                if (!s.b.get(x, y + sgn).is_vacant() ||
                    s.b.get(x - 1, y) == '-' ||
                    s.b.get(x + 1, y) == '-')
                    return;
            } else {
                if (s.b.get(x, y + sgn) == '|' ||
                    !s.b.get(x - 1, y).is_vacant() ||
                    !s.b.get(x + 1, y).is_vacant())
                    return;
            }
        } else {
            if (!s.b.get(x + sgn, y).is_vacant() ||
                s.b.get(x, y - 1) == '|' ||
                s.b.get(x, y + 1) == '|')
                return;
        }
    }
    /* Ok. */
    add_state(
        states,
        max_length,
        max_area,
        {board(s.b).set(x, y, {fuse, t.time + 1}), s.fw, x, y, dir}
    );
}
/* Adds the state after adding a firework at the given direction, if it's a
 * valid move. */
void add_firework(vector<state>& states, int max_length, int max_area,
                    const state& s, char dir) {
    int x = s.x, y = s.y;
    int sgn;
    bool horz;
    switch (dir) {
    case 'E': ++x; sgn = 1; horz = true; break;
    case 'W': --x; sgn = -1; horz = true; break;
    case 'S': ++y; sgn = 1; horz = false; break;
    case 'N': --y; sgn = -1; horz = false; break;
    }
    if (s.b.get(x, y))
        /* Tile is not empty. */
        return;
    /* Make sure we don't run into an undeliberate fuse. */
    if (horz) {
        if (s.b.get(x + sgn, y) == '-' || s.b.get(x, y - 1) == '|' ||
            s.b.get(x, y + 1) == '|')
            return;
    } else {
        if (s.b.get(x, y + sgn) == '|' || s.b.get(x - 1, y) == '-' ||
            s.b.get(x + 1, y) == '-')
            return;
    }
    /* Ok. */
    add_state(
        states,
        max_length,
        max_area,
        /* After adding a firework, we can move in any direction. */
        {board(s.b).set(x, y, {*s.fw}), s.fw + 1, s.x, s.y, 'A'}
    );
}
void add_possible_moves(vector<state>& states, int max_length, int max_area,
                        const state& s) {
    /* We add the new states in reverse-desirability order. The most
     * (aesthetically) desirable states are added last. */

    const tile t = s.b.get(s.x, s.y);
    assert(t.is_fuse());

    /* Move in all (possible) directions. */
    for (char dir : "WENS")
        if (dir) add_movement(states, max_length, max_area, s, dir);

    /* If the fuse is too short for the next firework, keep adding fuse. */
    if (t.time < s.fw->time) {
        if (t == '-') {
            add_fuse(states, max_length, max_area, s, 'N', '|');
            add_fuse(states, max_length, max_area, s, 'S', '|');
            add_fuse(states, max_length, max_area, s, 'W', '|');
            add_fuse(states, max_length, max_area, s, 'W', '-');
            add_fuse(states, max_length, max_area, s, 'E', '|');
            add_fuse(states, max_length, max_area, s, 'E', '-');
        } else {
            add_fuse(states, max_length, max_area, s, 'W', '-');
            add_fuse(states, max_length, max_area, s, 'E', '-');
            add_fuse(states, max_length, max_area, s, 'N', '-');
            add_fuse(states, max_length, max_area, s, 'N', '|');
            add_fuse(states, max_length, max_area, s, 'S', '-');
            add_fuse(states, max_length, max_area, s, 'S', '|');
        }
    } else if (t.time == s.fw->time) {
        /* If we have enough fuse for the next firework, place the firework (if
         * possible) and don't add more fuse, or else we'll never finish... */
        if (t == '-') {
            add_firework(states, max_length, max_area, s, 'W');
            add_firework(states, max_length, max_area, s, 'E');
        } else {
            add_firework(states, max_length, max_area, s, 'N');
            add_firework(states, max_length, max_area, s, 'S');
        }
    }
}

void thread_proc(mutex& lock, int& total_length, int& total_area,
                    int& failures) {
    fireworks fw;
    vector<state> states;

    while (true) {
        /* Read input. */
        string input;
        {
            lock_guard<mutex> lg(lock);

            while (!cin.eof() && input.empty())
                getline(cin, input);
            if (input.empty())
                break;
        }
        fw.clear();
        int length = 0, area;
        {
            stringstream is;
            is << input;
            while (!is.eof()) {
                char c;
                int t;
                if (is >> c >> t) {
                    /* Fireworks must be sorted by launch time. */
                    assert(fw.empty() || t >= fw.back().time);
                    fw.push_back({c, t});
                    length += t;
                }
            }
            assert(!fw.empty());
            area = fw.back().time * fw.back().time;
        }

        /* Add initial state. */
        states.push_back({board().set(0, 0, {'-', 1}), fw.begin(), 0, 0, 'A'});

        board solution;
        int moves = 0;
        int frustration_moves = FRUSTRATION_MOVES;

        while (!states.empty()) {
            /* Check for solutions (all fireworks consumed.) */
            while (!states.empty() && states.back().fw == fw.end()) {
                state& s = states.back();
                /* Did we find a better solution? */
                if (solution.area() == 0 || s.b.length() < length ||
                    (s.b.length() == length && s.b.area() < area)
                ) {
                    solution = move(s.b);
                    moves = 0;
                    length = solution.length();
                    area = solution.area();
                }
                states.pop_back();
            }

            /* Expand the top state. */
            if (!states.empty()) {
                state s = move(states.back());
                states.pop_back();
                add_possible_moves(states, length, area, s);
            }

            /* Getting frustrated? */
            ++moves;
            if (moves > frustration_moves) {
                /* Get rid of some data. */
                states.erase(
                    states.begin() + states.size() * FRUSTRATION_STATES_BACKOFF,
                    states.end()
                );
                frustration_moves *= FRUSTRATION_MOVES_BACKOFF;
                moves = 0;
            }
        }

        /* Print solution. */
        {
            lock_guard<mutex> lg(lock);

            cout << input << endl;

            if (solution.area())
                cout << solution;
            else {
                cout << "FAILED!" << endl;
                ++failures;
            }

            cout << "Length: " << length <<
                    ", Area: " << area <<
                    "." << endl << endl;
            total_length += length;
            total_area += area;
        }
    }
}

int main(int argc, const char* argv[]) {
    thread threads[THREAD_COUNT];
    mutex lock;
    int total_length = 0, total_area = 0, failures = 0;

    for (int i = 0; i < THREAD_COUNT; ++i)
        threads[i] = thread(thread_proc, ref(lock), ref(total_length),
                            ref(total_area), ref(failures));
    for (int i = 0; i < THREAD_COUNT; ++i)
        threads[i].join();

    cout << "Total Length: " << total_length <<
            ", Total Area: " << total_area <<
            ", Failures: " << failures <<
            "." << endl;
}

Python

Total Length: 17387, Total Area: 62285, Failures: 44.


Sample output:

a 6 b 8 c 11 d 11 e 11 f 11 g 12 h 15 i 18 j 18 k 21 l 23 m 26 n 28 o 28 p 30 q 32 r 33 s 33 t 34
------a                
     |----f            
     |---c             
     b|||---h          
      |dg  |           
      e    |-j         
           |---k       
           i  |        
              |---m    
              l  |-o   
                 |--p  
                 n |--s
                   |-r 
                   q|  
                    t  
Length: 45, Area: 345.

Full output: http://pastebin.com/raw.php?i=mgiqXCRK


For reference, here's a much simpler approach. It tries to connect fireworks to a single main fuse line, creating a "staircase" shape. If a firework can't connect to the main line directly (which happens when two or more fireworks light at the same time) it traces back the main line looking for a point where it can branch perpendicularly down or to the right (and fails if no such point exists.)

Unsurprisingly, it does worse than the brute-force solver, but not by a huge margin. Honestly, I expected the difference to be somewhat bigger.

Run with: python fireworks.py.

from __future__ import print_function
import sys

total_length = total_area = failures = 0

for line in sys.stdin:
    # Read input.
    line = line.strip()
    if line == "": continue
    fws = line.split(' ')
    # The fireworks are a list of pairs of the form (<letter>, <time>).
    fws = [(fws[i], int(fws[i + 1])) for i in xrange(0, len(fws), 2)]

    # The board is a dictionary of the form <coord>: <tile>.
    # The first tile marks the "starting point" and is out-of-bounds.
    board = {(-1, 0): '*'}
    # The tip of the main "staircase" fuse.
    tip_x, tip_y = -1, 0
    tip_time = 0
    # We didn't fail. Yet...
    failed = False

    for (fw, fw_time) in fws:
        dt = fw_time - tip_time
        # Can we add the firework to the main fuse line?
        if dt > 0:
            # We can. Alternate the direction to create a "staircase" pattern.
            if board[(tip_x, tip_y)] == '-':    dx, dy = 0, 1; fuse = '|'
            else:                               dx, dy = 1, 0; fuse = '-'
            x, y = tip_x, tip_y
            tip_x += dt * dx
            tip_y += dt * dy
            tip_time += dt
        else:
            # We can't. Trace the main fuse back until we find a point where we
            # can thread, or fail if we reach the starting point.
            x, y = tip_x, tip_y
            while board[(x, y)] != '*':
                horz = board[(x, y)] == '-'
                if horz:    dx, dy = 0, 1; fuse = '|'
                else:       dx, dy = 1, 0; fuse = '-'
                if dt > 0 and (x + dx, y + dy) not in board: break
                if horz:    x -= 1
                else:       y -= 1
                dt += 1
            if board[(x, y)] == '*':
                failed = True
                break
        # Add the fuse and firework.
        for i in xrange(dt):
            x += dx; y += dy
            board[(x, y)] = fuse
        board[(x + dx, y + dy)] = fw

    # Print output.
    print(line)
    if not failed:
        max_x, max_y = (max(board, key=lambda p: p[i])[i] + 1 for i in (0, 1))
        for y in xrange(max_y):
            for x in xrange(max_x):
                print(board.get((x, y), ' '), end = "")
            print()
        length = len(board) - len(fws) - 1
        area = max_x * max_y
    else:
        print("FAILED!")
        failures += 1
        length = sum(map(lambda fw: fw[1], fws))
        area = fws[-1][1] ** 2
    print("Length: %d, Area: %d.\n" % (length, area))
    total_length += length; total_area += area

print("Total Length: %d, Total Area: %d, Failures: %d." %
        (total_length, total_area, failures))
\$\endgroup\$
2
  • \$\begingroup\$ Out of curiosity, how long does this take to complete with the current parameters? \$\endgroup\$
    – Geobits
    Aug 9, 2014 at 16:11
  • \$\begingroup\$ @Geobits: It's machine-dependent, obviously, and I didn't watch too closely, but I think about twenty minutes, give or take. \$\endgroup\$
    – DarwinBot
    Aug 9, 2014 at 16:16
1
\$\begingroup\$

Python3

Total length: 8343; Failures: 4; Total time: ~140.34 seconds

import collections, re, time

def shadow(board):
    return [["#" if b not in ['|', '-', ' '] else b for b in i] for i in board]

def full_length(s):
    return len(re.findall('\-|\|', s))
  
def adjust_board_size(_x, _y, board, mappings, cells):
    if _y < 0:
        _y = 0
        board = [[' ']+i for i in board]
        mappings = {(x, y+1):b for (x, y), b in mappings.items()}
        cells = [(x, y + 1) for x, y in cells]

    if _x < 0:
        _x = 0
        board = [[' ' for _ in board[0]]] + board
        mappings = {(x + 1, y):b for (x, y), b in mappings.items()}
        cells = [(x + 1, y) for x, y in cells]
    
    if _x == len(board):
        board = board + [[' ' for _ in board[0]]]

    if _y == len(board[0]):
        board = [i + [' '] for i in board]

    return _x, _y, eval(str(board)), eval(str(mappings)), eval(str(cells))

def draw_path(f, x_y, dist, d, depth, board, mappings, cells) -> iter:
    q = [(*x_y, *i, 0, board, mappings, cells, None) for i in d[:depth]]
    o_dist = mappings[x_y]
    while q:
        x, y, X, Y, prog, board, mappings, cells, smb = q.pop(0)
        if prog == dist:
            _x, _y = x + X, y + Y
            _x, _y, board, mappings, cells = adjust_board_size(_x, _y, board, mappings, cells)
            if (_x, _y) not in cells:
                cells.append((_x, _y))
                board[_x][_y] = f
                yield board, mappings, cells

            continue

        if smb is None:
            q.append((x, y, X, Y, prog, board, mappings, cells, '|' if board[x][y] == '-' else '-'))
            continue
        
        if (x+X, y+Y) in cells:
            q.extend([(x, y, X, Y, prog, board, mappings, cells, None) for X, Y in [(0, 1), (0, -1), (1, 0), (-1, 0)] if (x + X, y + Y) not in cells and x + X >= 0])
            continue

        _x, _y = x + X, y + Y
        _x, _y, board, mappings, cells = adjust_board_size(_x, _y, board, mappings, cells)
        prog += 1
        board[_x][_y] = smb
        mappings[(_x, _y)] = o_dist + prog
        cells.append((_x, _y))
        q.append((_x, _y, X, Y, prog, board, mappings, cells, smb))


def solver(t, depth = 3, option_depth = 2, board_depth = 3, breadth = -1) -> None:
    t = t.split()
    t = {t[i]:int(t[i+1]) for i in range(0, len(t), 2)}
    u_t = {b:a for a, b in t.items()}
    q = collections.deque([([['-' for _ in range(a)]+[b]], (mp:={(0,i):i+1 for i in range(a)}), [*mp], {*t}-{b})for a, b in [*u_t.items()][:len(u_t) if breadth == -1 else breadth]])
    seen = []
    results = []
    best_score = None
    while q:
        board, mappings, cells, f_works = q.popleft()
        #print('\n'.join(map(''.join, board)))
        if not f_works:
            results.append((board, mappings))
            best_score = len(mappings) if best_score is None else min(len(mappings), best_score)
            #print('got solution!')
            continue
        
        options = collections.defaultdict(list)
        for f in f_works:
            for (x, y), v in mappings.items():
                if (d:=[(X, Y) for X, Y in [(0, 1), (0, -1), (1, 0), (-1, 0)] if (x+X, y+Y) not in cells and ((board[x][y] == '-' and X) or (board[x][y] == '|' and Y))]):
                    if v + 1 == t[f]:
                        options[f].append(((x, y), 1, d))

                    elif v < t[f]:
                        options[f].append(((x, y), t[f] - v, d))
        
        #print(sorted([(a, min(b, key=lambda x:x[1])) for a, b in options.items()], key=lambda x:x[1][1]))
        #TODO: group on ties for best performance (include a suboptimal?)
        for f, ((x, y), dist, d), B in sorted([(a, min(b, key=lambda x:x[1]), sum(j[1] == min(b, key=lambda x:x[1]) for j in b)) for a, b in options.items()], key=lambda x:x[1][1])[:option_depth]:
            #print(B)
            if best_score is None or len(mappings) + dist < best_score:
                all_boards = []
                for _board, _mappings, _cells in draw_path(f, (x, y), dist, d, depth, board, mappings, cells):
                    if (S:=shadow(_board)) not in seen:
                        all_boards.append((_board, _mappings, _cells, f_works - {f}, S))

                for _board, _mappings, _cells, _f_works, S in all_boards[:board_depth]:
                    seen.append(S)
                    q.appendleft((_board, _mappings, _cells, _f_works))
                
        
    return best_score, sum(t.values())

Usage:

total_length, failed = 0, 0
full_failed = []
t = time.time()
c = 0
for i in filter(None, cases.split('\n')):
    c += 1
    print(c)
    result, f = solver(i.strip(), depth = 1, option_depth = 1, board_depth = 1, breadth = 1)
    if result is None:
        result, f = solver(i.strip(), depth = 1, option_depth = 2, board_depth = 1, breadth = 1)
    if result is None:
        failed += 1
        total_length += f
    else:
        total_length += result

print(total_length, failed, time.time() - t)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.