mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Solution for day 9 part 2.
This commit is contained in:
@@ -5,3 +5,4 @@ authors = ["Bert Peters <bert.ljpeters@gmail.com>"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
regex = "^0.1"
|
regex = "^0.1"
|
||||||
|
lazy_static = "^0.2"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#[macro_use] extern crate lazy_static;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@@ -5,6 +6,43 @@ use std::env;
|
|||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
|
fn decoded_len(content: &str, recurse: bool) -> i64
|
||||||
|
{
|
||||||
|
lazy_static!{
|
||||||
|
static ref R: Regex = Regex::new(r"\((\d+)x(\d+)\)").expect("Failed to compile regex");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut len = 0;
|
||||||
|
let mut pos = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let todo = &content[pos..];
|
||||||
|
match R.captures(todo) {
|
||||||
|
Some(captures) => {
|
||||||
|
let start = todo.find(&captures[0]).unwrap();
|
||||||
|
pos += start;
|
||||||
|
len += start as i64;
|
||||||
|
|
||||||
|
let (length, amount) = (captures[1].parse::<i64>().unwrap(), captures[2].parse::<i64>().unwrap());
|
||||||
|
|
||||||
|
let part = &todo[start + captures[0].len()..start + captures[0].len() + length as usize];
|
||||||
|
|
||||||
|
let part_length = if recurse { decoded_len(part, true) } else { length };
|
||||||
|
|
||||||
|
pos += captures[0].len() as usize + length as usize;
|
||||||
|
len += part_length * amount;
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
// No more markers remaining, just append the rest
|
||||||
|
len += content.len() as i64 - pos as i64;
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
let mut f = File::open(&args[1]).expect("Could not open file");
|
let mut f = File::open(&args[1]).expect("Could not open file");
|
||||||
@@ -14,28 +52,7 @@ fn main() {
|
|||||||
|
|
||||||
let content = data.trim();
|
let content = data.trim();
|
||||||
|
|
||||||
let r = Regex::new(r"\((\d+)x(\d+)\)").expect("Failed to compile regex");
|
println!("Decoded v1 is {} long", decoded_len(content, false));
|
||||||
|
println!("Decoded v2 is {} long", decoded_len(content, true));
|
||||||
let mut len = 0;
|
|
||||||
let mut pos = 0;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let todo = &content[pos..];
|
|
||||||
match r.captures(todo) {
|
|
||||||
Some(captures) => {
|
|
||||||
let (start, _) = r.find(todo).unwrap();
|
|
||||||
let (length, amount) = (captures[1].parse::<i64>().unwrap(), captures[2].parse::<i64>().unwrap());
|
|
||||||
pos += start + captures[0].len() as usize + length as usize;
|
|
||||||
len += start as i64 + length as i64 * amount;
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
// No more markers remaining, just append the rest
|
|
||||||
len += content.len() as i64 - pos as i64;
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Decoded text is {} long", len);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user