Add solution for 2022 day 01

This commit is contained in:
Patrick Auernig 2022-12-01 16:12:11 +01:00
parent 8612463f8f
commit bede4baead
13 changed files with 2395 additions and 0 deletions

View File

@ -16,3 +16,6 @@ indent_size = 2
[*.ml] [*.ml]
indent_size = 2 indent_size = 2
[*.txt]
insert_final_newline = false

1
2022/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

19
2022/Justfile Normal file
View File

@ -0,0 +1,19 @@
@_run_day DAY PART INPUT:
just \
--working-directory "./day-{{DAY}}" \
--justfile "./day-{{DAY}}/Justfile" \
part {{PART}} {{INPUT}}
day DAYS="all" PARTS="one,two" INPUT_FILE="":
#!/usr/bin/env ruby
days = "{{DAYS}}" == "all" ? Dir["day-*"] : "{{DAYS}}".split(",")
input_file = "{{INPUT_FILE}}".then do |f|
(f.include?("/") || f.empty?) ? f : "inputs/#{f}.txt"
end
days.each do |day|
day = day.delete_prefix("day-")
"{{PARTS}}".split(",").each do |part|
part_num = part == "one" ? 1 : 2
puts "Day #{day}.#{part_num}: " + `just _run_day #{day} #{part} '#{input_file}'`
end
end

35
2022/README.md Normal file
View File

@ -0,0 +1,35 @@
# [Advent of Code 2022](https://adventofcode.com/2022)
## Progress
| Day | Part 1 | Part 2 | Language |
| :-: | :----: | :----: | :------- |
| 01 | ✓ | ✓ | [Rust] |
| 02 | | | |
| 03 | | | |
| 04 | | | |
| 05 | | | |
| 06 | | | |
| 07 | | | |
| 08 | | | |
| 09 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
<!-- links -->
[rust]: https://www.rust-lang.org

7
2022/day-01/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc-2022-01"
version = "0.1.0"

6
2022/day-01/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "aoc-2022-01"
version = "0.1.0"
edition = "2021"
[dependencies]

5
2022/day-01/Justfile Normal file
View File

@ -0,0 +1,5 @@
@part PART INPUT_FILE="inputs/puzzle.txt":
cargo run --bin part{{PART}} -- {{INPUT_FILE}}
clean:
cargo clean

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

View File

@ -0,0 +1,11 @@
use std::{env, io};
fn main() -> io::Result<()> {
let infile_path = env::args().nth(1).expect("input file");
let sums = aoc_2022_01::input_sums(infile_path)?;
let max = sums.max().unwrap();
println!("{max}");
Ok(())
}

View File

@ -0,0 +1,12 @@
use std::{env, io};
fn main() -> io::Result<()> {
let infile_path = env::args().nth(1).expect("input file");
let mut sums = aoc_2022_01::input_sums(infile_path)?.collect::<Vec<_>>();
sums.sort_by(|a, b| b.partial_cmp(a).unwrap());
let three_highest_sum: &u32 = &sums[..3].iter().sum();
println!("{three_highest_sum}");
Ok(())
}

44
2022/day-01/src/lib.rs Normal file
View File

@ -0,0 +1,44 @@
use std::{
fs::File,
io::{self, BufRead, BufReader},
path::Path,
};
pub fn input_sums<P>(path: P) -> io::Result<impl Iterator<Item = u32>>
where
P: AsRef<Path>,
{
let inputs = parse_input_file(path)?;
let sums = inputs.into_iter().map(|val| val.into_iter().sum());
Ok(sums)
}
fn parse_input_file<P>(path: P) -> io::Result<Vec<Vec<u32>>>
where
P: AsRef<Path>,
{
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut inputs = Vec::new();
let mut current = Vec::new();
for line in reader.lines() {
let line = line?;
if line.is_empty() {
inputs.push(current);
current = Vec::new();
} else {
let value = line.parse().expect("invalid number");
current.push(value);
}
}
if !current.is_empty() {
inputs.push(current);
}
Ok(inputs)
}

View File

@ -9,3 +9,4 @@
- [2019](2019/README.md) (0% completed) - [2019](2019/README.md) (0% completed)
- [2020](2020/README.md) (20% completed) - [2020](2020/README.md) (20% completed)
- [2021](2021/README.md) (68% completed) - [2021](2021/README.md) (68% completed)
- [2022](2022/README.md) (4% completed)