mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Add solution to day 9 part 1.
Part 2 is mean.
This commit is contained in:
7
2016/day-09/Cargo.toml
Normal file
7
2016/day-09/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "day-09"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Bert Peters <bert.ljpeters@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
regex = "^0.1"
|
||||||
1
2016/day-09/input.txt
Normal file
1
2016/day-09/input.txt
Normal file
File diff suppressed because one or more lines are too long
41
2016/day-09/src/main.rs
Normal file
41
2016/day-09/src/main.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
extern crate regex;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
use std::env;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
let mut f = File::open(&args[1]).expect("Could not open file");
|
||||||
|
|
||||||
|
let mut data = String::new();
|
||||||
|
f.read_to_string(&mut data).expect("How can I not read this?");
|
||||||
|
|
||||||
|
let content = data.trim();
|
||||||
|
|
||||||
|
let r = 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, _) = 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