From d1b425ba278c5b3ca9a4e90a8f12b66402089f1c Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 2 Dec 2023 09:36:36 -0600 Subject: [PATCH] Format day 1 solution --- src/day01.rs | 54 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/day01.rs b/src/day01.rs index 22cd6e2..cd1bf91 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -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 { @@ -8,29 +14,35 @@ fn input_generator(input: &str) -> Vec { #[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::>() - }).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::>() + }) + .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 { #[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() }