diff --git a/2023/src/day12.rs b/2023/src/day12.rs index 574e0d4..32e4cf0 100644 --- a/2023/src/day12.rs +++ b/2023/src/day12.rs @@ -33,7 +33,7 @@ fn number_ways(line: &[u8], groups: &[u8]) -> u64 { // Either defective or maybe defective if c != b'.' && cur_group < usize::from(group) { - next[group_pos * group_stride + cur_group as usize + 1] += ways; + next[group_pos * group_stride + cur_group + 1] += ways; } if c != b'#' { diff --git a/2023/src/day18.rs b/2023/src/day18.rs index 41f22fd..709a530 100644 --- a/2023/src/day18.rs +++ b/2023/src/day18.rs @@ -1,10 +1,8 @@ use nom::bytes::complete::tag; use nom::bytes::complete::take; -use nom::bytes::complete::take_while1; use nom::combinator::map; use nom::combinator::map_res; use nom::multi::many1; -use nom::sequence::delimited; use nom::sequence::pair; use nom::sequence::terminated; use nom::IResult; @@ -17,8 +15,8 @@ struct Dig { amount: u64, } -fn parse_instructions(i: &[u8]) -> IResult<&[u8], Vec<(Dig, u64)>> { - many1(pair( +fn parse_instructions(i: &[u8]) -> IResult<&[u8], Vec> { + many1(terminated( map( pair( terminated( @@ -37,24 +35,18 @@ fn parse_instructions(i: &[u8]) -> IResult<&[u8], Vec<(Dig, u64)>> { ), |(dir, amount)| Dig { dir, amount }, ), - delimited( - tag("(#"), - map_res(take_while1(nom::character::is_hex_digit), |s| { - u64::from_str_radix(std::str::from_utf8(s).expect("Checked hex digits"), 16) - }), - tag(")\n"), - ), + take(10usize), ))(i) } -fn compute_points(instructions: &[(Dig, u64)]) -> Vec<(i64, i64)> { +fn compute_points(instructions: &[Dig]) -> Vec<(i64, i64)> { let mut result = Vec::with_capacity(instructions.len() + 1); result.push((0, 0)); let mut x = 0; let mut y = 0; - for &(Dig { dir, amount }, _) in instructions { + for &Dig { dir, amount } in instructions { match dir { Direction::Up => y -= amount as i64, Direction::Left => x -= amount as i64, @@ -78,22 +70,24 @@ fn shoelace(points: &[(i64, i64)]) -> i64 { / 2 } -pub fn part1(input: &[u8]) -> anyhow::Result { - let instructions = parse_input(input, parse_instructions)?; - let points = compute_points(&instructions); +fn solve(digs: &[Dig]) -> anyhow::Result { + let points = compute_points(digs); let area = shoelace(&points); // Assumption: we don't cross over ourselves - let perimeter = instructions - .iter() - .map(|(dig, _)| dig.amount as i64) - .sum::(); + let perimeter = digs.iter().map(|dig| dig.amount as i64).sum::(); let total = area + perimeter / 2 + 1; Ok(total.to_string()) } +pub fn part1(input: &[u8]) -> anyhow::Result { + let digs = parse_input(input, parse_instructions)?; + + solve(&digs) +} + pub fn part2(_input: &[u8]) -> anyhow::Result { anyhow::bail!("Not implemented") }