mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
1 Commits
2022/4-ran
...
2022/day-2
| Author | SHA1 | Date | |
|---|---|---|---|
| 561fd2f07c |
@@ -1,12 +1,15 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use nom::character::complete::newline;
|
use nom::character::complete::newline;
|
||||||
|
use nom::combinator::map;
|
||||||
use nom::combinator::map_res;
|
use nom::combinator::map_res;
|
||||||
use nom::multi::many0;
|
|
||||||
use nom::sequence::separated_pair;
|
use nom::sequence::separated_pair;
|
||||||
use nom::sequence::terminated;
|
use nom::sequence::terminated;
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
|
||||||
use crate::common::parse_input;
|
use crate::common::parse_input;
|
||||||
|
use crate::common::reduce_many1;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||||
enum Rps {
|
enum Rps {
|
||||||
@@ -70,7 +73,7 @@ impl TryFrom<u8> for Rps {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_plan(input: &[u8]) -> IResult<&[u8], Vec<(Rps, Rps)>> {
|
fn parse_line(input: &[u8]) -> IResult<&[u8], (Rps, Rps)> {
|
||||||
fn parse_rps(input: &[u8]) -> IResult<&[u8], Rps> {
|
fn parse_rps(input: &[u8]) -> IResult<&[u8], Rps> {
|
||||||
// Note: alpha1 also sort of works but is significantly slower
|
// Note: alpha1 also sort of works but is significantly slower
|
||||||
map_res(nom::bytes::complete::take(1usize), |v: &[u8]| {
|
map_res(nom::bytes::complete::take(1usize), |v: &[u8]| {
|
||||||
@@ -78,33 +81,34 @@ fn parse_plan(input: &[u8]) -> IResult<&[u8], Vec<(Rps, Rps)>> {
|
|||||||
})(input)
|
})(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_line(input: &[u8]) -> IResult<&[u8], (Rps, Rps)> {
|
terminated(
|
||||||
separated_pair(parse_rps, nom::character::complete::char(' '), parse_rps)(input)
|
separated_pair(parse_rps, nom::character::complete::char(' '), parse_rps),
|
||||||
}
|
newline,
|
||||||
|
)(input)
|
||||||
many0(terminated(parse_line, newline))(input)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &[u8]) -> Result<String> {
|
pub fn part1(input: &[u8]) -> Result<String> {
|
||||||
let plan = parse_input(input, parse_plan)?;
|
parse_input(
|
||||||
|
input,
|
||||||
let result: u32 = plan
|
reduce_many1(
|
||||||
.into_iter()
|
map(parse_line, |(them, us)| us.score() + us.score_against(them)),
|
||||||
.map(|(them, us)| us.score() + us.score_against(them))
|
Add::add,
|
||||||
.sum();
|
),
|
||||||
|
)
|
||||||
Ok(result.to_string())
|
.map(|sum| sum.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &[u8]) -> Result<String> {
|
pub fn part2(input: &[u8]) -> Result<String> {
|
||||||
let plan = parse_input(input, parse_plan)?;
|
parse_input(
|
||||||
|
input,
|
||||||
let result: u32 = plan
|
reduce_many1(
|
||||||
.into_iter()
|
map(parse_line, |(them, us)| {
|
||||||
.map(|(them, us)| us.score_result() + us.needed(them).score())
|
us.score_result() + us.needed(them).score()
|
||||||
.sum();
|
}),
|
||||||
|
Add::add,
|
||||||
Ok(result.to_string())
|
),
|
||||||
|
)
|
||||||
|
.map(|sum| sum.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user