day3: full solution
This commit is contained in:
parent
b24593a469
commit
7a7fb4d02a
54
3/Cargo.lock
generated
Normal file
54
3/Cargo.lock
generated
Normal file
@ -0,0 +1,54 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day3"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
7
3/Cargo.toml
Normal file
7
3/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day3"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.11.1"
|
88
3/src/main.rs
Normal file
88
3/src/main.rs
Normal file
@ -0,0 +1,88 @@
|
||||
use regex::Regex;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Lines};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
// BOILERPLATE
|
||||
type InputIter = Lines<BufReader<File>>;
|
||||
|
||||
pub fn get_input() -> InputIter {
|
||||
let f = File::open("input").unwrap();
|
||||
let br = BufReader::new(f);
|
||||
br.lines()
|
||||
}
|
||||
|
||||
fn duration_format(duration: Duration) -> String {
|
||||
match duration.as_secs_f64() {
|
||||
x if x > 1.0 => format!("{:.3}s", x),
|
||||
x if x > 0.010 => format!("{:.3}ms", x * 1e3),
|
||||
x => format!("{:.3}us", x * 1e6),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = get_input();
|
||||
let start = Instant::now();
|
||||
let ans1 = problem1(input);
|
||||
let duration1 = start.elapsed();
|
||||
println!("Problem 1 solution: {} [{}]", ans1, duration_format(duration1));
|
||||
|
||||
let input = get_input();
|
||||
let start = Instant::now();
|
||||
let ans2 = problem2(input);
|
||||
let duration2 = start.elapsed();
|
||||
println!("Problem 2 solution: {} [{}]", ans2, duration_format(duration2));
|
||||
println!("Total duration: {}", duration_format(duration1 + duration2));
|
||||
}
|
||||
|
||||
// PROBLEM 1 solution
|
||||
|
||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
let mut sum = 0u64;
|
||||
for line in input.map(|i| i.unwrap()) {
|
||||
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
|
||||
for m in re.captures_iter(&line) {
|
||||
sum += m[1].parse::<u64>().unwrap() * m[2].parse::<u64>().unwrap();
|
||||
}
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
// PROBLEM 2 solution
|
||||
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
let mut sum = 0u64;
|
||||
let mut do_mul = true;
|
||||
for line in input.map(|i| i.unwrap()) {
|
||||
let re = Regex::new(r"(do\(\)|don't\(\)|mul\((\d+),(\d+)\))").unwrap();
|
||||
for m in re.captures_iter(&line) {
|
||||
match &m[1] {
|
||||
"do()" => do_mul = true,
|
||||
"don't()" => do_mul = false,
|
||||
_ if do_mul => sum += m[2].parse::<u64>().unwrap() * m[3].parse::<u64>().unwrap(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
const EXAMPLE1: &str = &"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
|
||||
const EXAMPLE2: &str = &"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
|
||||
|
||||
#[test]
|
||||
fn problem1_example() {
|
||||
let c = Cursor::new(EXAMPLE1);
|
||||
assert_eq!(problem1(c.lines()), 161);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn problem2_example() {
|
||||
let c = Cursor::new(EXAMPLE2);
|
||||
assert_eq!(problem2(c.lines()), 48);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user