feat: solve day 2
This commit is contained in:
parent
b36e0b4b6e
commit
7f4517b9e6
4 changed files with 97 additions and 14 deletions
40
Cargo.lock
generated
40
Cargo.lock
generated
|
|
@ -23,9 +23,9 @@ version = "0.12.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"dhat",
|
"dhat",
|
||||||
|
"fancy-regex",
|
||||||
"itertools",
|
"itertools",
|
||||||
"pico-args",
|
"pico-args",
|
||||||
"regex",
|
|
||||||
"tinyjson",
|
"tinyjson",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -74,6 +74,21 @@ dependencies = [
|
||||||
"rustc-demangle",
|
"rustc-demangle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bit-set"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"
|
||||||
|
dependencies = [
|
||||||
|
"bit-vec",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bit-vec"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
|
|
@ -143,6 +158,17 @@ version = "1.15.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fancy-regex"
|
||||||
|
version = "0.16.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "998b056554fbe42e03ae0e152895cd1a7e1002aec800fdc6635d20270260c46f"
|
||||||
|
dependencies = [
|
||||||
|
"bit-set",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gimli"
|
name = "gimli"
|
||||||
version = "0.28.1"
|
version = "0.28.1"
|
||||||
|
|
@ -329,18 +355,6 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex"
|
|
||||||
version = "1.12.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
|
|
||||||
dependencies = [
|
|
||||||
"aho-corasick",
|
|
||||||
"memchr",
|
|
||||||
"regex-automata",
|
|
||||||
"regex-syntax",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-automata"
|
name = "regex-automata"
|
||||||
version = "0.4.13"
|
version = "0.4.13"
|
||||||
|
|
|
||||||
|
|
@ -29,5 +29,5 @@ tinyjson = "2.5.1"
|
||||||
|
|
||||||
# Solution dependencies
|
# Solution dependencies
|
||||||
itertools = "0.14.0"
|
itertools = "0.14.0"
|
||||||
regex = "1.12.2"
|
fancy-regex = "0.16.2"
|
||||||
|
|
||||||
|
|
|
||||||
1
data/examples/02.txt
Normal file
1
data/examples/02.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||||
68
src/bin/02.rs
Normal file
68
src/bin/02.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
use fancy_regex::Regex;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
advent_of_code::solution!(2);
|
||||||
|
|
||||||
|
pub fn part_one(input: &str) -> Option<u64> {
|
||||||
|
input
|
||||||
|
.split(",")
|
||||||
|
.map(|range| {
|
||||||
|
let [start, end] = range.split("-").collect::<Vec<&str>>().try_into().unwrap();
|
||||||
|
let start_num = start.parse().unwrap_or(0);
|
||||||
|
let end_num = end.parse().unwrap_or(0);
|
||||||
|
(start_num..(end_num + 1))
|
||||||
|
.map(|id| {
|
||||||
|
let mut chars = id.to_string().chars().collect::<Vec<char>>();
|
||||||
|
let second_chars = chars.split_off(chars.len() / 2);
|
||||||
|
if chars == second_chars { id } else { 0 }
|
||||||
|
})
|
||||||
|
.sum1()
|
||||||
|
.unwrap_or(0)
|
||||||
|
})
|
||||||
|
.sum1()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part_two(input: &str) -> Option<u128> {
|
||||||
|
input
|
||||||
|
.split(",")
|
||||||
|
.map(|range| {
|
||||||
|
let [start, end] = range
|
||||||
|
.split("-")
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.try_into()
|
||||||
|
.unwrap();
|
||||||
|
let start_num: u128 = u128::from_str(start).unwrap();
|
||||||
|
let end_num: u128 = u128::from_str(end).unwrap();
|
||||||
|
(start_num..(end_num + 1))
|
||||||
|
.map(|id| {
|
||||||
|
let re = Regex::new(r"^(\d+)\1+$").unwrap();
|
||||||
|
if re.is_match(&id.to_string()).unwrap_or(false) {
|
||||||
|
id
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.sum1()
|
||||||
|
.unwrap_or(0)
|
||||||
|
})
|
||||||
|
.sum1()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_one() {
|
||||||
|
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||||
|
assert_eq!(result, Some(1227775554));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_two() {
|
||||||
|
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
|
||||||
|
assert_eq!(result, Some(4174379265));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue