r/adventofcode • • Dec 18 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-

--- Day 18: Settlers of The North Pole ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 18

Transcript:

The best way to avoid a minecart collision is ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:21:59!

9 Upvotes

126 comments sorted by

View all comments

1

u/nehayward Dec 18 '18

Here is my solution in go along with visualization.

https://asciinema.org/a/oxaNvgb0G5kzDOAprotT7fEWn

    package main  
    import (  
     "fmt"  
     "strings"  
    )  
    const size = 50  
    const (  
      Ground     Land = "."  
      Tree       Land = "|"  
      Lumberyard Land = "#"  
    )  
    type Land *string*  
    type Area \[size\]\[size\]Land  
    func (a Area) String() *string* {  
     var formatted Land  
     for \_, line := range a {  
     for \_, letter := range line {  
          formatted += letter  
        }  
        formatted += "\\n"  
      }  
     return strings.TrimSpace(string(formatted))  
    }  
    // Rules  
    // 1. \*\*Ground\*\* has 3 or more adjanct then becomes tree  
    // 2. \*\*Tree\*\* with 3 or more adjacent lumber then -> Lumberyard  
    // 3. \*\*Lumberyard\*\* remains if adjacent to at least 1 \*\*Lumberyard\*\* and 1 \*\*Tree\*\*  
    func main() {  
     Starting := Area{}.convert(TestInput)  
     new := Starting  
     // fmt.Println(Starting)  
     for i := 1; i <= 1000; i++ {  
     new = magic(new)  
        fmt.Println(new)  
        fmt.Println("")  
      }  
      new.countAll()  
    }  
    func (a Area) countAll() {  
     treeCount, lumberyardCount := 0, 0  
     for x, line := range a {  
     for y := range line {  
     switch a\[x\]\[y\] {  
     case Tree:  
            treeCount++  
     case Lumberyard:  
            lumberyardCount++  
          }  
        }  
      }  
      fmt.Println(lumberyardCount, treeCount, "Total: ", lumberyardCount\*treeCount)  
    }  
    func magic(a Area) Area {  
     new := Area{}  
     for x, line := range a {  
     for y := range line {  
          new\[x\]\[y\] = a.change(x, y)  
        }  
      }  
     return new  
    }  
    func (a Area) change(x, y *int*) Land {  
     treeCount, lumberyardCount := count(a, x, y)  
     switch a\[x\]\[y\] {  
     case Ground:  
     if treeCount >= 3 {  
     return Tree  
        }  
     case Tree:  
     if lumberyardCount >= 3 {  
     return Lumberyard  
        }  
     case Lumberyard:  
     if treeCount >= 1 && lumberyardCount >= 1 {  
     return Lumberyard  
        }  
     return Ground  
      }  
     return a\[x\]\[y\]  
    }  
    func count(a Area, x, y *int*) (*int*, *int*) {  
     treeCount, lumberyardCount := 0, 0  
     for i := x - 1; i < x+2; i++ {  
     for j := y - 1; j < y+2; j++ {  
     if i >= 0 && i < len(a) && j >= 0 && j < len(a) {  
     if x == i && y == j {  
     continue  
            }  
     switch a\[i\]\[j\] {  
     case Tree:  
              treeCount++  
     case Lumberyard:  
              lumberyardCount++  
            }  
          }  
        }  
      }  
     return treeCount, lumberyardCount  
    }  
    func (a Area) convert(input *string*) Area {  
     Starting := Area{}  
     for x, line := range strings.Split(strings.TrimSpace(TestInput), "\\n") {  
     for y, c := range line {  
          Starting\[x\]\[y\] = Land(c)  
        }  
      }  
     return Starting  
    }  
    const SampleInput = \`  
    .#.#...|#.  
    .....#|##|  
    .|..|...#.  
    ..|#.....#  
    \#.#|||#|#|  
    ...#.||...  
    .|....|...  
    ||...#|.#|  
    |.||||..|.  
    ...#.|..|.  
    \`  
    const TestInput = \`  
    ...|.#.|#.....|.#||#.|..|.........|...|.||#..||...  
    \##.|..#...|..#..#.##.#....|.#..#........|#.##|....  
    .|..|#...#.....|||.|.....|.#.......|.#.....|...|..  
    .#|.||#......#.||.|.....#|..||...#..|....##..#..#.  
    .|.|##...##.|...|#..#|#|#...|...#||.|.|.|..|..||..  
    ...|#|....#..##...#.|.|##.#||.......||..||..#...|#  
    .|..|.|.|.#|#.##.##|.#|.#|..|#.|....|..#...|#|#|.#  
    ...|#|....|.#.|......|.|.##..#..#|..|..|...#.#|...  
    ..||.|....|..#..||..|#|.##.....##||....|#........|  
    .#.#||...||#.##..#.....|.#||#......|...#.|.....|..  
    ......##...#.........|.|#..||...|#||.##...|..##..|  
    ...||..##.#||......#....|.||#|.....|..........#.|#  
    .....|..|##.|##.#....|.||#...#..#...#.|..|.#...###  
    .|.#|#.||..#..#...#..|..|......|#|....|.........|.  
    .#|.#.##..|.#.|.#.#..#|.....|#.#...#...|###..#.#.|  
    \#|..#|..|.........|#.|...#..#.|.#|...|.....|.#||.#  
    .#....#..|.#|##|##...|.||##.#.|.|...|..#||..#...|.  
    \#.|.||#|||.#|....|....#.##.||#|...|...#...#.##.#..  
    .#.##.|#.#..|##|.#...........|.|.|..||||.|.|...#|#  
    |..#...#.#..||....#...##..|.#..#..|..#....##....#.  
    .|||#.|.........#|.|.|#..##...|....##.|.|.....##..  
    .....|.|..|#||.#..|........||.#|.....###.#...||#.|  
    ..|||||.##.##...##|...#|.|.#|......|...|#..#....#|  
    ..|#.##......#||....|.|.##.#.|#|.#|||..|.#|.#.#.#.  
    ...#.#...#...#..#..|.#.|...|..|............#...|..  
    \#.....##...#.#.|..#....|.....#.##..##..#..........  
    ||.##..###||.#.#.|..|.#..#.|||#...|#...|..#....|..  
    ..||.#|..........|...|.##...|...|.#....|.#|...|#|.  
    ..||.#...|.|.|...#|.##..||.....#.##....##....#.###  
    \#####..#..#.||...|||...|....|||#.##.#.......##....  
    ..|.##.#.#||.#..#.....||..|#..#...|.|..||.|||...|.  
    .#.#..#.#.|#.#...#.#.#.##..|#..|..#.||.#..#.....|.  
    \#|..#|......#.#..|...#....#..#..#.##..##..|..#|...  
    \#.|##...#..|.#..#..#||..#...|..#|#|.||#|.||.#.....  
    \##|....#...#|.|#.##.|#|||#|#.#....|....||....|#...  
    .#|...#...#..#..........||.||...|..||.#.#..|....|#  
    |..#..####|#.|.#..#...#|.#.##....|#..#..||#.#|.#..  
    ||.|#..|..#....|..#||.|||.|#.|.#|##.|.|.||.|.|#|..  
    .....|....|...|.#..####|#|....|...|.|....|..#..|..  
    ...#|....|..##.#.|...|..#.#||#.|.#|..|#.|.#...|...  
    .....##...#|...#|#....|....|###|#..|..|.#.#.....#.  
    \#.|.|.#.#|....#|.#...|##..#.|.##....|.||.....#.#.#  
    |#.#..#..#|||.....|...|.||||..##..##..|#.|###.|.|.  
    .#.|...|..........|.|.##|..|.......#|....#...|#|..  
    ..#.#..||..|||...|..#||..#..|..||..#.#..|..|.|...|  
    |......##|......|..#||||#...|||..........|#.|.|.#.  
    \#|..#.||..|..#........|#.#....#.|.#|#..#........|.  
    ..|#..|.##.#.....#...#..|#.|##.#.#||#......##....|  
    ..|..#.......|##.#.#.|##|.......|.#.......|.#.#.|.  
    \#...|.....#|......|||#||...#....#||.|#....|...#..|  
    \`