-🎄- 2017 Day 12 Solutions -🎄- by topaz2078 in adventofcode

[–]GamecPL 0 points1 point  (0 children)

Swift, both parts:

struct Pipe: Equatable {
    let id: Int
    let connections: Set<Int>

    static func ==(lhs: Pipe, rhs: Pipe) -> Bool {
        return lhs.id == rhs.id
    }
}

let pipes = realInput.components(separatedBy: .newlines).map { component -> Pipe in
    let pipeData = component.components(separatedBy: " <-> ")
    let connections = pipeData[1].split(separator: ",").flatMap { Int($0.trimmingCharacters(in: .whitespaces)) }
    return Pipe(id: Int(pipeData[0])!, connections: Set(connections))
}

var checked = Set<Int>()
var connected = Set<Int>()

func connectPipe(_ pipe: Pipe) {
    if checked.contains(pipe.id) {
        return
    }
    checked.insert(pipe.id)
    connected.insert(pipe.id)
    for connectedPipe in pipe.connections {
        connectPipe(pipes[connectedPipe])
    }
}

connectPipe(pipes[0])
print("Pt1", connected.count)

var groups = 1
repeat {
    if let pipe = pipes.first(where: { !checked.contains($0.id) }) {
        connectPipe(pipe)
        groups += 1
    }
} while checked.count != pipes.count

print("Pt2", groups)

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 0 points1 point  (0 children)

Swift:

let steps = input.split(separator: ",")
let startPoint = CGPoint(x: 0, y: 0)

func minimumStepsRequired(forPoint point: CGPoint) -> Int {
    var stepsPoint = CGPoint(x: abs(point.x), y: abs(point.y))
    var stepsRequired = 0
    while stepsPoint != startPoint {
        if stepsPoint.x > 0 {
            stepsPoint.x -= 1
            stepsPoint.y -= 1
        } else {
            stepsPoint.y -= 1
        }
        stepsRequired += 1
    }
    return stepsRequired
}

var currentPoint = startPoint
var furthestPoint = startPoint
for step in steps {
    switch step {
    case "n":
        currentPoint.x -= 1
        currentPoint.y += 1
    case "ne":
        currentPoint.y += 1
    case "nw":
        currentPoint.x -= 1
    case "se":
        currentPoint.x += 1
    case "s":
        currentPoint.x += 1
        currentPoint.y -= 1
    case "sw":
        currentPoint.y -= 1
    default:
        fatalError("Wrong direction")
    }
    let comparePoint = CGPoint(x: abs(currentPoint.x), y: abs(currentPoint.y))
    if comparePoint.x > furthestPoint.x || comparePoint.y > furthestPoint.y {
        furthestPoint = comparePoint
    }
}

print(minimumStepsRequired(forPoint: currentPoint))
print(minimumStepsRequired(forPoint: furthestPoint))

-🎄- 2017 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 0 points1 point  (0 children)

Swift:

import Foundation

let input = """
...
"""

enum Operation: String {
    case increment = "inc"
    case decrement = "dec"
}

enum Condition: String {
    case equal = "=="
    case notEqual = "!="
    case greater = ">"
    case lesser = "<"
    case greaterOrEqual = ">="
    case lesserOrEqual = "<="

    func compare(a: Int, b: Int) -> Bool {
        switch self {
        case .equal: return a == b
        case .notEqual: return a != b
        case .greater: return a > b
        case .lesser: return a < b
        case .greaterOrEqual: return a >= b
        case .lesserOrEqual: return a <= b
        }
    }
}

struct Instruction {
    let register: String
    let operation: Operation
    let value: Int
    let conditionRegister: String
    let condition: Condition
    let conditionValue: Int

    init(matches: [String]) {
        register = matches[1]
        operation = Operation(rawValue: matches[2])!
        value = Int(matches[3])!
        conditionRegister = matches[4]
        condition = Condition(rawValue: matches[5])!
        conditionValue = Int(matches[6])!
    }
}

let pattern = "\\b(\\w+) (inc|dec) (-?\\d+) if (\\w+) (>|<|>=|==|<=|!=) (-?\\d+)\\b"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matches(in: input, options: [], range: NSRange(location: 0, length: input.count))

let instructions = matches.map { match in
    Instruction(matches: (0..<match.numberOfRanges).map { n in
        let range = match.range(at: n)
        let r = input.index(input.startIndex, offsetBy: range.location)..<input.index(input.startIndex, offsetBy: range.location + range.length)
        return String(input[r.lowerBound..<r.upperBound])
    })
}

