-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.u
59 lines (49 loc) · 3.67 KB
/
day7.u
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
---
namespace day7 where
select : [a] -> [(a, [a])]
select xs = case xs of
x +: xs -> (x, xs) +: List.map (p -> case p of (y, ys) -> (y, x +: ys)) (select xs)
_ -> []
permutations : [a] -> [[a]]
permutations xs = case xs of
[] -> [[]]
xs ->
f : (a, [a]) -> [[a]]
f s = case s of (y, ys) -> List.map (zs -> y +: zs) (permutations ys)
select xs
|> List.flatMap f
runIntCode : [Nat] -> [Int] -> Int
runIntCode phases input =
go signal ps =
case ps of
h +: t ->
newSignal = day5.newIntCode'' h signal input
go (day5.abs newSignal) t
[] -> Nat.toInt signal
go 0 phases
max : [Int] -> Optional Int
max input =
go curr as = case as of
h +: t -> if h > curr then go h t else go curr t
[] -> curr
max = go Int.minInt input
if max == Int.minInt then None else Some max
maxThrusterSignal : Nat -> [Int] -> Optional Int
maxThrusterSignal nbPhases input =
List.range 0 (nbPhases + 1)
|> permutations
|> List.map (s -> runIntCode s input)
|> max
> select (List.range 0 4)
> permutations (List.range 0 4)
> maxThrusterSignal 4 [+3,+15,+3,+16,+1002,+16,+10,+16,+1,+16,+15,+15,+4,+15,+99,+0,+0]
> maxThrusterSignal 4 [+3,+23,+3,+24,+1002,+24,+10,+24,+1002,+23,-1,+23,+101,+5,+23,+23,+1,+24,+23,+23,+4,+23,+99,+0,+0]
> max [+3,+4,+1]
test> tests.day7.maxThrusterSignal.ex1 = run ( expect (
maxThrusterSignal 4 [+3,+15,+3,+16,+1002,+16,+10,+16,+1,+16,+15,+15,+4,+15,+99,+0,+0] == Some +43210))
test> tests.day7.maxThrusterSignal.ex2 = run ( expect (
maxThrusterSignal 4 [+3,+23,+3,+24,+1002,+24,+10,+24,+1002,+23,-1,+23,+101,+5,+23,+23,+1,+24,+23,+23,+4,+23,+99,+0,+0] == Some +54321))
test> tests.day7.maxThrusterSignal.ex3 = run ( expect (
maxThrusterSignal 4 [+3,+31,+3,+32,+1002,+32,+10,+32,+1001,+31,-2,+31,+1007,+31,+0,+33,+1002,+33,+7,+33,+1,+33,+31,+31,+1,+32,+31,+31,+4,+31,+99,+0,+0,+0] == Some +65210))
input7 = [+3,+8,+1001,+8,+10,+8,+105,+1,+0,+0,+21,+46,+59,+84,+93,+110,+191,+272,+353,+434,+99999,+3,+9,+101,+2,+9,+9,+102,+3,+9,+9,+1001,+9,+5,+9,+102,+4,+9,+9,+1001,+9,+4,+9,+4,+9,+99,+3,+9,+101,+3,+9,+9,+102,+5,+9,+9,+4,+9,+99,+3,+9,+1001,+9,+4,+9,+1002,+9,+2,+9,+101,+2,+9,+9,+102,+2,+9,+9,+1001,+9,+3,+9,+4,+9,+99,+3,+9,+1002,+9,+2,+9,+4,+9,+99,+3,+9,+102,+2,+9,+9,+1001,+9,+5,+9,+1002,+9,+3,+9,+4,+9,+99,+3,+9,+102,+2,+9,+9,+4,+9,+3,+9,+102,+2,+9,+9,+4,+9,+3,+9,+102,+2,+9,+9,+4,+9,+3,+9,+102,+2,+9,+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,+3,+9,+101,+2,+9,+9,+4,+9,+3,+9,+1001,+9,+1,+9,+4,+9,+3,+9,+101,+2,+9,+9,+4,+9,+99,+3,+9,+102,+2,+9,+9,+4,+9,+3,+9,+1002,+9,+2,+9,+4,+9,+3,+9,+1002,+9,+2,+9,+4,+9,+3,+9,+1001,+9,+1,+9,+4,+9,+3,+9,+1001,+9,+2,+9,+4,+9,+3,+9,+101,+1,+9,+9,+4,+9,+3,+9,+1001,+9,+2,+9,+4,+9,+3,+9,+1002,+9,+2,+9,+4,+9,+3,+9,+1001,+9,+1,+9,+4,+9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