From ed184fc92c87a6d6de0e147128d9dac2e21ebc64 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Fri, 13 Dec 2024 17:42:35 -0800 Subject: [PATCH] day12/day13: codspeed compat (&str input) --- src/day12.rs | 36 ++++++++++++++++++++---------------- src/day13.rs | 34 ++++++++++++++-------------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/day12.rs b/src/day12.rs index ef15b47..3f9c98e 100644 --- a/src/day12.rs +++ b/src/day12.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use aoc_runner_derive::{aoc, aoc_generator}; use grid::{Coord2d, Grid}; @@ -5,11 +7,10 @@ pub struct Farm { map: Grid, } -impl From<&[u8]> for Farm { - fn from(input: &[u8]) -> Self { - Self { - map: Grid::from(input), - } +impl FromStr for Farm { + type Err = as FromStr>::Err; + fn from_str(input: &str) -> Result { + Ok(Self { map: input.parse()? }) } } @@ -100,10 +101,12 @@ impl Farm { if visited.get(adj) == Some(&false) { // add the perimeter of the growth from there if not visited yet let (n_area, n_corners) = self.region_corners(adj, visited); - (area+n_area, corners+n_corners) - } else { (area, corners) } + (area + n_area, corners + n_corners) + } else { + (area, corners) + } } - Some(_) | None => { (area, corners) } + Some(_) | None => (area, corners), } }) } @@ -131,25 +134,26 @@ impl Farm { } } -#[aoc_generator(day12)] -fn parse(input: &[u8]) -> Farm { - input.into() +fn parse(input: &str) -> Farm { + input.parse().unwrap() } #[aoc(day12, part1)] -pub fn part1(farm: &Farm) -> u64 { +pub fn part1(input: &str) -> u64 { + let farm = parse(input); farm.regions_cost() } #[aoc(day12, part2)] -pub fn part2(farm: &Farm) -> u64 { +pub fn part2(input: &str) -> u64 { + let farm = parse(input); farm.regions_discount_cost() } #[cfg(test)] mod tests { use super::*; - const EXAMPLE: &[u8] = b"RRRRIICCFF + const EXAMPLE: &str = "RRRRIICCFF RRRRIICCCF VVRRRCCFFF VVRCCCJFFF @@ -162,11 +166,11 @@ MMMISSJEEE"; #[test] fn part1_example() { - assert_eq!(part1(&parse(EXAMPLE)), 1930); + assert_eq!(part1(EXAMPLE), 1930); } #[test] fn part2_example() { - assert_eq!(part2(&parse(EXAMPLE)), 1206); + assert_eq!(part2(EXAMPLE), 1206); } } diff --git a/src/day13.rs b/src/day13.rs index e82985c..ff2a299 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -1,8 +1,7 @@ -use std::io::{BufRead, Lines}; - -use aoc_runner_derive::{aoc, aoc_generator}; +use aoc_runner_derive::aoc; use itertools::Itertools; use regex::Regex; +use std::str::Lines; #[derive(Debug, Clone)] pub struct MachineAction(i64, i64); @@ -15,17 +14,12 @@ struct ClawMachine { } impl ClawMachine { - fn consume_from_input(input: &mut Lines) -> Option { + fn consume_from_input(input: &mut Lines) -> Option { // consume any empty lines at the front let ofs_re = Regex::new(r"X([+-]\d+), Y([+-]\d+)").unwrap(); let prize_re = Regex::new(r"X=(\d+), Y=(\d+)").unwrap(); // consume 3 lines - a, b, prize - if let Some((a_line, b_line, prize_line)) = input - .filter(|l| l.as_ref().is_ok_and(|l| !l.is_empty())) - .take(3) - .map(|l| l.unwrap()) - .collect_tuple() - { + if let Some((a_line, b_line, prize_line)) = input.filter(|l| !l.is_empty()).take(3).collect_tuple() { let a_caps = ofs_re.captures(&a_line).unwrap(); let b_caps = ofs_re.captures(&b_line).unwrap(); let prize_caps = prize_re.captures(&prize_line).unwrap(); @@ -76,8 +70,8 @@ struct ClawMachines { machines: Vec, } -impl From<&[u8]> for ClawMachines { - fn from(input: &[u8]) -> Self { +impl From<&str> for ClawMachines { + fn from(input: &str) -> Self { let mut machines = Vec::new(); let mut lines = input.lines(); while let Some(machine) = ClawMachine::consume_from_input(&mut lines) { @@ -87,19 +81,19 @@ impl From<&[u8]> for ClawMachines { } } -#[aoc_generator(day13)] -fn parse(input: &[u8]) -> ClawMachines { +fn parse(input: &str) -> ClawMachines { ClawMachines::from(input) } #[aoc(day13, part1)] -fn part1(machines: &ClawMachines) -> i64 { +fn part1(input: &str) -> i64 { + let machines = parse(input); machines.machines.iter().filter_map(|m| m.cheapest_prize()).sum() } #[aoc(day13, part2)] -fn part2(machines: &ClawMachines) -> i64 { - let mut machines = machines.clone(); +fn part2(input: &str) -> i64 { + let mut machines = parse(input); machines .machines .iter_mut() @@ -113,7 +107,7 @@ fn part2(machines: &ClawMachines) -> i64 { #[cfg(test)] mod tests { use super::*; - const EXAMPLE: &[u8] = b"Button A: X+94, Y+34 + const EXAMPLE: &str = "Button A: X+94, Y+34 Button B: X+22, Y+67 Prize: X=8400, Y=5400 @@ -131,11 +125,11 @@ Prize: X=18641, Y=10279"; #[test] fn part1_example() { - assert_eq!(part1(&parse(EXAMPLE)), 480); + assert_eq!(part1(EXAMPLE), 480); } #[test] fn part2_example() { - assert_eq!(part2(&parse(EXAMPLE)), 875318608908); + assert_eq!(part2(EXAMPLE), 875318608908); } }