Last active
July 6, 2022 08:28
-
-
Save yvonmanzi/ca03079131e078533fb431cb04fe3359 to your computer and use it in GitHub Desktop.
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
(function :Int maxFromList [ :Array<Int> list] | |
// Looks like variables declared inside `let` are all final. ?? Humn... | |
// Ooooh, you need a `&mut` to indicate mutability. ie. by default all | |
// variables are immutable. | |
(let [&mut :Int maxNumber 0] | |
//lovely almost pythonic syntax | |
(doFor number list | |
(set maxNumber (max maxNumber number)) | |
) | |
maxNumber) | |
) | |
(assert (= 5 (maxFromList [1 3 4 5 5]))) | |
// Specifying types for params | |
// Interesting note: `maxx` is implicitly converted into a `String`. | |
//? Is it good for the compiler to do that? problly...but then? shouldn't it at least give a warning? | |
// (function :String maxNumber [ :Int a :Int b] | |
// (let [maxx (max a b)] | |
// maxx | |
// )) | |
// (print "$(maxNumber 3 4)") | |
//Alternatively to maxNumber. Why is this assertion failing tho? | |
//? Returning an int. why? | |
// (function :String maxNumber [ :Int a :Int b] | |
// (let [maxx (max a b)] | |
// (.toString maxx) | |
// maxx | |
// )) | |
// (assert (= "5" (maxNumber 4 5))) | |
//Accessing stuff form an array. | |
//?Quick question: Seems like list and array might be diff things in lisp. is that so? | |
// (print "$(nth [1 2 3 4] 3)") | |
//?Why aren't these working? MakeQuickNth... | |
// (print "$(nth [1 2 3] first)") | |
// (function arrayAccess [:Array<Int> array] | |
// (print "$(nth array first)") | |
// ) | |
// ! A bit interesting that accessing out of bound returns the last element instead | |
//! of indicating an error or at least some sort of warning. | |
// This is a comparator for our boxes. It sorts boxes by their units per box in descending order. | |
// We can probably use a lambad function instead of this... | |
(function :Int sortboxTypes [ :Array<Int> a1 :Array<Int> a2] | |
(let [unitsA (nth a1 1) unitsB (nth a2 1)] | |
// There's probablly a more succint way of writing this. | |
(if (= unitsA unitsB) | |
(return 0)) | |
(if (> unitsA unitsB) | |
(return -1) | |
) | |
(if (< unitsA unitsB) | |
(return 1)) | |
) | |
) | |
(print "$(sortboxTypes [1 3][2 3])") | |
(assert (= 0 (sortboxTypes [1 3][2 3]))) | |
(print "$(sort [[1 2] [2 3] [4 5 9]] sortboxTypes )") | |
(assert (= 0 (sortboxTypes [2, 3][2, 3]))) | |
//? Question: Why can't we specify that boxTypes and trucksize will be mutable. | |
// I mean, Why can't write sth like this: | |
//`function :Int maximumUnits [ &mut :Array<Array<Int>> boxTypes &mut :Int truckSize]` | |
// When I write that, I get a syntax error. | |
(function :Int maximumUnits [ :Array<Array<Int>> boxTypes :Int truckSize] | |
(let [boxTypes (sort boxTypes sortboxTypes) | |
&mut maxUnits 0] | |
(print "$(.toString boxTypes)") | |
(doFor arr boxTypes | |
(let [currentNumberOfBoxes (nth arr 0) | |
unitsPerBox (nth arr 1)] | |
(let [diff (- truckSize currentNumberOfBoxes) | |
totalAvailableUnits (* currentNumberOfBoxes unitsPerBox)] | |
(if (= diff 0) | |
(return (+ maxUnits totalAvailableUnits))) | |
(if (< diff 0) | |
(return (+ maxUnits (* truckSize unitsPerBox)))) | |
(set maxUnits (+ maxUnits totalAvailableUnits)) | |
(set truckSize (- truckSize currentNumberOfBoxes)) | |
))) | |
maxUnits | |
) | |
) | |
(assert (= 91 (maximumUnits [[5 10] [2 5] [4 7] [3 9]] 10))) | |
(assert (= 8 (maximumUnits [[1 3] [2 2] [3 1]] 4))) | |
(function :Int maps [] | |
// A bit of a weird error when you leave space inside map declarations. | |
// I'm not sure if it's intentional but seems a bit annoying. | |
(let [myMap (new Map<Array<Int>,Int>) | |
// cell (new Cell<>) | |
] | |
(dictSet myMap [1 0] 0) | |
(dictSet myMap [1 1] 1) | |
(dictSet myMap [1 2] 2) | |
// (set cell.row 1) | |
// (set cell.col 2) | |
(print myMap) | |
(print "accessing...$(dictGet myMap [1 1])") | |
(return (dictGet myMap [1 1])) | |
) | |
) | |
(print " here's maps: $(maps)") | |
(assert (= 0 (maps))) | |
// Have no idea why this is not working. I defined Cell inside Main.hx | |
// (let [cell (Cell)] | |
// (set cell.row 1) | |
// (set cell.col 2) | |
// (print cell) | |
// ) | |
//... Still no where close to being completed this one. | |
(function :Void solveSudoku [ :Array<Array<String>>] | |
(let [n 9 | |
emptyCells (new Array<Array<Int>>) | |
// Should change these to some sort of sets if they exist. | |
rows (new Map<Int,Array<Int>) | |
cols (new Map<Int,Array<Int>)//problly use a class to define cell. or maybe list can be hashed given that they're immutable?. | |
grids (new Map<Array<Int>, Array<Int>) | |
getSubGrid (lambda [row col]([(/ row 3) (/ col 3)])) | |
] | |
(doFor row (range 0 n) | |
(doFor col (range 0 n) | |
// we should problly write: nth (nth board row) col | |
(if (!= (nth (nth board row) col) ".") | |
(if (rows.exists row) | |
(let [numArray (dictGet rows row) | |
//(add numArray (nth (nth board row) col))) | |
) | |
) | |
) | |
) | |
)) | |
(function solve[ :Int index] | |
(if (= index m) | |
(return true)) | |
(let [row (nth emptyCells index)] | |
(doFor num (range 1 (+ n 1)) | |
(.toString num) | |
(if (nth rows row).exists num | |
(continue )) | |
(if (nth cols col).exists num | |
(continue )) | |
(if (nth grids (getSubGrid row col)) | |
(continue ) | |
) | |
(set (nth (nth board row) col num)) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment