This commit is contained in:
Gabe Venberg 2023-12-01 11:14:22 -06:00
parent b8f85706f1
commit 65179c9f23
10 changed files with 1164 additions and 126 deletions

11
days/day01/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "day01"
authors.workspace = true
description.workspace = true
version.workspace = true
edition.workspace = true
[dependencies]
aoc_libs.workspace = true
regex.workspace = true
once_cell.workspace = true

1000
days/day01/src/input.txt Normal file

File diff suppressed because it is too large Load diff

15
days/day01/src/main.rs Normal file
View file

@ -0,0 +1,15 @@
mod part1;
mod part2;
mod parse;
fn main() {
let input = include_str!("./input.txt");
let structured_input = parse::parse(input);
println!("Part One");
println!("Result: {}", part1::part1(&structured_input));
let structured_input = parse::parse_english(input);
println!("Part Two");
println!("Result: {}", part2::part2(&structured_input));
}

77
days/day01/src/parse.rs Normal file
View file

@ -0,0 +1,77 @@
static REPLACEMENT_STRINGS: [(&str, &str); 10] = [
("zero", "zero0zero"),
("one", "one1one"),
("two", "two2two"),
("three", "three3three"),
("four", "four4four"),
("five", "five5five"),
("six", "six6six"),
("seven", "seven7seven"),
("eight", "eight8eight"),
("nine", "nine9nine"),
];
pub fn parse_english(input: &str) -> Vec<Vec<char>> {
input
.to_string()
.lines()
.map(|l| {
let mut ret = l.to_string();
for (string, replacement) in REPLACEMENT_STRINGS.iter() {
ret = ret.replace(string, replacement)
}
ret
})
.map(|l| l.chars().filter(|c| c.is_ascii_digit()).collect())
.collect()
}
pub fn parse(input: &str) -> Vec<Vec<char>> {
input
.lines()
.map(|l| l.chars().filter(|c| c.is_ascii_digit()).collect())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let input = concat!("1abc2\n", "pqr3stu8vwx\n", "a1b2c3d4e5f\n", "treb7uchet\n",);
assert_eq!(
parse(input),
vec![
vec!['1', '2',],
vec!['3', '8',],
vec!['1', '2', '3', '4', '5'],
vec!['7']
]
);
}
#[test]
fn test_english() {
let input = concat!(
"two1nine\n",
"eightwothree\n",
"abcone2threexyz\n",
"xtwone3four\n",
"4nineeightseven2\n",
"zoneight234\n",
"7pqrstsixteen\n",
);
assert_eq!(
parse_english(input),
vec![
vec!['2', '1', '9',],
vec!['8', '2', '3',],
vec!['1', '2', '3',],
vec!['2', '1', '3', '4'],
vec!['4', '9', '8', '7', '2'],
vec!['1', '8', '2', '3', '4'],
vec!['7', '6'],
]
);
}
}

27
days/day01/src/part1.rs Normal file
View file

@ -0,0 +1,27 @@
pub fn part1(input: &[Vec<char>]) -> usize {
input
.iter()
.map(|s| {
let mut string = String::new();
string.push_str(&s.first().unwrap().to_string());
string.push_str(&s.last().unwrap().to_string());
string.parse::<usize>().unwrap()
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
let input = vec![
vec!['1', '2'],
vec!['3', '8'],
vec!['1', '2', '3', '4', '5'],
vec!['7'],
];
assert_eq!(part1(&input), 142);
}
}

30
days/day01/src/part2.rs Normal file
View file

@ -0,0 +1,30 @@
pub fn part2(input: &[Vec<char>]) -> usize {
input
.iter()
.map(|s| {
let mut string = String::new();
string.push_str(&s.first().unwrap().to_string());
string.push_str(&s.last().unwrap().to_string());
string.parse::<usize>().unwrap()
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part2() {
let input = vec![
vec!['2', '1', '9'],
vec!['8', '2', '3'],
vec!['1', '2', '3'],
vec!['2', '1', '3', '4'],
vec!['4', '9', '8', '7', '2'],
vec!['1', '8', '2', '3', '4'],
vec!['7', '6'],
];
assert_eq!(part2(&input), 281);
}
}