This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let countDiff = listOfDifferences.reduce(into: [:]) { counts, joltDiff in counts[joltDiff, default: 0] += 1 } | |
| print(countDiff) | |
| print((countDiff[1] ?? 0 ) * (countDiff[3] ?? 0 )) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var iterations = 0 | |
| var newGrid = grid | |
| repeat { | |
| grid = newGrid | |
| newGrid = [String]() | |
| for (rowIndex, row) in grid.enumerated() { | |
| var newRow = "" | |
| for (columnIndex, element) in row.enumerated() { | |
| if element == "L" { | |
| let count = countAdjacentChars(of: "#", in: grid, at: (columnIndex, rowIndex), allowedMismatches: 8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var x = 0.0 | |
| var y = 0.0 | |
| var direction = 0.0 // degrees east | |
| lines.forEach { (line) in | |
| if let instruction = line.first, let amount = Double(line.dropFirst()) { | |
| if instruction == "E" { | |
| x += amount | |
| } else if instruction == "W" { | |
| x -= amount |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let departureTime = Int(lines[0])! | |
| let busIds = lines[1].components(separatedBy: ",").compactMap{ Int($0) } | |
| let minTimeToWait = busIds.map { busId in (busId, busId - (departureTime % busId)) }.min { first, second in first.1 < second.1 }! | |
| print(minTimeToWait.0 * minTimeToWait.1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var currentMask = "" | |
| var memory = [Int: String]() | |
| lines.forEach { line in | |
| let parts = line.components(separatedBy: " = ") | |
| let variable = parts[0] | |
| let value = parts[1] | |
| print(variable, value) | |
| if variable.contains("mask") { | |
| currentMask = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let startingNumbers = [12,1,16,3,11,0] | |
| var numbers = [Int : Int]() | |
| startingNumbers.enumerated().dropLast().forEach { numbers[$0.element] = $0.offset } | |
| var roundCount = startingNumbers.count | |
| var currentNumber = startingNumbers.last! | |
| //let roundEnd = 2020 | |
| let roundEnd = 30000000 | |
| while roundCount < roundEnd { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Rule { | |
| let name: String | |
| let range1: ClosedRange<Int> | |
| let range2: ClosedRange<Int> | |
| func contains(value: Int) -> Bool { | |
| return range1.contains(value) || range2.contains(value) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december17.txt" | |
| let contents = try! String(contentsOfFile: filename) | |
| let lines = contents.components(separatedBy: CharacterSet.newlines).filter { !$0.isEmpty } | |
| var grid = [D4 : Bool]() | |
| //var grid = [D3 : Bool]() // for 3D simulation | |
| for (x,line) in lines.enumerated() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func calcNoBrackets(expression: String) -> Int { | |
| let parts = expression.components(separatedBy: " ") | |
| let numbers = parts.enumerated().filter {$0.0 % 2 == 0 && !$0.1.isEmpty }.map { $0.1.toInt() } | |
| let operators: [((Int,Int) -> Int)] = parts.enumerated().filter {$0.0 % 2 == 1 && !$0.1.isEmpty }.map { $0.1.trimmingCharacters(in: .whitespacesAndNewlines) }.map { | |
| switch $0 { | |
| case "*": return (*) | |
| case "+": return (+) | |
| default: | |
| return (+) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| struct Rule { | |
| let condition1: [Int]? | |
| let condition2: [Int]? | |
| let char: Character? | |
| } | |
| let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december19.txt" |