Day 1 done
This commit is contained in:
@@ -1,3 +1,2 @@
|
|||||||
|
|
||||||
pub mod part1;
|
pub mod part1;
|
||||||
pub mod part2;
|
pub mod part2;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use day_1::part2::part2;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = include_str!("./input.txt");
|
let input = include_str!("./input.txt");
|
||||||
let (_,part1_result) = part1(input).unwrap();
|
let (_, part1_result) = part1(input).unwrap();
|
||||||
println!("part 1: {part1_result}");
|
println!("part 1: {part1_result}");
|
||||||
let part2_result = part2(input);
|
let part2_result = part2(input);
|
||||||
println!("part 2: {part2_result}");
|
println!("part 2: {part2_result}");
|
||||||
|
|||||||
@@ -1,14 +1,32 @@
|
|||||||
use nom::{multi::{separated_list1}, self, character::complete::{alphanumeric1, newline}};
|
use nom::{
|
||||||
|
self,
|
||||||
|
character::complete::{alphanumeric1, newline},
|
||||||
|
multi::separated_list1,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn part1(input: &str) -> nom::IResult<&str,String> {
|
pub fn part1(input: &str) -> nom::IResult<&str, String> {
|
||||||
let (_, values) = parse_input(input)?;
|
let (_, values) = parse_input(input)?;
|
||||||
println!("{values:?}");
|
println!("{values:?}");
|
||||||
Ok(("", values.iter().map(|v| v.first().unwrap() * 10 + v.last().unwrap() ).sum::<u32>().to_string()))
|
Ok((
|
||||||
|
"",
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.map(|v| v.first().unwrap() * 10 + v.last().unwrap())
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(input: &str) -> nom::IResult<&str, Vec<Vec<u32>>> {
|
fn parse_input(input: &str) -> nom::IResult<&str, Vec<Vec<u32>>> {
|
||||||
let (i, j) = separated_list1(newline, alphanumeric1)(input)?;
|
let (i, j) = separated_list1(newline, alphanumeric1)(input)?;
|
||||||
let res = j.iter().map(|v| v.chars().filter_map(|x| x.to_digit(10)).collect::<Vec<u32>>()).collect::<Vec<Vec<u32>>>();
|
let res = j
|
||||||
|
.iter()
|
||||||
|
.map(|v| {
|
||||||
|
v.chars()
|
||||||
|
.filter_map(|x| x.to_digit(10))
|
||||||
|
.collect::<Vec<u32>>()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Vec<u32>>>();
|
||||||
Ok((i, res))
|
Ok((i, res))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,4 +43,5 @@ treb7uchet";
|
|||||||
fn part1_works() {
|
fn part1_works() {
|
||||||
let (_, result) = part1(INPUT).unwrap();
|
let (_, result) = part1(INPUT).unwrap();
|
||||||
assert_eq!(result, "142".to_string());
|
assert_eq!(result, "142".to_string());
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,43 +1,46 @@
|
|||||||
|
|
||||||
pub fn part2(input: &str) -> String {
|
pub fn part2(input: &str) -> String {
|
||||||
let values = input.lines().map(parse_line).collect::<Vec<Vec<u32>>>();
|
let values = input.lines().map(parse_line).collect::<Vec<Vec<u32>>>();
|
||||||
println!("{values:?}");
|
println!("{values:?}");
|
||||||
values.iter().map(|v| v.first().unwrap() * 10 + v.last().unwrap() ).sum::<u32>().to_string()
|
values
|
||||||
|
.iter()
|
||||||
|
.map(|v| v.first().unwrap() * 10 + v.last().unwrap())
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_line(line: &str) -> Vec<u32> {
|
fn parse_line(line: &str) -> Vec<u32> {
|
||||||
(0..line.len()).filter_map(|index| {
|
(0..line.len())
|
||||||
|
.filter_map(|index| {
|
||||||
let reduced_line = &line[index..];
|
let reduced_line = &line[index..];
|
||||||
let result = if reduced_line.starts_with("one") {
|
let result = if reduced_line.starts_with("one") {
|
||||||
Some(1)
|
Some(1)
|
||||||
} else if reduced_line.starts_with("two"){
|
} else if reduced_line.starts_with("two") {
|
||||||
Some(2)
|
Some(2)
|
||||||
} else if reduced_line.starts_with("three"){
|
} else if reduced_line.starts_with("three") {
|
||||||
Some(3)
|
Some(3)
|
||||||
} else if reduced_line.starts_with("four"){
|
} else if reduced_line.starts_with("four") {
|
||||||
Some(4)
|
Some(4)
|
||||||
} else if reduced_line.starts_with("five"){
|
} else if reduced_line.starts_with("five") {
|
||||||
Some(5)
|
Some(5)
|
||||||
} else if reduced_line.starts_with("six"){
|
} else if reduced_line.starts_with("six") {
|
||||||
Some(6)
|
Some(6)
|
||||||
} else if reduced_line.starts_with("seven"){
|
} else if reduced_line.starts_with("seven") {
|
||||||
Some(7)
|
Some(7)
|
||||||
} else if reduced_line.starts_with("eight"){
|
} else if reduced_line.starts_with("eight") {
|
||||||
Some(8)
|
Some(8)
|
||||||
} else if reduced_line.starts_with("nine"){
|
} else if reduced_line.starts_with("nine") {
|
||||||
Some(9)
|
Some(9)
|
||||||
} else if reduced_line.starts_with("zero"){
|
} else if reduced_line.starts_with("zero") {
|
||||||
Some(0)
|
Some(0)
|
||||||
} else {
|
} else {
|
||||||
reduced_line.chars().next().unwrap().to_digit(10)
|
reduced_line.chars().next().unwrap().to_digit(10)
|
||||||
};
|
};
|
||||||
|
|
||||||
result
|
result
|
||||||
|
})
|
||||||
}).collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -54,4 +57,5 @@ zoneight234
|
|||||||
fn part2_works() {
|
fn part2_works() {
|
||||||
let result = part2(INPUT);
|
let result = part2(INPUT);
|
||||||
assert_eq!(result, "281".to_string());
|
assert_eq!(result, "281".to_string());
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user