1
0
Fork 0

day 1 solution

main
Andrew Coleman 2023-12-04 11:43:33 -05:00
parent b531b180e8
commit b3ffab172d
6 changed files with 1300 additions and 0 deletions

136
2023/Cargo.lock generated Normal file
View File

@ -0,0 +1,136 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "aoc-runner"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d21ef9204ad206a5a3e918e9920da04e1118ad91ce4f23570be964b9d6b9dfcb"
[[package]]
name = "aoc-runner-derive"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba8b944269d3fee645d281b1335e1797044db497bb02d0098cc3fdb8900069cc"
dependencies = [
"aoc-runner-internal",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "aoc-runner-internal"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "274b0ba7f3669a45ec0aaacf94eb032a749de880ab776091576cca94037c9982"
dependencies = [
"serde",
"serde_derive",
"serde_json",
]
[[package]]
name = "aoc2023"
version = "0.1.0"
dependencies = [
"anyhow",
"aoc-runner",
"aoc-runner-derive",
]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]]
name = "serde_json"
version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

13
2023/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "aoc2023"
version = "0.1.0"
edition = "2021"
[lib]
name = "aoc2023"
path = "src/lib.rs"
[dependencies]
anyhow = "1.0.75"
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"

1000
2023/input/2023/day1.txt Normal file

File diff suppressed because it is too large Load Diff

139
2023/src/day01.rs Normal file
View File

@ -0,0 +1,139 @@
use aoc_runner_derive::aoc;
fn parse(input: &str) -> Vec<u32> {
input
.lines()
.filter_map(|l| {
let v: Vec<&str> = l.matches(char::is_numeric).collect();
if !v.is_empty() {
let tens = v[0].parse::<u32>().unwrap() * 10;
let ones = v[v.len() - 1].parse::<u32>().unwrap();
Some(tens + ones)
} else {
None
}
})
.collect()
}
#[aoc(day1, part1)]
fn part1_sum(input: &str) -> u32 {
let lines = parse(input);
lines.iter().sum()
}
#[aoc(day1, part2)]
fn part2_sum(input: &str) -> u32 {
sub_letters_for_digits(input).iter().sum()
}
fn char_to_num(c: char) -> Option<u32> {
match c {
'0' => Some(0),
'1' => Some(1),
'2' => Some(2),
'3' => Some(3),
'4' => Some(4),
'5' => Some(5),
'6' => Some(6),
'7' => Some(7),
'8' => Some(8),
'9' => Some(9),
_ => None,
}
}
const DIGITS: [&'static str; 10] = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];
fn letter_digit_at(line: &str, index: usize) -> Option<u32> {
for (dindex, d) in DIGITS.iter().enumerate() {
if index + d.len() > line.len() {
continue;
}
let index2 = index + d.len();
let substring: &str = line[index..index2].as_ref();
if *d == substring {
// println!("compare {} dindex {} d {}", index, dindex, d);
return Some(dindex as u32);
}
}
None
}
fn digit_for_char(line: &str, index: usize, c: char) -> Option<u32> {
if c.is_ascii_digit() {
char_to_num(c)
} else {
letter_digit_at(line, index)
}
}
fn sub_letters_for_digits(input: &str) -> Vec<u32> {
input
.lines()
.filter_map(|line| {
if !line.is_empty() {
let mut first_digit = 0;
let mut second_digit = 0;
for (index, c) in line.chars().enumerate() {
if let Some(digit) = digit_for_char(line, index, c) {
first_digit = digit;
break;
}
}
for (index, c) in line.chars().rev().enumerate() {
if let Some(digit) = digit_for_char(line, line.len() - index - 1, c) {
second_digit = digit;
break;
}
}
let num = first_digit * 10 + second_digit;
println!("line {} num {}", line, num);
Some(num)
} else {
None
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_DATA: &'static str = r#"
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
"#;
#[test]
fn sample_data() {
let expected = [12, 38, 15, 77];
let data = parse(SAMPLE_DATA);
assert_eq!(data, expected);
let part1 = part1_sum(SAMPLE_DATA);
assert_eq!(part1, 142);
}
const SAMPLE_DATA2: &'static str = r#"
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
"#;
#[test]
fn sample_data_part2() {
let data = sub_letters_for_digits(SAMPLE_DATA2);
assert_eq!(data, [29, 83, 13, 24, 42, 14, 76]);
let part2 = part2_sum(SAMPLE_DATA2);
assert_eq!(part2, 281);
}
}

5
2023/src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
use aoc_runner_derive::aoc_lib;
mod day01;
aoc_lib! { year = 2023 }

7
2023/src/main.rs Normal file
View File

@ -0,0 +1,7 @@
extern crate aoc2023;
extern crate aoc_runner;
extern crate aoc_runner_derive;
use aoc_runner_derive::aoc_main;
aoc_main! { lib = aoc2023; }