diff --git a/Cargo.lock b/Cargo.lock index 5ad0563..6fcfa27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,6 +12,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" name = "aoc2023" version = "0.1.0" dependencies = [ + "tap", "ureq", ] @@ -170,6 +171,12 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tinyvec" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 2465393..2651608 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ codegen-units = 16 rpath = false [dependencies] +tap = "1.0.1" ureq = { version = "2.9.1" } diff --git a/src/day1/2.rs b/src/day1/2.rs index b51073e..0d14c9e 100644 --- a/src/day1/2.rs +++ b/src/day1/2.rs @@ -1 +1,134 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +use std::collections::VecDeque; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn main() { + for input in INPUTS.iter() { + let mut total: u64 = 0; + + for line in input.split('\n') { + let line: Vec = line.chars().collect(); + + let mut first = None; + let mut last = 0; + + let mut tmp: VecDeque = VecDeque::new(); + + for c in line.iter() { + if tmp.len() >= 5 { + tmp.pop_front(); + } + + match c { + '1' => { + if first.is_none() { + first = Some(10 * 1); + } + last = 1; + + tmp.clear(); + } + '2' => { + if first.is_none() { + first = Some(10 * 2); + } + last = 2; + tmp.clear(); + } + '3' => { + if first.is_none() { + first = Some(10 * 3); + } + last = 3; + tmp.clear(); + } + '4' => { + if first.is_none() { + first = Some(10 * 4); + } + last = 4; + tmp.clear(); + } + '5' => { + if first.is_none() { + first = Some(10 * 5); + } + last = 5; + tmp.clear(); + } + '6' => { + if first.is_none() { + first = Some(10 * 6); + } + last = 6; + tmp.clear(); + } + '7' => { + if first.is_none() { + first = Some(10 * 7); + } + last = 7; + tmp.clear(); + } + '8' => { + if first.is_none() { + first = Some(10 * 8); + } + last = 8; + tmp.clear(); + } + '9' => { + if first.is_none() { + first = Some(10 * 9); + } + last = 9; + tmp.clear(); + } + + c => { + tmp.push_back(*c); + } + } + + if let Some(digit) = get_digit(tmp.make_contiguous()) { + if first.is_none() { + first = Some(10 * digit); + } + last = digit; + } + } + + if let Some(digit) = get_digit(tmp.make_contiguous()) { + if first.is_none() { + first = Some(10 * digit); + } + last = digit; + } + + total += first.unwrap_or(0) as u64 + last as u64; + } + + println!("total = {}", total); + } +} + +fn get_digit(set: &[char]) -> Option { + match set { + ['t', 'h', 'r', 'e', 'e'] => Some(3), + ['s', 'e', 'v', 'e', 'n'] => Some(7), + ['e', 'i', 'g', 'h', 't'] => Some(8), + ['o', 'n', 'e', _, _] | ['o', 'n', 'e'] | [_, 'o', 'n', 'e'] | [_, _, 'o', 'n', 'e'] => { + Some(1) + } + ['t', 'w', 'o', _, _] | ['t', 'w', 'o'] | [_, 't', 'w', 'o'] | [_, _, 't', 'w', 'o'] => { + Some(2) + } + ['s', 'i', 'x', _, _] | ['s', 'i', 'x'] | [_, 's', 'i', 'x'] | [_, _, 's', 'i', 'x'] => { + Some(6) + } + ['n', 'i', 'n', 'e', _] | ['n', 'i', 'n', 'e'] | [_, 'n', 'i', 'n', 'e'] => Some(9), + ['f', 'o', 'u', 'r', _] | ['f', 'o', 'u', 'r'] | [_, 'f', 'o', 'u', 'r'] => Some(4), + ['f', 'i', 'v', 'e', _] | ['f', 'i', 'v', 'e'] | [_, 'f', 'i', 'v', 'e'] => Some(5), + _ => None, + } +} diff --git a/src/day1/sample.txt b/src/day1/sample.txt index 7bbc69a..a18eda9 100644 --- a/src/day1/sample.txt +++ b/src/day1/sample.txt @@ -1,4 +1,9 @@ -1abc2 -pqr3stu8vwx -a1b2c3d4e5f -treb7uchet +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen +2tqbxgrrpmxqfglsqjkqthree6nhjvbxpflhr1eightwohr +