day 01
This commit is contained in:
commit
2b512aaf72
|
@ -0,0 +1 @@
|
|||
/target
|
|
@ -0,0 +1,32 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
|
@ -0,0 +1,8 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"day-01",
|
||||
]
|
||||
|
||||
[workspace.dependencies]
|
||||
nom = "7.1.3"
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
nom = "7.1.3"
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,33 @@
|
|||
fn main() {
|
||||
let input = include_str!("./input1.txt");
|
||||
println!("{}", part1(input));
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> u32 {
|
||||
input.lines().map(line).sum()
|
||||
}
|
||||
|
||||
fn line(input: &str) -> u32 {
|
||||
let mut numbers = input.chars().filter_map(|c| c.to_digit(10)).peekable();
|
||||
let first = numbers.peek().unwrap().to_owned();
|
||||
let last = numbers.last().unwrap();
|
||||
(first * 10) + last
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
assert_eq!(part1("1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet"), 142);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line1() {
|
||||
assert_eq!(line("1abc2"), 12);
|
||||
assert_eq!(line("pqr3stu8vwx"), 38);
|
||||
assert_eq!(line("a1b2c3d4e5f"), 15);
|
||||
assert_eq!(line("treb7uchet"), 77);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
character::complete::anychar,
|
||||
combinator::{map, map_opt, value},
|
||||
multi::many1,
|
||||
IResult,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("./input2.txt");
|
||||
println!("{}", part2(input));
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> u32 {
|
||||
input.lines().map(line).sum()
|
||||
}
|
||||
|
||||
fn line(input: &str) -> u32 {
|
||||
let digits = many1(alt((map(digit, Some), value(None, anychar))))(input)
|
||||
.unwrap()
|
||||
.1;
|
||||
let mut digits = digits.iter().filter_map(|d| d.to_owned()).peekable();
|
||||
let first = digits.peek().unwrap().to_owned();
|
||||
let last = digits.last().unwrap();
|
||||
|
||||
(first * 10) + last
|
||||
}
|
||||
|
||||
fn digit(i: &str) -> IResult<&str, u32> {
|
||||
alt((
|
||||
map_opt(anychar, |c| c.to_digit(10)),
|
||||
value(1, tag("one")),
|
||||
value(2, tag("two")),
|
||||
value(3, tag("three")),
|
||||
value(4, tag("four")),
|
||||
value(5, tag("five")),
|
||||
value(6, tag("six")),
|
||||
value(7, tag("seven")),
|
||||
value(8, tag("eight")),
|
||||
value(9, tag("nine")),
|
||||
))(i)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
assert_eq!(part2("two1nine\neightwothree\nabcone2threexyz\nxtwone3four\n4nineeightseven2\nzoneight234\n7pqrstsixteen"), 281);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line2() {
|
||||
assert_eq!(line("two1nine"), 29);
|
||||
assert_eq!(line("eightwothree"), 83);
|
||||
assert_eq!(line("abcone2threexyz"), 13);
|
||||
assert_eq!(line("xtwone3four"), 24);
|
||||
assert_eq!(line("4nineeightseven2"), 42);
|
||||
assert_eq!(line("zoneight234"), 14);
|
||||
assert_eq!(line("7pqrstsixteen"), 76);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue