switched to workspaces.
This should let me make a cross-day library.
This commit is contained in:
parent
1469c3a32b
commit
242989bb95
57 changed files with 156 additions and 89 deletions
6
days/day01/Cargo.toml
Normal file
6
days/day01/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day01"
|
||||
authors.workspace = true
|
||||
description.workspace = true
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
2255
days/day01/src/input.txt
Normal file
2255
days/day01/src/input.txt
Normal file
File diff suppressed because it is too large
Load diff
14
days/day01/src/main.rs
Normal file
14
days/day01/src/main.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
mod parse;
|
||||
mod part1;
|
||||
mod part2;
|
||||
|
||||
fn main() {
|
||||
let _input = include_str!("./input.txt");
|
||||
let _structured_input = parse::parse(_input);
|
||||
|
||||
println!("Part One");
|
||||
println!("Result: {}", part1::part1(&_structured_input));
|
||||
|
||||
println!("Part Two");
|
||||
println!("Result: {}", part2::part2(&_structured_input));
|
||||
}
|
48
days/day01/src/parse.rs
Normal file
48
days/day01/src/parse.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Elf(pub Vec<usize>);
|
||||
|
||||
pub fn parse(input: &str) -> Vec<Elf> {
|
||||
input
|
||||
.trim()
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
Elf(group
|
||||
.split('\n')
|
||||
.map(|line| line.parse().unwrap())
|
||||
.collect())
|
||||
})
|
||||
.collect::<Vec<Elf>>()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let input = "1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000";
|
||||
assert_eq!(
|
||||
parse(input),
|
||||
vec![
|
||||
Elf(vec![1000, 2000, 3000]),
|
||||
Elf(vec![4000]),
|
||||
Elf(vec![5000, 6000]),
|
||||
Elf(vec![7000, 8000, 9000]),
|
||||
Elf(vec![10000])
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
27
days/day01/src/part1.rs
Normal file
27
days/day01/src/part1.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use crate::parse;
|
||||
|
||||
pub fn part1(input: &[parse::Elf]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.map(|elf| elf.0.iter().sum::<usize>())
|
||||
.max()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
let input = vec![
|
||||
parse::Elf(vec![1000, 2000, 3000]),
|
||||
parse::Elf(vec![4000]),
|
||||
parse::Elf(vec![5000, 6000]),
|
||||
parse::Elf(vec![7000, 8000, 9000]),
|
||||
parse::Elf(vec![10000]),
|
||||
];
|
||||
|
||||
assert_eq!(part1(&input), 24000);
|
||||
}
|
||||
}
|
27
days/day01/src/part2.rs
Normal file
27
days/day01/src/part2.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use crate::parse;
|
||||
|
||||
pub fn part2(input: &[parse::Elf]) -> usize {
|
||||
let mut input = input
|
||||
.iter()
|
||||
.map(|elf| elf.0.iter().sum::<usize>())
|
||||
.collect::<Vec<usize>>();
|
||||
input.sort_unstable();
|
||||
input[input.len() - 3..].iter().sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
let input = vec![
|
||||
parse::Elf(vec![1000, 2000, 3000]),
|
||||
parse::Elf(vec![4000]),
|
||||
parse::Elf(vec![5000, 6000]),
|
||||
parse::Elf(vec![7000, 8000, 9000]),
|
||||
parse::Elf(vec![10000]),
|
||||
];
|
||||
assert_eq!(part2(&input), 45000);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue