day01
This commit is contained in:
parent
b8f85706f1
commit
65179c9f23
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
124
Cargo.lock
generated
124
Cargo.lock
generated
|
@ -18,111 +18,24 @@ version = "0.1.0"
|
|||
[[package]]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day03"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day04"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day05"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day06"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day07"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"regex",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day08"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc_libs",
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc_libs",
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
|
||||
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 = "regex"
|
||||
version = "1.10.2"
|
||||
|
@ -152,46 +65,9 @@ version = "0.8.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[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 = "template"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc_libs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
|
11
days/day01/Cargo.toml
Normal file
11
days/day01/Cargo.toml
Normal 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
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
15
days/day01/src/main.rs
Normal 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
77
days/day01/src/parse.rs
Normal 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
27
days/day01/src/part1.rs
Normal 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
30
days/day01/src/part2.rs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
use crate::parse::*
|
||||
use crate::parse::*;
|
||||
|
||||
pub fn part1() -> usize {
|
||||
unimplemented!()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::parse::*
|
||||
use crate::parse::*;
|
||||
|
||||
pub fn part2() -> usize {
|
||||
unimplemented!()
|
||||
|
|
Loading…
Reference in a new issue