Skip to content

Instantly share code, notes, and snippets.

View sssbohdan's full-sized avatar
❤️

Bohdan Savych sssbohdan

❤️
View GitHub Profile
@sssbohdan
sssbohdan / URLProtocolMock.swift
Last active January 17, 2024 13:49
URLProtocolMock.swift
import Foundation
final class URLProtocolMock: URLProtocol {
static var data = [URL: (error: Error?, data: Data?)]()
override class func canInit(with request: URLRequest) -> Bool {
return true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
@sssbohdan
sssbohdan / BigInt2.swift
Created May 6, 2023 19:51
BigInt2.swift
struct BigInt {
let string: String
func add(_ value: BigInt) -> BigInt {
if self.string == "0" { return value }
if value.string == "0" { return self }
var lhs = self.string
var rhs = value.string
@sssbohdan
sssbohdan / BigInt.swift
Created May 6, 2023 19:23
BigInt1.swift
struct BigInt {
private let overflows: UInt64
private var remainder: UInt64
func add(_ x: BigInt) -> BigInt {
let totalOverflows = self.overflows + x.overflows
let result = self.remainder.addingReportingOverflow(x.remainder)
return BigInt(
@sssbohdan
sssbohdan / decimal.swift
Last active April 22, 2023 19:53
Decimal
let res = Decimal(string: "9999999999999999999999999999999999999999999")!
* Decimal(string: "99999999999998999999999999999999999")!
print(res) // 999999999999989999999999999999999989990000000000000000000000000000000000000000 - wrong
// correct result – 999999999999989999999999999999999989999999900000000000001000000000000000000001
@sssbohdan
sssbohdan / day13.swift
Created December 14, 2022 11:26
day13
enum AoCDay13 {
static func solve() -> Int {
let pairs = input.split(separator: "\n\n")
.map { $0.split(separator: "\n") }
.map { (String($0[0]), String($0[1])) }
var sum = 0
for (index, pair) in pairs.enumerated() {
print("====================== Pair \(index) ======================")
print("\(pair.0)\n\(pair.1)")
//
// Matrix.swift
// SudokuSolver
//
// Created by Bohdan Savych on 19/4/22.
//
import Foundation
//
// MonadsExperimentations.swift
// Algorithms
//
// Created by Bohdan Savych on 23/10/2022.
//
import Foundation
precedencegroup CompositionPrecedence {
@sssbohdan
sssbohdan / DI.swift
Last active August 5, 2022 13:41
Resolver
//
// DI.swift
// MobiusNotes
//
// Created by bsavych on 05/08/2022.
//
import Foundation
enum DIScope {
var dict = [Int: Int?]()
let val = dict[0] // Int??
func f(_ optionalInt: Int?) -> Int { optionalInt ?? 0 }
//f(val) // compiler error
f(val.flatten()) // works
@sssbohdan
sssbohdan / ObserverExample.swift
Created January 17, 2021 16:54
ObserverExample
protocol ActionDoable: AnyObject {
func doAction(with value: Int)
}
class A: ActionDoable {
func doAction(with value: Int) {
print("do from A \(value)")
}
}