Add Code for Day 13 (only the start)
Day 13: Mine Cart Madness - add the "Cart" struct to do everything "cart" relatedmaster
parent
949f3e7444
commit
3e711561fa
@ -0,0 +1,194 @@
|
|||||||
|
//
|
||||||
|
// Advent of Code 2018 "Day 13: Mine Cart Madness"
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
enum Sequence: Int {
|
||||||
|
case left
|
||||||
|
case straight
|
||||||
|
case right
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Direction: Int {
|
||||||
|
case up
|
||||||
|
case right
|
||||||
|
case down
|
||||||
|
case left
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Cart: Equatable, Comparable {
|
||||||
|
var loc = GridPoint(X: 0, Y: 0)
|
||||||
|
var dir = Direction.up
|
||||||
|
var intersection = Sequence.left
|
||||||
|
var dirValue: (Int, Int) {
|
||||||
|
get {
|
||||||
|
switch dir {
|
||||||
|
case .up : return (0, -1)
|
||||||
|
case .right: return (1, 0)
|
||||||
|
case .down : return (0, 1)
|
||||||
|
case .left : return (-1, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func move() {
|
||||||
|
loc.X = loc.X + dirValue.0
|
||||||
|
loc.Y = loc.Y + dirValue.1
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func changeDirection(with map: Character) {
|
||||||
|
func rotateRight() { dir = Direction(rawValue: dir.rawValue + 1) ?? Direction.up }
|
||||||
|
func rotateLeft() { dir = Direction(rawValue: dir.rawValue - 1) ?? Direction.up }
|
||||||
|
func increaseSequence() { intersection = Sequence(rawValue: intersection.rawValue + 1) ?? Sequence.left }
|
||||||
|
|
||||||
|
// No change
|
||||||
|
if map == "-" || map == "|" { return }
|
||||||
|
// Execute intersection sequence
|
||||||
|
if map == "+" {
|
||||||
|
switch intersection {
|
||||||
|
case .left : rotateLeft()
|
||||||
|
case .straight: break
|
||||||
|
case .right : rotateRight()
|
||||||
|
}
|
||||||
|
increaseSequence()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Turn to follow curve
|
||||||
|
switch dir {
|
||||||
|
case .up, .down :
|
||||||
|
if map == "/" { rotateRight() }
|
||||||
|
if map == "\\" { rotateLeft() }
|
||||||
|
case .left, .right :
|
||||||
|
if map == "/" { rotateLeft() }
|
||||||
|
if map == "\\" { rotateRight() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func == (lhs: Cart, rhs: Cart) -> Bool {
|
||||||
|
return (lhs.loc.X == rhs.loc.X) && (lhs.loc.Y == rhs.loc.Y)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func < (lhs: Cart, rhs: Cart) -> Bool {
|
||||||
|
return lhs.loc.Y == rhs.loc.Y ? lhs.loc.X < rhs.loc.X : lhs.loc.Y < rhs.loc.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialization:
|
||||||
|
// 1. read in the grid to get the size (width anf height)
|
||||||
|
// 2. convert to 2-dimensional grid of Character
|
||||||
|
// 3. scan for carts
|
||||||
|
// a. note the grid location
|
||||||
|
// b. note the direction
|
||||||
|
// c. initialize the "turning sequence" (count, direction)
|
||||||
|
// d. convert the cart to the appropriate track
|
||||||
|
// 1. '<' or '>' => '-'
|
||||||
|
// 2. '^' or 'v' => '|'
|
||||||
|
class Madness {
|
||||||
|
var carts: [Cart] = []
|
||||||
|
}
|
||||||
|
|
||||||
|
class Day13: AOCDay {
|
||||||
|
lazy var tests: (() -> ()) = day13Tests
|
||||||
|
lazy var final: (() -> ()) = day13Final
|
||||||
|
|
||||||
|
func testCartMove() {
|
||||||
|
var car = Cart(loc: GridPoint(X: 10, Y:10), dir: Direction.up, intersection: Sequence.left)
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartMove", withExpression: (car.loc.X == 10 && car.loc.Y == 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCartChangeDirection() {
|
||||||
|
var car = Cart(loc: GridPoint(X: 10, Y:10), dir: Direction.up, intersection: Sequence.left)
|
||||||
|
car.move()
|
||||||
|
car.changeDirection(with: "-")
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '-'", withExpression: (car.loc.X == 10 && car.loc.Y == 8))
|
||||||
|
car.changeDirection(with: "|")
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '|'", withExpression: (car.loc.X == 10 && car.loc.Y == 7))
|
||||||
|
car.changeDirection(with: "/")
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '/'", withExpression: (car.loc.X == 11 && car.loc.Y == 7))
|
||||||
|
car.changeDirection(with: "\\")
|
||||||
|
car.move()
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '\\'", withExpression: (car.loc.X == 11 && car.loc.Y == 9))
|
||||||
|
car.changeDirection(with: "+")
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '+'", withExpression: (car.loc.X == 12 && car.loc.Y == 9))
|
||||||
|
car.changeDirection(with: "+")
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '+'", withExpression: (car.loc.X == 13 && car.loc.Y == 9))
|
||||||
|
car.changeDirection(with: "+")
|
||||||
|
car.move()
|
||||||
|
XCTAssertEqual(test: "testCartChangeDirection '+'", withExpression: (car.loc.X == 13 && car.loc.Y == 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCartSort() {
|
||||||
|
var cars: [Cart] = []
|
||||||
|
cars.append(Cart(loc: GridPoint(X: 10, Y:10), dir: Direction.up, intersection: Sequence.left))
|
||||||
|
cars.append(Cart(loc: GridPoint(X: 5, Y:22), dir: Direction.up, intersection: Sequence.left))
|
||||||
|
cars.append(Cart(loc: GridPoint(X: 22, Y:5), dir: Direction.up, intersection: Sequence.left))
|
||||||
|
cars.append(Cart(loc: GridPoint(X: 2, Y:10), dir: Direction.up, intersection: Sequence.left))
|
||||||
|
// for car in cars { print("\(car)")}
|
||||||
|
cars.sort()
|
||||||
|
// print("")
|
||||||
|
// for car in cars { print("\(car)")}
|
||||||
|
let lastCar = cars.last!
|
||||||
|
XCTAssertEqual(test: "testCartSort last", withExpression: (lastCar.loc.X == 5 && lastCar.loc.Y == 22))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCartEquatable() {
|
||||||
|
var car1 = Cart(loc: GridPoint(X: 10, Y:10), dir: Direction.up, intersection: Sequence.left)
|
||||||
|
car1.move()
|
||||||
|
car1.changeDirection(with: "-")
|
||||||
|
car1.move()
|
||||||
|
// XCTAssertEqual(test: "testCartChangeDirection '-'", withExpression: (car1.loc.X == 10 && car1.loc.Y == 8))
|
||||||
|
let car2 = Cart(loc: GridPoint(X: 10, Y:8), dir: Direction.up, intersection: Sequence.left)
|
||||||
|
XCTAssertEqual(test: "testCartEquatable", withExpression: (car1 == car2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func day13Tests() {
|
||||||
|
testCartMove()
|
||||||
|
testCartChangeDirection()
|
||||||
|
testCartSort()
|
||||||
|
testCartEquatable()
|
||||||
|
}
|
||||||
|
|
||||||
|
func day13Final() {
|
||||||
|
let retVal = "None"
|
||||||
|
print("Answer to part 1 is: \(retVal)")
|
||||||
|
print("Answer to part 2 is: \(retVal)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Start of Tick:
|
||||||
|
// 1. order carts by location
|
||||||
|
// 2. move cart one step in it's direction
|
||||||
|
// 3. Look to see where we landed
|
||||||
|
// a. other cart => CRASH
|
||||||
|
// b. '-' or '|' is a straight track => do nothing
|
||||||
|
// c. '\' => rotate right
|
||||||
|
// d. '/' => rotate left
|
||||||
|
// e. '+' => [Left, Straight, Right] sequense for this cart
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// We'll need to figure out the size of the grid
|
||||||
|
// - scan and find the number of carts and their locations and direction
|
||||||
|
// - convert the carts to "track"
|
||||||
|
// - At intersections, each cart executes their own "sequense"
|
||||||
|
// - Left, Straight, Right, repeat
|
||||||
|
// - move carts in order of Top, Left to Bottom, Right
|
||||||
|
// - order carts by (x,y) position, then execute moves
|
||||||
|
// Watch for first crash and report crash location
|
||||||
|
|
||||||
Loading…
Reference in New Issue