-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path23.rs
More file actions
63 lines (59 loc) · 9.46 KB
/
23.rs
File metadata and controls
63 lines (59 loc) · 9.46 KB
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
use std::time::Instant;
use intcoder::{IntCoder, ExitCode};
const PROGRAM: [i64; 2237] = [3,62,1001,62,11,10,109,2237,105,1,0,919,1344,1032,2175,1730,1573,882,1439,1901,773,1866,1761,606,1544,2039,1313,1185,2206,1156,1251,2109,1932,1127,1220,1001,851,2006,1608,814,571,960,1509,1377,1833,639,1798,2078,1975,728,1094,1410,699,1670,670,1699,1639,1476,1284,2140,1065,0,0,0,0,0,0,0,0,0,0,0,0,3,64,1008,64,-1,62,1006,62,88,1006,61,170,1105,1,73,3,65,21002,64,1,1,20101,0,66,2,21102,105,1,0,1105,1,436,1201,1,-1,64,1007,64,0,62,1005,62,73,7,64,67,62,1006,62,73,1002,64,2,132,1,132,68,132,1001,0,0,62,1001,132,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,1002,65,1,0,1102,1,1,61,1101,0,0,63,7,63,67,62,1006,62,203,1002,63,2,194,1,68,194,194,1006,0,73,1001,63,1,63,1106,0,178,21101,0,210,0,105,1,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,1106,0,73,109,4,21102,1,0,-3,21101,0,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,21201,-3,0,-3,109,-4,2106,0,0,109,4,21102,1,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,22102,1,-3,-3,109,-4,2105,1,0,109,1,101,1,68,359,20102,1,0,1,101,3,68,367,20101,0,0,2,21102,376,1,0,1105,1,436,21202,1,1,0,109,-1,2106,0,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,1,0,-4,21102,1,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,21101,-1,0,-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,2105,1,0,109,1,101,1,68,564,20102,1,0,0,109,-1,2105,1,0,1101,18191,0,66,1102,1,3,67,1102,598,1,68,1101,0,253,69,1101,0,1,71,1102,1,604,72,1105,1,73,0,0,0,0,0,0,39,124114,1102,1,35267,66,1101,0,2,67,1101,0,633,68,1102,1,302,69,1101,0,1,71,1102,637,1,72,1106,0,73,0,0,0,0,2,89942,1102,78877,1,66,1102,1,1,67,1102,666,1,68,1101,556,0,69,1102,1,1,71,1101,668,0,72,1106,0,73,1,59,21,465199,1101,42793,0,66,1102,1,1,67,1101,0,697,68,1101,556,0,69,1101,0,0,71,1102,1,699,72,1106,0,73,1,1963,1101,0,7393,66,1102,1,1,67,1102,726,1,68,1102,1,556,69,1101,0,0,71,1101,728,0,72,1105,1,73,1,1141,1102,28081,1,66,1102,1,1,67,1102,755,1,68,1101,0,556,69,1101,8,0,71,1101,0,757,72,1106,0,73,1,1,12,70534,2,44971,1,156158,33,35983,5,167001,6,1471,11,188644,14,28463,1101,0,9059,66,1102,1,1,67,1102,1,800,68,1101,556,0,69,1101,0,6,71,1102,1,802,72,1106,0,73,1,2,21,398742,39,62057,48,179373,14,142315,30,98387,30,196774,1101,45869,0,66,1102,1,4,67,1101,0,841,68,1102,1,302,69,1101,1,0,71,1101,0,849,72,1106,0,73,0,0,0,0,0,0,0,0,30,491935,1101,4759,0,66,1102,1,1,67,1101,0,878,68,1102,1,556,69,1101,1,0,71,1102,880,1,72,1106,0,73,1,14,5,111334,1101,1471,0,66,1101,4,0,67,1101,0,909,68,1102,302,1,69,1102,1,1,71,1101,917,0,72,1105,1,73,0,0,0,0,0,0,0,0,29,54573,1101,26293,0,66,1102,1,1,67,1101,946,0,68,1102,556,1,69,1101,0,6,71,1101,948,0,72,1106,0,73,1,20183,26,82487,10,172138,10,258207,31,20857,31,41714,31,62571,1101,98387,0,66,1101,0,6,67,1102,987,1,68,1101,0,302,69,1102,1,1,71,1101,999,0,72,1105,1,73,0,0,0,0,0,0,0,0,0,0,0,0,32,89158,1101,89071,0,66,1101,0,1,67,1102,1028,1,68,1102,556,1,69,1101,1,0,71,1102,1030,1,72,1105,1,73,1,18947,12,35267,1101,0,44971,66,1102,2,1,67,1101,0,1059,68,1101,302,0,69,1102,1,1,71,1101,0,1063,72,1105,1,73,0,0,0,0,1,78079,1101,79379,0,66,1102,1,1,67,1101,1092,0,68,1101,556,0,69,1101,0,0,71,1102,1,1094,72,1105,1,73,1,1917,1101,62057,0,66,1101,0,2,67,1102,1,1121,68,1101,0,302,69,1101,0,1,71,1102,1,1125,72,1105,1,73,0,0,0,0,48,59791,1101,0,10289,66,1101,0,1,67,1101,1154,0,68,1101,556,0,69,1102,1,0,71,1101,0,1156,72,1105,1,73,1,1630,1101,0,51893,66,1102,1,1,67,1102,1183,1,68,1102,1,556,69,1101,0,0,71,1101,1185,0,72,1106,0,73,1,1144,1101,0,72859,66,1101,0,1,67,1101,0,1212,68,1102,1,556,69,1101,0,3,71,1101,1214,0,72,1106,0,73,1,5,28,91738,28,137607,30,393548,1102,1,12983,66,1101,0,1,67,1102,1,1247,68,1102,556,1,69,1102,1,1,71,1102,1249,1,72,1105,1,73,1,79,11,94322,1102,1,102059,66,1101,0,1,67,1101,1278,0,68,1102,1,556,69,1101,2,0,71,1102,1280,1,72,1106,0,73,1,13,21,199371,14,85389,1102,1,33469,66,1101,1,0,67,1101,0,1311,68,1101,556,0,69,1102,1,0,71,1101,1313,0,72,1105,1,73,1,1521,1101,10399,0,66,1101,1,0,67,1102,1,1340,68,1102,556,1,69,1101,0,1,71,1102,1342,1,72,1106,0,73,1,-177,14,56926,1102,78079,1,66,1102,1,2,67,1102,1,1371,68,1102,302,1,69,1102,1,1,71,1101,1375,0,72,1105,1,73,0,0,0,0,33,71966,1102,44579,1,66,1102,1,2,67,1101,0,1404,68,1102,351,1,69,1101,1,0,71,1102,1408,1,72,1105,1,73,0,0,0,0,255,26293,1102,96469,1,66,1102,1,1,67,1102,1,1437,68,1101,556,0,69,1102,0,1,71,1102,1439,1,72,1106,0,73,1,1517,1102,30851,1,66,1101,0,4,67,1101,1466,0,68,1102,253,1,69,1102,1,1,71,1102,1474,1,72,1106,0,73,0,0,0,0,0,0,0,0,32,44579,1101,89387,0,66,1102,1,1,67,1102,1503,1,68,1101,556,0,69,1102,1,2,71,1101,0,1505,72,1106,0,73,1,10,28,45869,30,295161,1102,1,20857,66,1102,1,3,67,1101,1536,0,68,1101,0,302,69,1102,1,1,71,1102,1542,1,72,1105,1,73,0,0,0,0,0,0,7,61702,1102,84481,1,66,1101,0,1,67,1101,0,1571,68,1102,1,556,69,1102,1,0,71,1101,0,1573,72,1106,0,73,1,1220,1102,55667,1,66,1102,1,3,67,1101,0,1600,68,1102,302,1,69,1101,1,0,71,1102,1606,1,72,1105,1,73,0,0,0,0,0,0,29,18191,1102,997,1,66,1101,1,0,67,1101,0,1635,68,1102,1,556,69,1101,1,0,71,1102,1,1637,72,1106,0,73,1,5068,11,141483,1102,1,73061,66,1102,1,1,67,1101,0,1666,68,1102,556,1,69,1101,0,1,71,1102,1668,1,72,1105,1,73,1,68,11,47161,1102,1,73259,66,1102,1,1,67,1102,1,1697,68,1101,0,556,69,1102,0,1,71,1101,0,1699,72,1105,1,73,1,1939,1101,29059,0,66,1101,0,1,67,1101,0,1726,68,1101,556,0,69,1101,0,1,71,1102,1728,1,72,1106,0,73,1,160,30,590322,1101,0,36161,66,1101,1,0,67,1102,1757,1,68,1101,0,556,69,1101,1,0,71,1101,0,1759,72,1106,0,73,1,283,6,5884,1102,47161,1,66,1101,0,4,67,1102,1788,1,68,1101,302,0,69,1102,1,1,71,1101,1796,0,72,1105,1,73,0,0,0,0,0,0,0,0,29,36382,1101,86197,0,66,1102,1,1,67,1101,1825,0,68,1101,0,556,69,1101,3,0,71,1101,1827,0,72,1105,1,73,1,7,21,66457,48,119582,14,113852,1102,1,35983,66,1101,2,0,67,1102,1860,1,68,1101,0,302,69,1102,1,1,71,1101,1864,0,72,1105,1,73,0,0,0,0,21,265828,1101,0,86069,66,1102,3,1,67,1101,0,1893,68,1101,302,0,69,1102,1,1,71,1102,1,1899,72,1105,1,73,0,0,0,0,0,0,7,123404,1102,1,27691,66,1102,1,1,67,1101,0,1928,68,1102,1,556,69,1102,1,1,71,1102,1,1930,72,1105,1,73,1,8363,5,55667,1101,66457,0,66,1102,7,1,67,1101,0,1959,68,1102,302,1,69,1102,1,1,71,1101,1973,0,72,1105,1,73,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,30851,1102,1,6991,66,1101,0,1,67,1101,2002,0,68,1102,1,556,69,1102,1,1,71,1101,2004,0,72,1106,0,73,1,125,28,183476,1101,0,82487,66,1101,0,2,67,1102,2033,1,68,1101,0,302,69,1102,1,1,71,1102,2037,1,72,1106,0,73,0,0,0,0,7,92553,1101,0,28463,66,1101,5,0,67,1101,2066,0,68,1101,0,302,69,1101,0,1,71,1102,1,2076,72,1106,0,73,0,0,0,0,0,0,0,0,0,0,10,86069,1101,91771,0,66,1101,1,0,67,1101,0,2105,68,1101,556,0,69,1102,1,1,71,1101,2107,0,72,1106,0,73,1,2437,21,332285,1101,103969,0,66,1102,1,1,67,1101,0,2136,68,1102,556,1,69,1102,1,1,71,1102,1,2138,72,1105,1,73,1,17,6,2942,1101,0,59791,66,1101,3,0,67,1101,2167,0,68,1101,0,302,69,1102,1,1,71,1101,0,2173,72,1105,1,73,0,0,0,0,0,0,26,164974,1102,1,18859,66,1101,1,0,67,1101,2202,0,68,1102,556,1,69,1101,1,0,71,1101,2204,0,72,1106,0,73,1,2081,6,4413,1101,0,84179,66,1102,1,1,67,1102,1,2233,68,1102,556,1,69,1101,1,0,71,1102,1,2235,72,1106,0,73,1,4,21,132914];
fn part_one() -> i64 {
let mut net = vec![IntCoder::new(&PROGRAM); 50];
for i in 0..50 { net[i].push_input(i as i64); }
for i in (0..50).cycle() {
match net[i].execute() {
ExitCode::Output(adr) => {
let x = net[i].execute_until_output();
let y = net[i].execute_until_output();
if adr == 255 { return y; }
net[adr as usize].push_input(x);
net[adr as usize].push_input(y);
}
ExitCode::AwaitInput => net[i].push_input(-1),
ExitCode::Halted => unreachable!(),
}
}
unreachable!()
}
fn part_two() -> i64 {
let mut net = vec![IntCoder::new(&PROGRAM); 50];
for i in 0..50 { net[i].push_input(i as i64); }
let (mut nat_x, mut nat_y, mut last_y) = (0,0,1);
loop {
let mut idle = true;
for i in 0..50 {
match net[i].execute() {
ExitCode::Output(adr) => {
let x = net[i].execute_until_output();
let y = net[i].execute_until_output();
if adr == 255 {
nat_x = x;
nat_y = y;
continue;
}
net[adr as usize].push_input(x);
net[adr as usize].push_input(y);
idle = false;
}
ExitCode::AwaitInput => net[i].push_input(-1),
ExitCode::Halted => unreachable!(),
}
}
if idle {
if last_y == nat_y { return nat_y; }
net[0].push_input(nat_x);
net[0].push_input(nat_y);
last_y = nat_y;
}
};
}
fn main() {
let now = Instant::now();
println!("Part one: {}", part_one());
println!("Part two: {}", part_two());
println!("Time: {}ms", now.elapsed().as_millis());
}