-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsol_07.aes
85 lines (75 loc) · 3.66 KB
/
sol_07.aes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@compiler >= 4.2.0
include "List.aes"
include "/Users/hans/Personal/Repos/AoC2019/07/Intcode.aes"
contract Day7 =
type process = Intcode.process
type program = Intcode.program
entrypoint solve_1() =
solve([0, 1, 2, 3, 4])
entrypoint solve_2() =
solve([5, 6, 7, 8, 9])
function solve(phases : list(int)) =
let m0 = Intcode.setup_mem(code())
let p0 = {pc = 0, mem = m0, input = [], output = [], route = 0, status = Intcode.Run}
let prog = {[1] = p0{route = 2},
[2] = p0{route = 3},
[3] = p0{route = 4},
[4] = p0{route = 5},
[5] = p0{route = 1}}
let cfgs = permutations(phases)
tune_amps(prog, cfgs, (0, []))
function
tune_amps(prog, [], best) = best
tune_amps(prog, cfg :: cfgs, best) =
let prog' = Intcode.run_prog([1, 2, 3, 4, 5], setup_prog(prog, cfg))
switch((prog'[5].output, best))
([n], (m, cfg')) =>
if(n < m) tune_amps(prog, cfgs, best)
else tune_amps(prog, cfgs, (n, cfg))
function setup_prog(prog, [ph1, ph2, ph3, ph4, ph5]) =
prog{[1] @ p = p{input = [ph1, 0]},
[2] @ p = p{input = [ph2]},
[3] @ p = p{input = [ph3]},
[4] @ p = p{input = [ph4]},
[5] @ p = p{input = [ph5]}}
function
permutations : (list('a)) => list(list('a))
permutations([]) = [[]]
permutations(xs) =
[ hd :: tl | hd <- xs, tl <- permutations(delete(hd, xs)) ]
function
delete : ('a, list('a)) => list('a)
delete(_, []) = []
delete(x, x' :: xs) =
if(x == x') xs
else x' :: delete(x, xs)
function code1_0() =
[3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
function code2_0() =
[3, 26, 1001, 26, -4, 26, 3, 27, 1002, 27, 2, 27, 1, 27, 26,
27, 4, 27, 1001, 28, -1, 28, 1005, 28, 6, 99, 0, 0, 5]
function code() =
[3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 38, 63, 72, 81, 106, 187, 268,
349, 430, 99999, 3, 9, 101, 5, 9, 9, 1002, 9, 3, 9, 101, 3, 9, 9, 4, 9,
99, 3, 9, 102, 3, 9, 9, 101, 4, 9, 9, 1002, 9, 2, 9, 1001, 9, 2, 9, 1002,
9, 4, 9, 4, 9, 99, 3, 9, 1001, 9, 3, 9, 4, 9, 99, 3, 9, 102, 5, 9, 9, 4,
9, 99, 3, 9, 102, 4, 9, 9, 1001, 9, 2, 9, 1002, 9, 5, 9, 1001, 9, 2, 9,
102, 3, 9, 9, 4, 9, 99, 3, 9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4,
9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 102, 2, 9,
9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101,
1, 9, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 99, 3,
9, 1001, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4,
9, 3, 9, 102, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 1001, 9, 1,
9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 101,
1, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 99, 3, 9, 102, 2, 9, 9, 4, 9, 3,
9, 1001, 9, 2, 9, 4, 9, 3, 9, 1002, 9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