feat: added day 1 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Signed-off-by: Louis Vallat <contact@louis-vallat.fr>
This commit is contained in:
commit
c7e7823bc1
22
.gitea/workflows/build_and_run.yml
Normal file
22
.gitea/workflows/build_and_run.yml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
name: Build and run challenges
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
shell: sh
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-and-run:
|
||||||
|
name: Challenge for day
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
day_number: [1]
|
||||||
|
runs-on: rust-bookworm
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Run challenge
|
||||||
|
run: |
|
||||||
|
cd day${{ matrix.day_number }}
|
||||||
|
cargo run --release ./input
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
target
|
||||||
|
Cargo.lock
|
||||||
|
.idea/
|
6
day1/Cargo.toml
Normal file
6
day1/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day-1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
1000
day1/input
Normal file
1000
day1/input
Normal file
File diff suppressed because it is too large
Load Diff
59
day1/src/main.rs
Normal file
59
day1/src/main.rs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::{env, fs};
|
||||||
|
|
||||||
|
fn read_input(path: &str) -> String {
|
||||||
|
fs::read_to_string(path).expect("Cannot read file.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(input: &str) -> (Vec<i32>, Vec<i32>) {
|
||||||
|
let (mut right, mut left) =
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.filter(|line| !line.is_empty())
|
||||||
|
.fold((vec![], vec![]), |mut acc, line| {
|
||||||
|
let split = line.trim().split_once(" ").expect("Cannot split line.");
|
||||||
|
acc.0.push(
|
||||||
|
split
|
||||||
|
.0
|
||||||
|
.parse::<i32>()
|
||||||
|
.expect("Cannot parse integer on the left."),
|
||||||
|
);
|
||||||
|
acc.1.push(
|
||||||
|
split
|
||||||
|
.1
|
||||||
|
.parse::<i32>()
|
||||||
|
.expect("Cannot parse integer on the right."),
|
||||||
|
);
|
||||||
|
acc
|
||||||
|
});
|
||||||
|
right.sort();
|
||||||
|
left.sort();
|
||||||
|
(left, right)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(left: &Vec<i32>, right: &Vec<i32>) -> i32 {
|
||||||
|
left.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, x)| (x - right[i]).abs())
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(left: &Vec<i32>, right: &Vec<i32>) -> i32 {
|
||||||
|
let mut occur = HashMap::new();
|
||||||
|
for x in right.iter() {
|
||||||
|
*occur.entry(x).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
left.iter().map(|x| x * occur.get(x).unwrap_or(&0)).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
for arg in args.iter().skip(1) {
|
||||||
|
let input = read_input(&arg);
|
||||||
|
let (left, right) = parse_input(&input);
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", part_1(&left, &right));
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", part_2(&left, &right));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user