3/4 of the way through day 1.
This commit is contained in:
parent
ea7220bb00
commit
50fbfd5438
|
@ -10,5 +10,9 @@ edition = "2021"
|
|||
name="template"
|
||||
path="src/template/solve.rs"
|
||||
|
||||
[[bin]]
|
||||
name="day01"
|
||||
path="src/day01/solve.rs"
|
||||
|
||||
[dependencies]
|
||||
regex="1"
|
||||
|
|
2255
src/day01/input.txt
Normal file
2255
src/day01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
49
src/day01/parse.rs
Normal file
49
src/day01/parse.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
#[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
src/day01/part1.rs
Normal file
27
src/day01/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);
|
||||
}
|
||||
}
|
25
src/day01/part2.rs
Normal file
25
src/day01/part2.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use crate::parse;
|
||||
|
||||
pub fn part2(input: &[parse::Elf]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.map(|elf| elf.0.iter().sum::<usize>())
|
||||
// not sure what to do next here, how do I get the 3 max values from here?
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
14
src/day01/solve.rs
Normal file
14
src/day01/solve.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
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));
|
||||
|
||||
println!("Part Two");
|
||||
println!("Result: {}", part2::part2(&_structured_input));
|
||||
}
|
17
src/template/parse.rs
Normal file
17
src/template/parse.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
pub fn parse(input: &str) -> usize {
|
||||
println!("{}", input);
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let input =
|
||||
"test"
|
||||
;
|
||||
assert_eq!(parse(input), 0);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
pub fn part1() -> usize {
|
||||
return 0;
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod day01 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
pub fn part2() -> usize {
|
||||
return 0;
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod day01 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
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());
|
||||
|
|
Loading…
Reference in a new issue