Format day 1 solution

This commit is contained in:
pjht 2023-12-02 09:36:36 -06:00
parent b9dfa6ab29
commit d1b425ba27
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -1,5 +1,11 @@
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)]
fn input_generator(input: &str) -> Vec<String> {
@ -8,29 +14,35 @@ fn input_generator(input: &str) -> Vec<String> {
#[aoc(day1, part1)]
fn part1(input: &[String]) -> u32 {
input.iter().map(|line| {
line.chars()
.filter(|c| c.is_ascii_digit())
.map(|c| (c as u32) - (b'0' as u32))
.collect::<Vec<_>>()
}).map(|line| (line[0] * 10) + (line.last().unwrap())).sum()
input
.iter()
.map(|line| {
line.chars()
.filter(|c| c.is_ascii_digit())
.map(|c| (c as u32) - (b'0' as u32))
.collect::<Vec<_>>()
})
.map(|line| (line[0] * 10) + (line.last().unwrap()))
.sum()
}
fn parse_text_digit(input: &str) -> IResult<&str, u32> {
let one = map(tag("one"), |_| 1);
let two = map(tag("two"), |_| 2);
let three = map(tag("three"), |_| 3);
let four = map(tag("four"), |_| 4);
let five = map(tag("five"), |_| 5);
let six = map(tag("six"), |_| 6);
let seven = map(tag("seven"), |_| 7);
let eight = map(tag("eight"), |_| 8);
let nine = map(tag("nine"), |_| 9);
alt((one, two, three, four, five, six, seven, eight, nine))(input)
let one = map(tag("one"), |_| 1);
let two = map(tag("two"), |_| 2);
let three = map(tag("three"), |_| 3);
let four = map(tag("four"), |_| 4);
let five = map(tag("five"), |_| 5);
let six = map(tag("six"), |_| 6);
let seven = map(tag("seven"), |_| 7);
let eight = map(tag("eight"), |_| 8);
let nine = map(tag("nine"), |_| 9);
alt((one, two, three, four, five, six, seven, eight, nine))(input)
}
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> {
@ -51,5 +63,9 @@ fn parse_line(line: &str) -> Vec<u32> {
#[aoc(day1, part2)]
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()
}