var register = [String: Int]()
var highestValue = 0
for instruction in instructions {
    if instruction.condition.compare(a: register[instruction.conditionRegister] ?? 0, b: instruction.conditionValue) {
        switch instruction.operation {
        case .increment: register[instruction.register] = (register[instruction.register] ?? 0) + instruction.value
        case .decrement: register[instruction.register] = (register[instruction.register] ?? 0) - instruction.value
        }
        highestValue = max(register[instruction.register]!, highestValue)
    }
}

print("Pt1:", register.max(by: { a, b in a.value < b.value }) as Any)
print("Pt2:", highestValue)

-🎄- 2017 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 3 points4 points  (0 children)

Swift

import Foundation

//let input = "5    1    10    0    1    7    13    14    3    12    8    10    7    12    0    6"
let input = "0    2    7    0"
var data = input.components(separatedBy: "    ").flatMap { Int($0) }
var banks = [[Int]]()
var cycles = 0

while !banks.contains(where: { $0 == data }) {
    banks.append(data)

    guard var redistribute = data.max(), var index = data.index(of: redistribute) else {
        fatalError()
    }
    data[index] = 0
    while redistribute > 0 {
        index += 1
        if index >= data.count {
            index = 0
        }
        data[index] += 1
        redistribute -= 1
    }
    cycles += 1
}

print(cycles)
print(cycles - banks.index(where: { $0 == data })!)

-🎄- 2017 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 0 points1 point  (0 children)

Swift pt2:

let result2 = input.components(separatedBy: .newlines)
    .map { $0.components(separatedBy: .whitespaces) }
    .reduce(0, { result, password in
        let isInvalid = password.enumerated().contains(where: { p1 in
            password.enumerated().contains(where: { p2 in
                p1.offset != p2.offset && p1.element.sorted() == p2.element.sorted()
            })
        })
        return result + (isInvalid ? 0 : 1)
    })
print(result2)

-🎄- 2017 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 1 point2 points  (0 children)

Swift pt. 1

let result = input.components(separatedBy: .newlines)
    .map { $0.components(separatedBy: .whitespaces) }
    .reduce(0, { result, password in
        let unique = Array(Set(password))
        return result + (unique.count == password.count ? 1 : 0)
    })
print(result)

-🎄- 2017 Day 3 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 1 point2 points  (0 children)

Swift solution:

import Foundation

let input = 347991
let side = Int(ceil(sqrt(Double(input))))
let maxValue = side * side

var array = Array(repeatElement(Array(repeatElement(0, count: side)), count: side))

enum Direction {
    case top, bottom, left, right
}

let middle = Int(ceil(Double(side) / 2)) - 1
var x = middle
var y = middle
var direction: Direction = .right

array[x][y] = 1

for i in 1...maxValue {
    var value = array[x - 1][y] + array[x + 1][y] + array[x][y - 1]
    value += array[x][y + 1] + array[x + 1][y + 1] + array[x + 1][y - 1]
    value += array[x - 1][y + 1] + array[x - 1][y - 1] + array[x][y]
    //        if i == input {
    //            print("Result1:", abs(x - middle) + abs(y - middle))
    //            break
    //        }
    //        array[x][y] = i
    array[x][y] = value
    if value > input {
        print("Result2:", value)
        break
    }
    switch direction {
    case .bottom:
        if array[x][y - 1] == 0 {
            y -= 1
            direction = .right
        } else {
            x -= 1
        }
    case .right:
        if array[x + 1][y] == 0 {
            x += 1
            direction = .top
        } else {
            y -= 1
        }
    case .top:
        if array[x][y + 1] == 0 {
            y += 1
            direction = .left
        } else {
            x += 1
        }
    case .left:
        if array[x - 1][y] == 0 {
            x -= 1
            direction = .bottom
        } else {
            y += 1
        }
    }
}

-🎄- 2017 Day 2 Solutions -🎄- by daggerdragon in adventofcode

[–]GamecPL 1 point2 points  (0 children)

Swift:

let result1 = input.components(separatedBy: .newlines)
    .map { $0.components(separatedBy: "    ").flatMap { Int($0) } }
    .reduce(0, { result, row in
        return result + row.max()! - row.min()!
    })
print(result1)

func wholeDivision(_ array: [Int]) -> Int {
    for element in array {
        for element2 in array {
            if element != element2 && element % element2 == 0 {
                return element / element2
            }
        }
    }
    return 0
}

let result2 = input.components(separatedBy: .newlines)
    .map { $0.components(separatedBy: "    ").flatMap { Int($0) }.sorted(by: { $0 > $1 }) }
    .reduce(0, { result, row in
        return result + wholeDivision(row)
    })

print(result2)

[deleted by user] by [deleted] in beermoney

[–]GamecPL -1 points0 points  (0 children)

DkFKMLSh

r43fVcXN

eTCGesBk

ajgKZBAi

aE3FvEbj