summaryrefslogtreecommitdiff
path: root/2018/src/day14.rs
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/day14.rs')
-rw-r--r--2018/src/day14.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/2018/src/day14.rs b/2018/src/day14.rs
new file mode 100644
index 0000000..ed9bb74
--- /dev/null
+++ b/2018/src/day14.rs
@@ -0,0 +1,46 @@
+use std::io;
+use std::io::BufRead;
+
+fn simulate(list: &mut Vec<u8>, ptr1: &mut usize, ptr2: &mut usize) {
+ let n1 = (list[*ptr1] + list[*ptr2]) / 10;
+ let n2 = (list[*ptr1] + list[*ptr2]) % 10;
+
+ if n1 != 0 { list.push(n1); }
+ list.push(n2);
+
+ *ptr1 = (*ptr1 + list[*ptr1] as usize + 1) % list.len();
+ *ptr2 = (*ptr2 + list[*ptr2] as usize + 1) % list.len();
+}
+
+pub fn main<T: BufRead>(reader: T) -> io::Result<(String, String)> {
+ let basestr = reader.lines().next().unwrap().unwrap();
+ let basenum = basestr.parse::<usize>().unwrap();
+
+ let mut list: Vec<u8> = Vec::with_capacity(basenum + 11);
+ list.push(3);
+ list.push(7);
+ let mut ptr1 = 0;
+ let mut ptr2 = 1;
+
+ while list.len() < basenum + 10 {
+ simulate(&mut list, &mut ptr1, &mut ptr2);
+ }
+
+ let part1 = String::from_utf8(list[basenum..basenum+10].iter().map(|n| n + 48).collect()).unwrap();
+
+ let mut part2 = 0;
+
+ let needle: Vec<u8> = basestr.as_bytes().iter().map(|n| n - 48).collect();
+ for i in 0.. {
+ if list.len() < i + needle.len() {
+ simulate(&mut list, &mut ptr1, &mut ptr2);
+ }
+
+ if list[i..i+needle.len()] == *needle {
+ part2 = i;
+ break;
+ }
+ }
+
+ Ok((part1, part2.to_string()))
+}