Skip to content

Instantly share code, notes, and snippets.

View mrnkr's full-sized avatar
🏠
Working from home

Alvaro Nicoli mrnkr

🏠
Working from home
View GitHub Profile
@mrnkr
mrnkr / MinimizeSum.swift
Created December 5, 2018 14:18
Backtracking / Dynamic programming exercise - Go from start to finish in a matrix accumulating the least possible sum of inner elements
import Foundation
func min(a: Int, b: Int) -> Int {
return a < b ? a : b
}
func adjacentSpots(board: [[Int]], start: [Int]) -> [[Int]] {
let i = start[0]
let j = start[1]
var ret: [[Int]] = []
task type Semaphore is
entry Init (Initial_Capacity: in Int);
entry Wait;
entry Signal;
end
task body Semaphore is
Remaining_Capacity: Int;
begin
accept Init (Initial_Capacity: in Int) do
Remaining_Capacity := Initial_Capacity;
task Readers_Writers is
procedure Reader (Read: out Int);
entry Writer (Written: in Int);
entry Begin_Reading;
entry Finish_Reading;
end
procedure Reader (Read: out Int) is
begin
Begin_Reading;
Read := Shared;
task Readers_Writers is
procedure Reader (Read: out Int);
entry Writer (Written: in Int);
entry Begin_Reading;
entry Finish_Reading;
end;
procedure Reader (Read: out Int) is
begin
Begin_Reading;
task Buffer is
entry Insert (In_Val: in T);
entry Extract (Out_Val: out T);
end
task body Buffer is
Data: Array 0..N-1 of Int;
Top: Int;
Bottom: Int;
Length: Int;
x: T;
task Alice is
end
task body Alice is
begin
loop
Yard.Request;
Walk_Dog;
Yard.Release;
end loop
end Alice;
Philosophers: Array 0..4 of Philosopher;
task type Philosopher is
end
task body Philosopher is
begin
loop
Table.Enter
Chopsticks.Request
Chopsticks.Request
Chopsticks: Array 0..4 of Chopstick;
Philosophers: Array 0..4 of Philosopher;
task type Philosopher is
end
task body Philosopher is
Left: Int;
Enumerator.Gimme_My_Number(Left);
Right: Int := (Left + 1) mod 5;
begin
@mrnkr
mrnkr / monitors.swift
Created October 24, 2018 21:14
Semaphores implemented using monitors and condition variables
import UIKit
class Condition {
var waiting: Bool
init() {
waiting = false
}
func wait() {
@mrnkr
mrnkr / philosophers.swift
Created October 24, 2018 18:52
Philosophers problem solved with semaphores on swift
import UIKit
class Semaphore {
var n: Int
init(n: Int) {
self.n = n
}
func P() {