r/adventofcode • • Dec 17 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 17 Solutions -🎄-

--- Day 17: Reservoir Research ---


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 17

Transcript:

All aboard the Easter Bunny HQ monorail, and mind the gap! Next stop: ___


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 01:24:07!

15 Upvotes

105 comments sorted by

View all comments

5

u/xthexder Dec 17 '18

This problem reminds me of the falling sand game I used to play.

Solved in Golang 60/57

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

var data [2000][2000]byte

var minx, maxx int = 2000, 0
var miny, maxy int = 2000, 0

func minmax(x, y int) {
	if x < minx {
		minx = x
	}
	if x > maxx {
		maxx = x
	}
	if y < miny {
		miny = y
	}
	if y > maxy {
		maxy = y
	}
}

func open(x, y int) bool {
	return data[x][y] == 0 || data[x][y] == '|'
}

func fill(x, y int) {
	if y > maxy {
		return
	} else if !open(x, y) {
		return
	}

	if !open(x, y+1) {
		leftX := x
		for open(leftX, y) && !open(leftX, y+1) {
			data[leftX][y] = '|'
			leftX--
		}
		rightX := x + 1
		for open(rightX, y) && !open(rightX, y+1) {
			data[rightX][y] = '|'
			rightX++
		}
		if open(leftX, y+1) || open(rightX, y+1) {
			fill(leftX, y)
			fill(rightX, y)
		} else if data[leftX][y] == '#' && data[rightX][y] == '#' {
			for x2 := leftX + 1; x2 < rightX; x2++ {
				data[x2][y] = '~'
			}
		}
	} else if data[x][y] == 0 {
		data[x][y] = '|'
		fill(x, y+1)
		if data[x][y+1] == '~' {
			fill(x, y)
		}
	}
}

func main() {
	reader, err := os.Open("day17.txt")
	if err != nil {
		log.Fatal(err)
	}

	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		line := strings.Split(scanner.Text(), ", ")
		a, _ := strconv.Atoi(line[0][2:])
		bstr := strings.Split(line[1][2:], "..")
		bmin, _ := strconv.Atoi(bstr[0])
		bmax, _ := strconv.Atoi(bstr[1])
		if line[0][0] == 'x' {
			for y := bmin; y <= bmax; y++ {
				minmax(a, y)
				data[a][y] = '#'
			}
		} else {
			for x := bmin; x <= bmax; x++ {
				minmax(x, a)
				data[x][a] = '#'
			}
		}
	}
	reader.Close()

	fill(500, 0)

	water, touched := 0, 0
	for x := minx - 1; x <= maxx+1; x++ {
		for y := miny; y <= maxy; y++ {
			if data[x][y] == '|' {
				touched++
			} else if data[x][y] == '~' {
				water++
			}
		}
	}

	// Print board
	for y := miny - 1; y <= maxy; y++ {
		for x := minx - 1; x <= maxx+1; x++ {
			if x == 500 && y == miny-1 {
				fmt.Print("+")
			} else {
				if data[x][y] == 0 {
					fmt.Print(".")
				} else {
					fmt.Print(string(data[x][y]))
				}
			}
		}
		fmt.Println()
	}
	fmt.Println("Part A:", water+touched)
	fmt.Println("Part B:", water)
}