summaryrefslogtreecommitdiff
path: root/2018
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2018-12-14 10:32:15 +0100
committertomsmeding <tom.smeding@gmail.com>2018-12-14 10:32:15 +0100
commit8a2f05141568fd3a5b29bb6e0623f4d96fd4d43b (patch)
tree5a83e423df448a1fe67903d94646aa6f2f4d6c28 /2018
parent17c7296e7aa5672e74aa41f6eb41cc09977d91e6 (diff)
Day 14
Diffstat (limited to '2018')
-rw-r--r--2018/input/14.txt1
-rw-r--r--2018/src/day14.rs46
-rw-r--r--2018/src/main.rs4
3 files changed, 50 insertions, 1 deletions
diff --git a/2018/input/14.txt b/2018/input/14.txt
new file mode 100644
index 0000000..7157be5
--- /dev/null
+++ b/2018/input/14.txt
@@ -0,0 +1 @@
+409551
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()))
+}
diff --git a/2018/src/main.rs b/2018/src/main.rs
index 27125d3..447005e 100644
--- a/2018/src/main.rs
+++ b/2018/src/main.rs
@@ -18,8 +18,9 @@ mod day10;
mod day11;
mod day12;
mod day13;
+mod day14;
-static NUM_DAYS: i32 = 13;
+static NUM_DAYS: i32 = 14;
fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> {
match day {
@@ -36,6 +37,7 @@ fn day_switch<T: BufRead>(day: i32, reader: T) -> io::Result<(String, String)> {
11 => day11::main(reader),
12 => day12::main(reader),
13 => day13::main(reader),
+ 14 => day14::main(reader),
_ => Err(Error::new(ErrorKind::Other, "Invalid day"))
}
}