Skip to content

Instantly share code, notes, and snippets.

@r2p2
Created December 9, 2017 21:26
Show Gist options
  • Save r2p2/3915a18c3e250b3b21fcacbba909f9f1 to your computer and use it in GitHub Desktop.
Save r2p2/3915a18c3e250b3b21fcacbba909f9f1 to your computer and use it in GitHub Desktop.
Advent Of Code Day 5
struct Cpu {
mem : Vec<i32>,
pc : i32,
cycles : u32,
ext : bool,
}
impl Cpu {
pub fn new(mem : &Vec<i32>, extention : bool) -> Cpu {
Cpu {
mem : mem.clone(),
pc : 0,
cycles : 0,
ext : extention,
}
}
pub fn step(&mut self) {
if self.is_oom() {
return
}
let rel_dst = self.mem[self.pc as usize];
if ! self.ext || rel_dst < 3 {
self.mem[self.pc as usize] = self.mem[self.pc as usize] + 1;
}
else {
self.mem[self.pc as usize] = self.mem[self.pc as usize] - 1;
}
self.pc += rel_dst;
self.cycles += 1;
}
pub fn is_oom(&self) -> bool {
self.pc < 0 || self.pc as usize >= self.mem.len()
}
pub fn cycles(&self) -> u32 {
self.cycles
}
pub fn show(&self) {
println!("cpu: {:?}", self.mem);
}
}
fn main() {
let test_data = vec![0, 3, 0, 1, -3];
let prod_data = vec![
0,
2,
0,
0,
-2,
-2,
-1,
-4,
-5,
-6,
0,
1,
-5,
-3,
-10,
-8,
-2,
-13,
-14,
-15,
-8,
-5,
-13,
-16,
-21,
-3,
-14,
-23,
-9,
-11,
-19,
-29,
-2,
-20,
-28,
1,
-3,
-35,
1,
-20,
-4,
-37,
-11,
-27,
-33,
-43,
-20,
-5,
-9,
-22,
-47,
-5,
-49,
-13,
-22,
-2,
-2,
-51,
-53,
-22,
-38,
-16,
-37,
-30,
-49,
-48,
-35,
-5,
-42,
-21,
-31,
-61,
-43,
-31,
-72,
-35,
-3,
-31,
-65,
-78,
2,
-17,
-80,
-10,
-6,
-68,
-69,
-44,
-71,
-78,
-89,
-19,
-22,
-28,
-21,
-7,
-54,
-63,
-48,
-70,
-73,
-52,
-47,
-49,
-2,
-91,
-65,
-76,
-58,
-47,
-45,
-21,
-11,
-112,
-80,
-93,
-98,
-41,
-54,
-105,
-36,
-102,
-75,
-102,
-67,
-100,
-41,
-56,
-19,
-90,
-5,
-66,
-41,
-3,
-32,
-95,
-65,
-44,
-1,
1,
-62,
-7,
-29,
-61,
-7,
1,
-63,
0,
-20,
-58,
-58,
-7,
-54,
-80,
-48,
-51,
-151,
-141,
-37,
-122,
-130,
-132,
-158,
-117,
-63,
-103,
-130,
-116,
-130,
-63,
-134,
-131,
-59,
-30,
-33,
-38,
-127,
-31,
-76,
-35,
-162,
-132,
-121,
-31,
-28,
-2,
-29,
-148,
-156,
-168,
2,
-33,
-85,
-25,
-18,
-167,
-152,
-22,
-38,
-136,
-83,
-46,
-73,
-139,
-15,
-185,
-197,
-125,
-159,
-80,
-161,
-158,
-82,
-36,
-52,
-210,
-200,
-90,
-199,
-70,
-135,
-195,
-54,
-156,
-46,
-74,
-73,
-221,
-96,
-37,
-189,
-27,
-209,
-30,
-50,
-4,
-74,
-15,
-184,
2,
-78,
-33,
-37,
-99,
-65,
-196,
-32,
-36,
-188,
-62,
-5,
-244,
-116,
-150,
-118,
-124,
-54,
-28,
-43,
-208,
-205,
-95,
-90,
-129,
-242,
-70,
-144,
-64,
-247,
-170,
-213,
-40,
-173,
-90,
-77,
-139,
-56,
-70,
-120,
-9,
-68,
-78,
-7,
-123,
-103,
-173,
-254,
-249,
-246,
-139,
-192,
-92,
-204,
-71,
-199,
-56,
-63,
-231,
-23,
-115,
-240,
-51,
-200,
-184,
-287,
-98,
-7,
-81,
-275,
-262,
-260,
-32,
-99,
-28,
-199,
-160,
-176,
-210,
-244,
-162,
-82,
-35,
-276,
-71,
-114,
-222,
-294,
-28,
-122,
-110,
-178,
-264,
-239,
-104,
-85,
-11,
-117,
-15,
-69,
-275,
-289,
-212,
1,
-296,
-285,
-9,
-95,
-149,
-197,
-152,
-141,
-148,
-138,
-173,
-224,
-297,
-299,
-53,
-335,
-36,
-17,
-291,
-25,
-211,
-175,
-104,
-328,
-58,
-15,
-198,
-102,
-122,
-211,
-74,
-117,
-205,
-143,
-353,
-187,
-323,
-172,
-133,
-170,
-41,
-92,
-84,
-72,
-352,
-278,
-164,
-124,
-175,
-113,
-175,
-152,
-160,
-33,
-126,
-226,
-237,
-135,
-156,
-190,
-378,
-168,
-271,
-240,
-111,
-398,
-91,
-243,
-336,
-311,
-368,
-396,
-202,
-262,
-18,
-303,
-363,
-67,
-36,
-284,
-404,
-120,
-97,
-387,
-26,
-135,
-112,
-325,
-82,
-53,
-307,
-410,
-276,
-384,
-64,
-60,
-412,
-335,
-356,
-82,
-134,
-251,
-408,
-342,
-9,
-73,
-27,
-388,
-434,
-80,
-231,
-114,
0,
-64,
-325,
-251,
-153,
-109,
1,
-92,
-167,
-89,
-454,
-154,
-13,
-283,
-231,
-357,
-244,
-324,
-134,
-41,
-380,
-169,
-247,
-301,
-297,
-388,
-304,
-135,
-403,
-168,
-314,
-117,
-281,
-76,
-473,
-281,
-322,
-79,
-39,
-129,
-432,
-452,
-183,
-164,
-76,
-382,
-306,
-58,
-126,
-141,
-4,
-3,
-201,
-480,
-443,
-313,
-361,
-279,
-250,
-38,
-1,
-340,
-138,
-69,
-462,
-32,
-68,
-19,
-31,
-271,
-86,
-141,
-331,
-412,
-29,
-369,
-518,
-103,
-502,
-24,
-67,
-130,
-247,
-331,
-535,
-77,
-305,
-153,
-44,
-382,
-309,
-162,
-430,
-480,
-25,
-431,
-78,
-442,
-549,
-184,
-523,
-94,
-380,
-227,
-526,
-209,
-508,
-129,
-36,
-510,
-310,
-133,
-145,
-146,
-244,
-245,
-541,
-362,
-7,
-103,
-565,
-209,
2,
-140,
-51,
-572,
-28,
-354,
-525,
-148,
-79,
-176,
-34,
-396,
-162,
-374,
-448,
-76,
-87,
-136,
-584,
-179,
-230,
-490,
-361,
-333,
-328,
-34,
-524,
-273,
-195,
-32,
-520,
-260,
-506,
-576,
-422,
-115,
-65,
-285,
-314,
-322,
-146,
-287,
-251,
-585,
-326,
-77,
-250,
-321,
-334,
-560,
-455,
-523,
-90,
-234,
-343,
-457,
-395,
-173,
-560,
-474,
-118,
-244,
-263,
-493,
-597,
-232,
-237,
-619,
-372,
-416,
-142,
-93,
-546,
-538,
-198,
-574,
-250,
-491,
-168,
-47,
-247,
-127,
-641,
-228,
-192,
-545,
-543,
-172,
-220,
-277,
-647,
-87,
-198,
-450,
-247,
-15,
-406,
-562,
-335,
-436,
-665,
-362,
-211,
-582,
-178,
-523,
-232,
-287,
-635,
-33,
-666,
-577,
-54,
-509,
-271,
-561,
-491,
-512,
-212,
-269,
-473,
-460,
-587,
-209,
-538,
-14,
-303,
-360,
-275,
-125,
-373,
-108,
-31,
-314,
-639,
-220,
-52,
-378,
-398,
-369,
-594,
-204,
-423,
-441,
-447,
-27,
-495,
-595,
-352,
-388,
-127,
-424,
-609,
-435,
-626,
-191,
-46,
-363,
-15,
-557,
-433,
-53,
-680,
-129,
-462,
-40,
-598,
-246,
-468,
-600,
-351,
-409,
-89,
-732,
-178,
-472,
-335,
-622,
-563,
-322,
-261,
-63,
-671,
-291,
-591,
-518,
-373,
-615,
-727,
-553,
-166,
-108,
-723,
-77,
-736,
-364,
-765,
-49,
-41,
-99,
-134,
-684,
-281,
-530,
-545,
-372,
-570,
-48,
-288,
-583,
-421,
-601,
-162,
-176,
-414,
-735,
-195,
-786,
-656,
-488,
-744,
-256,
-345,
-152,
-44,
-29,
1,
-582,
-30,
-351,
-379,
-23,
-48,
-737,
-293,
-525,
-73,
-79,
-531,
-775,
-706,
-59,
-74,
-805,
-311,
-544,
-33,
-603,
-454,
-700,
-506,
-489,
-617,
-485,
-267,
-794,
-13,
-707,
-557,
-368,
-730,
-696,
-728,
-167,
-413,
-639,
-705,
-391,
-11,
-195,
-416,
-788,
-295,
-768,
-192,
-2,
-771,
-675,
-687,
-198,
-568,
-663,
-302,
-732,
-265,
-796,
-370,
-18,
-579,
-771,
-349,
-365,
-214,
-598,
-314,
-752,
-315,
-815,
-487,
-511,
-126,
-6,
-146,
-353,
-787,
-204,
-330,
-517,
-456,
-805,
-4,
-500,
-150,
-242,
-833,
-804,
-663,
-554,
-41,
-607,
-121,
-762,
-892,
-249,
-405,
-403,
-255,
-457,
-613,
-91,
-157,
-890,
-631,
-908,
-544,
-487,
-813,
-541,
-108,
-147,
-702,
-301,
-430,
-66,
-492,
-902,
-284,
-464,
-784,
-312,
-762,
-588,
-17,
-809,
-436,
-483,
-16,
-410,
-180,
-568,
-37,
-687,
-444,
-619,
-211,
-386,
-673,
-600,
-155,
-558,
-849,
-37,
-717,
-867,
-236,
-98,
-165,
-579,
-677,
-691,
-602,
-878,
-555,
-893,
-773,
-395,
-942,
-661,
-850,
-881,
-485,
-312,
-689,
-258,
-899,
-120,
-227,
-349,
-467,
-404,
-45,
-919,
-329,
-365,
-22,
-462,
-632,
-498,
-873,
-288,
-901,
-655,
-321,
-922,
-882,
-416,
-946,
-320,
-5,
-57,
-352,
-711,
-197,
-705,
-737,
-439,
-39,
-252,
-1002,
-617,
-373,
-605,
-887,
-451,
-824,
-455,
-66,
-619,
-18,
-404,
-64,
-736,
-44,
-381,
-447,
-567,
-877,
-411,
-216,
-635,
-598,
-419,
-577,
-142,
-189,
-917,
-692,
-153,
-2,
-116,
-172,
-423,
-886,
-454,
-492,
-491,
-656,
-832,
-1036,
-468,
-23,
-709,
-292,
-668,
-454,
-478,
-302,
-182,
-677,
-904,
-648,
-513,
-901,
-331,
-750,
-445,
-758,
-842,
-372,
-471,
-109,
-239,
-704,
-817,
-340,
-591,
-40
];
let mut cpu = Cpu::new(&prod_data, true);
while ! cpu.is_oom() {
cpu.step();
}
println!("cycles: {}", cpu.cycles());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment