Skip to content

Commit

Permalink
Day 01b
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 1, 2023
1 parent 243aa00 commit 2e7d04a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions day01/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
aoc = { path = "../aoc" }
color-eyre = "0.6.2"
nom = "7.1.3"
regex = "1.10.2"
2 changes: 1 addition & 1 deletion day01/src/bin/day01a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn process(bufin: impl BufRead) -> Result<u32> {

#[test]
fn test() -> Result<()> {
assert_eq!(process(EXAMPLE.as_bytes())?, 142);
assert_eq!(process(EXAMPLE1.as_bytes())?, 142);
Ok(())
}

Expand Down
63 changes: 63 additions & 0 deletions day01/src/bin/day01b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use color_eyre::{eyre, Result};
use std::io::{stdin, BufRead};

use regex::Regex;

use day01::*;

fn readnum(s: &str) -> Result<u32> {
s.parse::<u32>().or(match s {
"zero" => Ok(0),
"one" => Ok(1),
"two" => Ok(2),
"three" => Ok(3),
"four" => Ok(4),
"five" => Ok(5),
"six" => Ok(6),
"seven" => Ok(7),
"eight" => Ok(8),
"nine" => Ok(9),
_ => Err(eyre!("invalid number")),
})
}

fn process(bufin: impl BufRead) -> Result<u32> {
let re1 = Regex::new(r"([0-9]|zero|one|two|three|four|five|six|seven|eight|nine)")?;
let re2 = Regex::new(r"([0-9]|enin|thgie|neves|xis|evif|ruof|eerht|owt|eno|orez)")?;
let lines = parser::parse(bufin)?;
Ok(lines
.into_iter()
.map(|line| {
let first = re1
.find(&line)
.ok_or_else(|| eyre!("could not match re1"))?;
let linerev = line.chars().rev().collect::<String>();
let lastrev = re2
.find(&linerev)
.ok_or_else(|| eyre!("could not match re2"))?;
let last = lastrev.as_str().chars().rev().collect::<String>();
let first = readnum(&first.as_str())?;
let last = readnum(&last)?;
let value = first * 10 + last;
Ok(value)
})
.collect::<eyre::Result<Vec<_>>>()?
.into_iter()
.sum())
}

#[test]
fn test() -> Result<()> {
assert_eq!(process(EXAMPLE2.as_bytes())?, 281);
Ok(())
}

fn main() -> Result<()> {
color_eyre::install()?;
println!("{}", process(stdin().lock())?);
Ok(())
}
14 changes: 12 additions & 2 deletions day01/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

pub use color_eyre::{eyre::eyre, Result};

pub const EXAMPLE: &str = "1abc2
pub const EXAMPLE1: &str = "1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
";

pub const EXAMPLE2: &str = "two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
";

pub mod parser {
use aoc::parser::*;

Expand All @@ -28,6 +37,7 @@ pub mod parser {

#[test]
fn test() -> Result<()> {
assert_eq!(parser::parse(EXAMPLE.as_bytes())?.len(), 4);
assert_eq!(parser::parse(EXAMPLE1.as_bytes())?.len(), 4);
assert_eq!(parser::parse(EXAMPLE2.as_bytes())?.len(), 7);
Ok(())
}

0 comments on commit 2e7d04a

Please sign in to comment.