Compare commits
	
		
			2 Commits
		
	
	
		
			67fd4e2085
			...
			5b21b1db58
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5b21b1db58 | |||
| 88321a2614 | 
							
								
								
									
										34
									
								
								day_1.sml
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								day_1.sml
									
									
									
									
									
								
							| @ -1,6 +1,6 @@ | ||||
| fun sortAsc (l: int list) : int list = ListMergeSort.sort (op >) l | ||||
| fun sortAsc (l : int list) : int list = ListMergeSort.sort (op >) l | ||||
| 
 | ||||
| fun sum (l: int list) : int = foldl (op +) 0 l | ||||
| fun sum (l : int list) : int = foldl (op +) 0 l | ||||
| 
 | ||||
| fun substrToInt (substr: substring) : int = valOf (Int.fromString (Substring.string substr)) | ||||
| 
 | ||||
| @ -11,18 +11,18 @@ TextIO.closeIn file; | ||||
| val lines = String.tokens (fn (char) => char = #"\n") input | ||||
| 
 | ||||
| val pairs = map | ||||
|     (fn (line) => | ||||
|         let val substr = Substring.extract (line, 0, NONE) | ||||
|         in [ | ||||
|             substrToInt (Substring.takel Char.isDigit substr), | ||||
|             substrToInt (Substring.taker Char.isDigit substr) | ||||
|         ] end | ||||
|     ) | ||||
|     lines | ||||
|   (fn (line) => | ||||
|     let val substr = Substring.extract (line, 0, NONE) | ||||
|     in [ | ||||
|       substrToInt (Substring.takel Char.isDigit substr), | ||||
|       substrToInt (Substring.taker Char.isDigit substr) | ||||
|     ] end | ||||
|   ) | ||||
|   lines | ||||
| 
 | ||||
| val lists = ( | ||||
|     sortAsc (map (fn (pair) => hd pair) pairs), | ||||
|     sortAsc (map (fn (pair) => List.last pair) pairs) | ||||
|   sortAsc (map (fn (pair) => hd pair) pairs), | ||||
|   sortAsc (map (fn (pair) => List.last pair) pairs) | ||||
| ) | ||||
| 
 | ||||
| (* Part 1 *) | ||||
| @ -34,11 +34,11 @@ val totalDistance = sum distances | ||||
| (* Part 2 *) | ||||
| 
 | ||||
| val similarities = ListPair.map | ||||
|     (fn (n1, n2) => | ||||
|         let val frequency = List.length (List.filter (fn (n) => n = n1) (#2 lists)) | ||||
|         in n1 * frequency end | ||||
|     ) | ||||
|     lists | ||||
|   (fn (n1, n2) => | ||||
|     let val frequency = List.length (List.filter (fn (n) => n = n1) (#2 lists)) | ||||
|     in n1 * frequency end | ||||
|   ) | ||||
|   lists | ||||
| 
 | ||||
| val similarityScore = sum similarities | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										22
									
								
								day_2.sml
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								day_2.sml
									
									
									
									
									
								
							| @ -1,14 +1,14 @@ | ||||
| fun deleteNth (l : int list, n : int) = List.take (l, n) @ List.drop (l, n + 1) | ||||
| 
 | ||||
| fun isDecreasing (l : int list) = | ||||
|     ListPair.all (fn (a, b) => b < a andalso b >= a - 3) (l, tl l) | ||||
|   ListPair.all (fn (a, b) => b < a andalso b >= a - 3) (l, tl l) | ||||
| 
 | ||||
| fun isIncreasing (l : int list) = | ||||
|     ListPair.all (fn (a, b) => b > a andalso b <= a + 3) (l, tl l) | ||||
|   ListPair.all (fn (a, b) => b > a andalso b <= a + 3) (l, tl l) | ||||
| 
 | ||||
| fun withTolerance (f : (int list) -> bool) = fn (l : int list) => | ||||
|     let val withRemoved = List.tabulate (List.length l, fn (i) => deleteNth (l, i)) | ||||
|     in List.exists f withRemoved end | ||||
|   let val withRemoved = List.tabulate (List.length l, fn (i) => deleteNth (l, i)) | ||||
|   in List.exists f withRemoved end | ||||
| 
 | ||||
| val file = TextIO.openIn "day_2.txt" | ||||
| val input = TextIO.inputAll file; | ||||
| @ -17,11 +17,11 @@ TextIO.closeIn file; | ||||
| val lines = String.tokens (fn (char) => char = #"\n") input | ||||
| 
 | ||||
| val reports = map | ||||
|     (fn (line) => | ||||
|         let val numbers = String.tokens Char.isSpace line | ||||
|         in map (fn (n) => valOf (Int.fromString n)) numbers end | ||||
|     ) | ||||
|     lines | ||||
|   (fn (line) => | ||||
|     let val numbers = String.tokens Char.isSpace line | ||||
|     in map (fn (n) => valOf (Int.fromString n)) numbers end | ||||
|   ) | ||||
|   lines | ||||
| 
 | ||||
| (* Part 1 *) | ||||
| 
 | ||||
| @ -32,8 +32,8 @@ val safeAmount = List.length safeReports | ||||
| (* Part 2 *) | ||||
| 
 | ||||
| val toleratedReports = List.filter | ||||
|     (fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report) | ||||
|     reports | ||||
|   (fn (report) => withTolerance isDecreasing report orelse withTolerance isIncreasing report) | ||||
|   reports | ||||
| 
 | ||||
| val toleratedAmount = List.length toleratedReports | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										30
									
								
								day_3.sml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								day_3.sml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| fun numberAt (i : int) = fn (s : string) => | ||||
|   if i >= String.size s then NONE | ||||
|   else SOME (Substring.takel Char.isDigit (Substring.extract (s, i, NONE))) | ||||
| 
 | ||||
| fun substrToInt (n : substring) = valOf (Int.fromString (Substring.string n)) | ||||
| 
 | ||||
| fun sum (l : int list) : int = foldl (op +) 0 l | ||||
| 
 | ||||
| val file = TextIO.openIn "day_3.txt" | ||||
| val input = TextIO.inputAll file; | ||||
| TextIO.closeIn file; | ||||
| 
 | ||||
| val instructions = List.tabulate ((size input), (fn i => | ||||
|   let | ||||
|     val n1 = numberAt (i + 4) input | ||||
|     val n2 = Option.mapPartial (fn n => numberAt (i + 5 + Substring.size n) input) n1 | ||||
|   in | ||||
|     if isSome n1 andalso isSome n2 andalso | ||||
|       String.substring (input, i, 4) = "mul(" andalso | ||||
|       String.sub (input, i + 4 + Substring.size (valOf n1)) = #"," andalso | ||||
|       String.sub (input, i + 5 + Substring.size (valOf n1) + Substring.size (valOf n2)) = #")" | ||||
|     then SOME (n1, n2) else NONE | ||||
| end)) | ||||
| 
 | ||||
| val instructions = List.mapPartial | ||||
|   (fn (num) => case num of SOME (SOME x, SOME y) => SOME (substrToInt x, substrToInt y) | _ => NONE) | ||||
|   instructions | ||||
| 
 | ||||
| val result = sum (map (op *) instructions) | ||||
| 
 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user