Skip to content

Commit ed6a1d5

Browse files
committed
Implement The Grand Optimizer (#6)
1 parent 75ba248 commit ed6a1d5

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/Optimizations.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,32 @@
44
-- version. It's up to you to figure out if the result is better than the
55
-- input, and if you should re-run optimizations to find new occurrences of
66
-- patterns.
7+
--
8+
-- WARNING: For now, these functions assume that there is only one nanobot!
79
module Optimizations where
810

11+
import Control.Monad (forM)
12+
913
import Trace
1014

15+
import Debug.Trace
16+
17+
-- | Returns all possible optimizations of a given command sequence.
18+
optimize :: [Command] -> [[Command]]
19+
optimize [] = []
20+
optimize cmds = go [cmds] [cmds]
21+
where
22+
go :: [[Command]] -> [[Command]] -> [[Command]]
23+
go [] optimized = optimized
24+
go (t:stack) optimized =
25+
let
26+
new = map (\f -> f t) optimizers
27+
new' = filter (/= t) new
28+
in go (new' ++ stack) (new' ++ optimized)
29+
30+
optimizers :: [[Command] -> [Command]]
31+
optimizers = [splitLMove, mergeSMoves]
32+
1133
-- | Turns [LMove a b, Wait] into [SMove a, SMove b]
1234
--
1335
-- The latter consumes 4 less energy points than the former.

0 commit comments

Comments
 (0)