Format day 1 solution
This commit is contained in:
parent
b9dfa6ab29
commit
d1b425ba27
26
src/day01.rs
26
src/day01.rs
@ -1,5 +1,11 @@
|
|||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use nom::{IResult, bytes::complete::tag, combinator::{map, opt}, branch::alt, character::complete::satisfy, Finish};
|
use nom::{
|
||||||
|
branch::alt,
|
||||||
|
bytes::complete::tag,
|
||||||
|
character::complete::satisfy,
|
||||||
|
combinator::{map, opt},
|
||||||
|
Finish, IResult,
|
||||||
|
};
|
||||||
|
|
||||||
#[aoc_generator(day1)]
|
#[aoc_generator(day1)]
|
||||||
fn input_generator(input: &str) -> Vec<String> {
|
fn input_generator(input: &str) -> Vec<String> {
|
||||||
@ -8,12 +14,16 @@ fn input_generator(input: &str) -> Vec<String> {
|
|||||||
|
|
||||||
#[aoc(day1, part1)]
|
#[aoc(day1, part1)]
|
||||||
fn part1(input: &[String]) -> u32 {
|
fn part1(input: &[String]) -> u32 {
|
||||||
input.iter().map(|line| {
|
input
|
||||||
|
.iter()
|
||||||
|
.map(|line| {
|
||||||
line.chars()
|
line.chars()
|
||||||
.filter(|c| c.is_ascii_digit())
|
.filter(|c| c.is_ascii_digit())
|
||||||
.map(|c| (c as u32) - (b'0' as u32))
|
.map(|c| (c as u32) - (b'0' as u32))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}).map(|line| (line[0] * 10) + (line.last().unwrap())).sum()
|
})
|
||||||
|
.map(|line| (line[0] * 10) + (line.last().unwrap()))
|
||||||
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_text_digit(input: &str) -> IResult<&str, u32> {
|
fn parse_text_digit(input: &str) -> IResult<&str, u32> {
|
||||||
@ -30,7 +40,9 @@ fn parse_text_digit(input: &str) -> IResult<&str, u32> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_ascii_digit(input: &str) -> IResult<&str, u32> {
|
fn parse_ascii_digit(input: &str) -> IResult<&str, u32> {
|
||||||
map(satisfy(|c| c.is_ascii_digit()), |c| (c as u32) - (b'0' as u32))(input)
|
map(satisfy(|c| c.is_ascii_digit()), |c| {
|
||||||
|
(c as u32) - (b'0' as u32)
|
||||||
|
})(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_digit(input: &str) -> IResult<&str, u32> {
|
fn parse_digit(input: &str) -> IResult<&str, u32> {
|
||||||
@ -51,5 +63,9 @@ fn parse_line(line: &str) -> Vec<u32> {
|
|||||||
|
|
||||||
#[aoc(day1, part2)]
|
#[aoc(day1, part2)]
|
||||||
fn part2(input: &[String]) -> u32 {
|
fn part2(input: &[String]) -> u32 {
|
||||||
input.iter().map(|line| parse_line(line)).map(|line| (line[0] * 10) + (line.last().unwrap())).sum()
|
input
|
||||||
|
.iter()
|
||||||
|
.map(|line| parse_line(line))
|
||||||
|
.map(|line| (line[0] * 10) + (line.last().unwrap()))
|
||||||
|
.sum()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user