-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.u
92 lines (82 loc) · 12.5 KB
/
day23.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
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
86
87
88
89
90
91
92
---
> input23 |> init |> network
network : Network -> Either Packet Packet
network n =
go n = case Network.ps n of
h +: t ->
if (Packet.to h) == +255 then h |> Right
else
case Network.ss n |> Map.lookup (Packet.to h) of
Some s ->
news = IsState.input.set (toInputs h) s |> run
Network.ss.modify (Map.insert (Packet.to h) news) n
|> Network.ps.modify (ps -> ps ++ (IsState.out news |> parsePackets))
|> go
_ -> h |> Left
_ -> Packet +0 +0 +0 |> Left
go n
boot : Network -> Network
boot n =
newss =
Network.ss n
|> Map.toList
|> List.map (p -> case p of (i, s) -> (i, IsState.input.set [-1] s |> run))
newps =
newss
|> List.flatMap (p -> case p of (i, s) -> IsState.out s |> parsePackets)
newss |> Map.fromList |> (m -> Network.ss.set m n) |> Network.ps.modify (ps -> ps ++ newps)
init : [Int] -> Network
init program =
p =
List.indexed program
|> List.map (p -> case p of (e, i) -> (Nat.toInt i, e))
|> Map.fromList
List.range 0 50
|> List.map (i -> (Nat.toInt i, IsState p +0 +0 [] [Nat.toInt i] |> run))
|> Map.fromList
|> (ss -> Network ss [])
--type Computer = { s : IsState, ps : [Packet] }
--type Packet = { to : Int, x : Int, y : Int }
type Network = { ss : Map Int IsState, ps : [Packet] }
toInputs : Packet -> [Int]
toInputs p = [Packet.x p, Packet.y p]
parsePackets : [Int] -> [Packet]
parsePackets os = case os of
[to, x, y] ++ t -> (Packet to x y) +: parsePackets t
_ -> []
run : IsState -> IsState
run s =
go' s =
case go s of
(s', true) -> go' s'
(s', false) -> s'
clear s |> go'
clear : IsState -> IsState
clear s = IsState.out.set [] s
go : IsState -> (IsState, Boolean)
go s = case parseOp s of (op, modes) -> case op of
1 -> (binary (Int.+) modes s, true)
2 -> (binary (Int.*) modes s, true)
3 ->
case List.uncons (IsState.input s) of
Some (h, t) ->
IsState.program.modify (Map.insert (nonPos +1 modes s) h) s
|> IsState.input.set t
|> IsState.pointer.modify (p -> p + +2)
|> (s -> (s, true))
_ -> (s, false)
4 ->
IsState.pointer.modify (p -> p + +2) s
|> IsState.out.modify (acc -> (pos +1 modes s) +: acc)
|> (s -> (s, true))
5 -> (branch (i -> not (i == +0)) modes s, true)
6 -> (branch (i -> i == +0) modes s, true)
7 -> (store Int.lt modes s, true)
8 -> (store (Int.==) modes s, true)
9 ->
IsState.base.modify (b -> b + pos +1 modes s) s
|> IsState.pointer.modify (p -> p + +2)
|> (s -> (s, true))
99 -> (s, false)
_ -> (s, false)
input23 = [+3,+62,+1001,+62,+11,+10,+109,+2249,+105,+1,+0,+1610,+884,+1261,+1063,+1125,+709,+960,+2216,+752,+1919,+2127,+1857,+1956,+1888,+915,+2158,+991,+639,+674,+1758,+1517,+1154,+1725,+1292,+1548,+1393,+602,+1688,+1995,+1094,+571,+2187,+1360,+1824,+1579,+1327,+814,+1486,+1787,+1026,+1230,+851,+2059,+2094,+1651,+1424,+2028,+783,+1189,+1455,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+3,+64,+1008,+64,-1,+62,+1006,+62,+88,+1006,+61,+170,+1106,+0,+73,+3,+65,+21002,+64,+1,+1,+20102,+1,+66,+2,+21101,+0,+105,+0,+1106,+0,+436,+1201,+1,-1,+64,+1007,+64,+0,+62,+1005,+62,+73,+7,+64,+67,+62,+1006,+62,+73,+1002,+64,+2,+133,+1,+133,+68,+133,+101,+0,+0,+62,+1001,+133,+1,+140,+8,+0,+65,+63,+2,+63,+62,+62,+1005,+62,+73,+1002,+64,+2,+161,+1,+161,+68,+161,+1102,+1,+1,+0,+1001,+161,+1,+169,+101,+0,+65,+0,+1102,+1,+1,+61,+1102,+1,+0,+63,+7,+63,+67,+62,+1006,+62,+203,+1002,+63,+2,+194,+1,+68,+194,+194,+1006,+0,+73,+1001,+63,+1,+63,+1105,+1,+178,+21102,+210,+1,+0,+106,+0,+69,+1202,+1,+1,+70,+1101,+0,+0,+63,+7,+63,+71,+62,+1006,+62,+250,+1002,+63,+2,+234,+1,+72,+234,+234,+4,+0,+101,+1,+234,+240,+4,+0,+4,+70,+1001,+63,+1,+63,+1105,+1,+218,+1105,+1,+73,+109,+4,+21102,+0,+1,-3,+21102,+1,+0,-2,+20207,-2,+67,-1,+1206,-1,+293,+1202,-2,+2,+283,+101,+1,+283,+283,+1,+68,+283,+283,+22001,+0,-3,-3,+21201,-2,+1,-2,+1105,+1,+263,+22101,+0,-3,-3,+109,-4,+2106,+0,+0,+109,+4,+21101,+0,+1,-3,+21102,+0,+1,-2,+20207,-2,+67,-1,+1206,-1,+342,+1202,-2,+2,+332,+101,+1,+332,+332,+1,+68,+332,+332,+22002,+0,-3,-3,+21201,-2,+1,-2,+1105,+1,+312,+21201,-3,+0,-3,+109,-4,+2106,+0,+0,+109,+1,+101,+1,+68,+359,+20101,+0,+0,+1,+101,+3,+68,+367,+20102,+1,+0,+2,+21102,+376,+1,+0,+1105,+1,+436,+22101,+0,+1,+0,+109,-1,+2105,+1,+0,+1,+2,+4,+8,+16,+32,+64,+128,+256,+512,+1024,+2048,+4096,+8192,+16384,+32768,+65536,+131072,+262144,+524288,+1048576,+2097152,+4194304,+8388608,+16777216,+33554432,+67108864,+134217728,+268435456,+536870912,+1073741824,+2147483648,+4294967296,+8589934592,+17179869184,+34359738368,+68719476736,+137438953472,+274877906944,+549755813888,+1099511627776,+2199023255552,+4398046511104,+8796093022208,+17592186044416,+35184372088832,+70368744177664,+140737488355328,+281474976710656,+562949953421312,+1125899906842624,+109,+8,+21202,-6,+10,-5,+22207,-7,-5,-5,+1205,-5,+521,+21102,+0,+1,-4,+21101,+0,+0,-3,+21102,+1,+51,-2,+21201,-2,-1,-2,+1201,-2,+385,+471,+20101,+0,+0,-1,+21202,-3,+2,-3,+22207,-7,-1,-5,+1205,-5,+496,+21201,-3,+1,-3,+22102,-1,-1,-5,+22201,-7,-5,-7,+22207,-3,-6,-5,+1205,-5,+515,+22102,-1,-6,-5,+22201,-3,-5,-3,+22201,-1,-4,-4,+1205,-2,+461,+1105,+1,+547,+21102,+1,-1,-4,+21202,-6,-1,-6,+21207,-7,+0,-5,+1205,-5,+547,+22201,-7,-6,-7,+21201,-4,+1,-4,+1106,+0,+529,+22101,+0,-4,-7,+109,-8,+2106,+0,+0,+109,+1,+101,+1,+68,+563,+21001,+0,+0,+0,+109,-1,+2105,+1,+0,+1102,+1,+68161,+66,+1102,+1,+1,+67,+1102,+598,+1,+68,+1102,+1,+556,+69,+1101,+0,+1,+71,+1102,+600,+1,+72,+1106,+0,+73,+1,+1451,+23,+63667,+1101,+36559,+0,+66,+1102,+4,+1,+67,+1102,+1,+629,+68,+1101,+253,+0,+69,+1102,+1,+1,+71,+1101,+637,+0,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+0,+0,+33,+63761,+1101,+0,+15299,+66,+1101,+3,+0,+67,+1101,+0,+666,+68,+1101,+302,+0,+69,+1101,+1,+0,+71,+1102,+672,+1,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+39,+34754,+1101,+9967,+0,+66,+1102,+1,+3,+67,+1101,+701,+0,+68,+1102,+302,+1,+69,+1101,+1,+0,+71,+1101,+707,+0,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+26,+36559,+1101,+0,+2473,+66,+1102,+1,+1,+67,+1101,+736,+0,+68,+1102,+1,+556,+69,+1102,+7,+1,+71,+1102,+1,+738,+72,+1106,+0,+73,+1,+2,+17,+15299,+12,+179534,+12,+359068,+33,+127522,+23,+127334,+48,+5573,+48,+11146,+1101,+0,+6661,+66,+1102,+1,+1,+67,+1102,+1,+779,+68,+1102,+1,+556,+69,+1102,+1,+1,+71,+1102,+1,+781,+72,+1106,+0,+73,+1,+107,+9,+3543,+1101,+16073,+0,+66,+1101,+0,+1,+67,+1102,+1,+810,+68,+1102,+1,+556,+69,+1102,+1,+1,+71,+1102,+812,+1,+72,+1105,+1,+73,+1,+521,+18,+9967,+1101,+0,+54059,+66,+1101,+0,+1,+67,+1102,+841,+1,+68,+1102,+556,+1,+69,+1102,+1,+4,+71,+1102,+1,+843,+72,+1105,+1,+73,+1,+5,+9,+1181,+44,+91394,+44,+137091,+48,+27865,+1101,+0,+77687,+66,+1101,+0,+2,+67,+1101,+878,+0,+68,+1101,+0,+302,+69,+1101,+0,+1,+71,+1102,+882,+1,+72,+1105,+1,+73,+0,+0,+0,+0,+17,+45897,+1102,+1,+97169,+66,+1102,+1,+1,+67,+1101,+911,+0,+68,+1102,+556,+1,+69,+1102,+1,+1,+71,+1101,+913,+0,+72,+1106,+0,+73,+1,+421,+42,+308949,+1102,+1,+39079,+66,+1101,+1,+0,+67,+1101,+942,+0,+68,+1102,+556,+1,+69,+1102,+8,+1,+71,+1101,+0,+944,+72,+1105,+1,+73,+1,+1,+9,+4724,+41,+155374,+17,+30598,+35,+85502,+28,+164722,+18,+19934,+38,+334652,+42,+205966,+1102,+1,+44657,+66,+1102,+1,+1,+67,+1101,+987,+0,+68,+1102,+1,+556,+69,+1101,+0,+1,+71,+1102,+989,+1,+72,+1105,+1,+73,+1,-78,+42,+102983,+1102,+1693,+1,+66,+1102,+3,+1,+67,+1102,+1,+1018,+68,+1102,+302,+1,+69,+1102,+1,+1,+71,+1101,+0,+1024,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+27,+182708,+1102,+1,+17377,+66,+1101,+4,+0,+67,+1101,+1053,+0,+68,+1102,+1,+253,+69,+1102,+1,+1,+71,+1102,+1061,+1,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+0,+0,+22,+65306,+1102,+1,+103171,+66,+1101,+1,+0,+67,+1102,+1090,+1,+68,+1101,+556,+0,+69,+1102,+1,+1,+71,+1101,+1092,+0,+72,+1105,+1,+73,+1,+7,+38,+83663,+1101,+0,+21773,+66,+1102,+1,+1,+67,+1102,+1,+1121,+68,+1101,+556,+0,+69,+1101,+1,+0,+71,+1102,+1123,+1,+72,+1106,+0,+73,+1,+314,+38,+250989,+1101,+11171,+0,+66,+1101,+0,+1,+67,+1101,+0,+1152,+68,+1101,+556,+0,+69,+1101,+0,+0,+71,+1101,+1154,+0,+72,+1105,+1,+73,+1,+1410,+1102,+103391,+1,+66,+1101,+3,+0,+67,+1102,+1,+1181,+68,+1102,+302,+1,+69,+1102,+1,+1,+71,+1102,+1187,+1,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+27,+137031,+1101,+0,+5573,+66,+1102,+1,+6,+67,+1101,+1216,+0,+68,+1101,+302,+0,+69,+1101,+0,+1,+71,+1101,+1228,+0,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+43,+126794,+1102,+104473,+1,+66,+1101,+0,+1,+67,+1101,+0,+1257,+68,+1102,+1,+556,+69,+1102,+1,+1,+71,+1102,+1,+1259,+72,+1105,+1,+73,+1,+52861,+35,+42751,+1101,+0,+92657,+66,+1101,+0,+1,+67,+1101,+1288,+0,+68,+1102,+1,+556,+69,+1102,+1,+1,+71,+1101,+1290,+0,+72,+1106,+0,+73,+1,-8004,+39,+52131,+1101,+63667,+0,+66,+1101,+3,+0,+67,+1101,+0,+1319,+68,+1101,+0,+302,+69,+1102,+1,+1,+71,+1101,+1325,+0,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+7,+116738,+1101,+0,+42751,+66,+1102,+2,+1,+67,+1102,+1354,+1,+68,+1101,+0,+302,+69,+1101,+1,+0,+71,+1102,+1358,+1,+72,+1106,+0,+73,+0,+0,+0,+0,+28,+82361,+1101,+65053,+0,+66,+1101,+0,+1,+67,+1101,+0,+1387,+68,+1102,+1,+556,+69,+1102,+2,+1,+71,+1101,+0,+1389,+72,+1105,+1,+73,+1,+10,+44,+182788,+48,+33438,+1101,+0,+1279,+66,+1102,+1,+1,+67,+1102,+1420,+1,+68,+1102,+556,+1,+69,+1102,+1,+1,+71,+1102,+1,+1422,+72,+1105,+1,+73,+1,+125,+44,+45697,+1102,+33493,+1,+66,+1102,+1,+1,+67,+1101,+0,+1451,+68,+1101,+0,+556,+69,+1101,+0,+1,+71,+1101,+0,+1453,+72,+1105,+1,+73,+1,+37951,+41,+77687,+1102,+1,+857,+66,+1101,+1,+0,+67,+1101,+0,+1482,+68,+1101,+0,+556,+69,+1101,+0,+1,+71,+1102,+1,+1484,+72,+1106,+0,+73,+1,+419,+12,+89767,+1102,+72977,+1,+66,+1102,+1,+1,+67,+1101,+1513,+0,+68,+1102,+1,+556,+69,+1102,+1,+1,+71,+1101,+1515,+0,+72,+1105,+1,+73,+1,+160,+48,+22292,+1102,+1,+64853,+66,+1101,+1,+0,+67,+1101,+1544,+0,+68,+1101,+0,+556,+69,+1101,+0,+1,+71,+1102,+1546,+1,+72,+1105,+1,+73,+1,-918,+26,+73118,+1101,+71999,+0,+66,+1102,+1,+1,+67,+1102,+1,+1575,+68,+1101,+556,+0,+69,+1102,+1,+1,+71,+1101,+1577,+0,+72,+1106,+0,+73,+1,-32,+18,+29901,+1102,+1,+11783,+66,+1102,+1,+1,+67,+1101,+1606,+0,+68,+1102,+1,+556,+69,+1102,+1,+1,+71,+1101,+0,+1608,+72,+1106,+0,+73,+1,+6976,+39,+17377,+1102,+1,+102877,+66,+1102,+1,+1,+67,+1101,+1637,+0,+68,+1102,+1,+556,+69,+1101,+0,+6,+71,+1102,+1639,+1,+72,+1106,+0,+73,+1,+17013,+7,+58369,+16,+1693,+16,+5079,+21,+103391,+21,+206782,+21,+310173,+1101,+45697,+0,+66,+1101,+4,+0,+67,+1101,+0,+1678,+68,+1101,+302,+0,+69,+1102,+1,+1,+71,+1101,+0,+1686,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+0,+0,+48,+16719,+1101,+45677,+0,+66,+1102,+1,+4,+67,+1102,+1,+1715,+68,+1102,+1,+253,+69,+1101,+0,+1,+71,+1101,+0,+1723,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+0,+0,+43,+63397,+1101,+0,+32653,+66,+1101,+0,+2,+67,+1102,+1,+1752,+68,+1101,+0,+302,+69,+1102,+1,+1,+71,+1101,+1756,+0,+72,+1105,+1,+73,+0,+0,+0,+0,+12,+448835,+1102,+1,+77999,+66,+1101,+0,+1,+67,+1102,+1,+1785,+68,+1101,+0,+556,+69,+1101,+0,+0,+71,+1101,+1787,+0,+72,+1106,+0,+73,+1,+1070,+1102,+83663,+1,+66,+1101,+0,+4,+67,+1102,+1814,+1,+68,+1101,+302,+0,+69,+1101,+0,+1,+71,+1102,+1,+1822,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+0,+0,+26,+146236,+1101,+0,+63761,+66,+1101,+2,+0,+67,+1101,+0,+1851,+68,+1101,+302,+0,+69,+1102,+1,+1,+71,+1101,+0,+1855,+72,+1106,+0,+73,+0,+0,+0,+0,+23,+191001,+1102,+1,+91997,+66,+1101,+0,+1,+67,+1102,+1884,+1,+68,+1101,+556,+0,+69,+1101,+1,+0,+71,+1101,+0,+1886,+72,+1105,+1,+73,+1,+842,+12,+269301,+1101,+97387,+0,+66,+1102,+1,+1,+67,+1102,+1915,+1,+68,+1102,+556,+1,+69,+1102,+1,+1,+71,+1101,+1917,+0,+72,+1106,+0,+73,+1,+13,+22,+32653,+1101,+1181,+0,+66,+1101,+4,+0,+67,+1101,+1946,+0,+68,+1102,+302,+1,+69,+1102,+1,+1,+71,+1102,+1954,+1,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+0,+0,+39,+69508,+1101,+89767,+0,+66,+1102,+5,+1,+67,+1101,+1983,+0,+68,+1101,+0,+302,+69,+1101,+1,+0,+71,+1101,+0,+1993,+72,+1106,+0,+73,+0,+0,+0,+0,+0,+0,+0,+0,+0,+0,+27,+91354,+1101,+82361,+0,+66,+1101,+2,+0,+67,+1102,+1,+2022,+68,+1101,+0,+302,+69,+1102,+1,+1,+71,+1102,+2026,+1,+72,+1106,+0,+73,+0,+0,+0,+0,+26,+109677,+1102,+1,+33679,+66,+1102,+1,+1,+67,+1102,+2055,+1,+68,+1101,+556,+0,+69,+1101,+1,+0,+71,+1102,+2057,+1,+72,+1105,+1,+73,+1,+67,+38,+167326,+1101,+0,+102983,+66,+1101,+0,+3,+67,+1101,+0,+2086,+68,+1101,+0,+302,+69,+1102,+1,+1,+71,+1102,+1,+2092,+72,+1105,+1,+73,+0,+0,+0,+0,+0,+0,+16,+3386,+1102,+1,+63397,+66,+1101,+0,+2,+67,+1102,+1,+2121,+68,+1102,+351,+1,+69,+1102,+1,+1,+71,+1101,+2125,+0,+72,+1106,+0,+73,+0,+0,+0,+0,+255,+102877,+1101,+32969,+0,+66,+1102,+1,+1,+67,+1101,+0,+2154,+68,+1101,+0,+556,+69,+1102,+1,+1,+71,+1102,+1,+2156,+72,+1105,+1,+73,+1,+61,+9,+2362,+1101,+0,+31751,+66,+1101,+0,+1,+67,+1102,+1,+2185,+68,+1102,+556,+1,+69,+1102,+1,+0,+71,+1101,+0,+2187,+72,+1105,+1,+73,+1,+1093,+1101,+50051,+0,+66,+1102,+1,+1,+67,+1102,+2214,+1,+68,+1101,+556,+0,+69,+1102,+1,+0,+71,+1102,+2216,+1,+72,+1106,+0,+73,+1,+1683,+1101,+58369,+0,+66,+1102,+1,+2,+67,+1102,+1,+2243,+68,+1102,+1,+302,+69,+1102,+1,+1,+71,+1101,+0,+2247,+72,+1106,+0,+73,+0,+0,+0,+0,+27,+45677]