r/adventofcode • • Dec 10 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-

--- Day 10: The Stars Align ---


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 10

Transcript: With just one line of code, you, too, can ___!


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:16:49!

23 Upvotes

233 comments sorted by

View all comments

2

u/TellowKrinkle Dec 10 '18

I'm glad I've been keeping around the helper functions I've been using in previous AOCs. I solved the problem by updating points until the total y area covered by them started increasing, assuming that they'd be closest together when they were valid text.

Swift, #64/#56

struct Point: Hashable {
	var x: Int
	var y: Int
}

extension Sequence where Element: Strideable {
	func minmax() -> ClosedRange<Element>? {
		var iter = makeIterator()
		guard var min = iter.next() else { return nil }
		var max = min
		while let next = iter.next() {
			min = Swift.min(min, next)
			max = Swift.max(max, next)
		}
		return min...max
	}
}

func aocD10(_ input: [(position: Point, velocity: Point)]) {
	var points = input
	var output = input
	var count = 0
	while true {
		for index in points.indices {
			points[index].position.x += points[index].velocity.x
			points[index].position.y += points[index].velocity.y
		}
		let range = points.lazy.map({ $0.position.y }).minmax()!
		let prevRange = output.lazy.map({ $0.position.y }).minmax()!
		if range.count > prevRange.count {
			break
		}
		output = points
		count += 1
	}
	let xrange = points.lazy.map({ $0.position.x }).minmax()!
	let yrange = points.lazy.map({ $0.position.y }).minmax()!

	var arr = [[Bool]](repeating: [Bool](repeating: false, count: xrange.count), count: yrange.count)
	for point in output {
		arr[point.position.y - yrange.lowerBound][point.position.x - xrange.lowerBound] = true
	}
	for row in arr {
		print(String(row.lazy.map({ $0 ? "#" : "." })))
	}
	print(count)
}

import Foundation
let str = try! String(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))

let points = str.split(separator: "\n").map { line -> (Point, Point) in
	let nums = line.split(whereSeparator: { !"-0123456789".contains($0) }).map({ Int($0)! })
	return (Point(x: nums[0], y: nums[1]), Point(x: nums[2], y: nums[3]))
}

aocD10(points)

4

u/tterrag1098 Dec 10 '18

That logic doesn't work on my input, I have one second after the "correct" one where all the points are closer together, but do not form text.