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