Skip to content

Instantly share code, notes, and snippets.

@seanhagen
Created December 20, 2019 02:35

Revisions

  1. seanhagen created this gist Dec 20, 2019.
    87 changes: 87 additions & 0 deletions aoc2019-18-1.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    // this is the node thing for getting from the start position to the last key
    type path struct {
    name string
    node *tile
    steps int
    parent *path
    children []path

    keysFound []string

    final bool

    mapNow grid
    }

    // traverse takes a root node ( the '@' node ) and creates a graph
    // the graph won't be even or whatever the term is for each branch being the
    // same length because it'll stop adding nodes whenever there are no 'gettable'
    // keys left from where it ends up
    func traverse(in path) path {
    if in.final {
    return in
    }

    tmpGrid := in.mapNow.getCopy() /// make a copy
    keys := tmpGrid.getableKeys(in.node)

    // for all those keys
    for _, k := range keys {
    ng := tmpGrid.getCopy() /// do it again to be safe -- apparently messing up
    // because when we do a second loop for [e d] it's not getting the right map
    d := ng.tileToTileSteps(in.node, k)

    kn := k.key
    kf := []string{kn}
    if len(in.keysFound) > 0 {
    kf = append(kf, in.keysFound...)
    }

    ng2 := ng.removeKey(kn) // another copy
    keysLeft := ng2.keys()
    gettableKeys := ng2.getableKeys(k)

    k.key = ""

    np := path{
    name: kn,
    node: k,
    parent: &in,
    steps: in.steps + d,
    children: []path{},
    mapNow: ng2.getCopy(), // do it again again, to be super safe?
    final: len(keysLeft) <= 0,
    keysFound: kf,
    }

    if len(keysLeft) > 0 && len(gettableKeys) > 0 {
    np = traverse(np)
    }

    in.children = append(in.children, np)
    }

    return in
    }

    // should be creating a copy of the map/grid/whatever, but maybe isn't???
    func (g grid) getCopy() grid {
    out := make(grid)
    for n, t := range g {
    nei := map[string]edge{}
    for nn, e := range t.neighbours {
    nei[nn] = e
    }

    nt := &tile{
    id: t.id,
    key: t.key,
    door: t.door,
    coord: t.coord,
    neighbours: nei,
    }

    out[n] = nt
    }
    return out
    }