Compare commits
2 Commits
c438d7d665
...
9ce742fd57
Author | SHA1 | Date | |
---|---|---|---|
9ce742fd57 | |||
f613854a56 |
35
2024/day-01/common.rs
Normal file
35
2024/day-01/common.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PuzzleInputs {
|
||||||
|
pub left: Vec<i32>,
|
||||||
|
pub right: Vec<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PuzzleInputs {
|
||||||
|
pub fn parse<P>(path: P) -> Self
|
||||||
|
where P: AsRef<Path>
|
||||||
|
{
|
||||||
|
let mut left = Vec::new();
|
||||||
|
let mut right = Vec::new();
|
||||||
|
|
||||||
|
let content = fs::read_to_string(path).unwrap();
|
||||||
|
|
||||||
|
for line in content.lines() {
|
||||||
|
let mut parts = line.split(' ');
|
||||||
|
|
||||||
|
// unwrap is very festive
|
||||||
|
let first = parts.next().unwrap();
|
||||||
|
let first = first.parse().unwrap();
|
||||||
|
let last = parts.last().unwrap();
|
||||||
|
let last = last.parse().unwrap();
|
||||||
|
|
||||||
|
left.push(first);
|
||||||
|
right.push(last);
|
||||||
|
}
|
||||||
|
|
||||||
|
Self { left, right }
|
||||||
|
}
|
||||||
|
}
|
22
2024/day-01/part_one.rs
Normal file
22
2024/day-01/part_one.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::PuzzleInputs;
|
||||||
|
|
||||||
|
|
||||||
|
const INPUT_FILE: &str = "puzzle.txt";
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut inputs = PuzzleInputs::parse(INPUT_FILE);
|
||||||
|
|
||||||
|
inputs.left.sort();
|
||||||
|
inputs.right.sort();
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for (l, r) in inputs.left.into_iter().zip(inputs.right) {
|
||||||
|
let dist = l.abs_diff(r);
|
||||||
|
sum += dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{sum}");
|
||||||
|
}
|
30
2024/day-01/part_two.rs
Normal file
30
2024/day-01/part_two.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use common::PuzzleInputs;
|
||||||
|
|
||||||
|
type Hist = HashMap<i32, i32>;
|
||||||
|
|
||||||
|
|
||||||
|
const INPUT_FILE: &str = "puzzle.txt";
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let inputs = PuzzleInputs::parse(INPUT_FILE);
|
||||||
|
let mut hist = Hist::new();
|
||||||
|
|
||||||
|
for elem in inputs.right {
|
||||||
|
hist.entry(elem).and_modify(|e| *e += 1).or_insert(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut score = 0;
|
||||||
|
|
||||||
|
for elem in &inputs.left {
|
||||||
|
if let Some(x) = hist.get(elem) {
|
||||||
|
score += elem * x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{score:?}");
|
||||||
|
}
|
1000
2024/day-01/puzzle.txt
Normal file
1000
2024/day-01/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2024/day-01/sample.txt
Normal file
6
2024/day-01/sample.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
48
flake.lock
Normal file
48
flake.lock
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1733024928,
|
||||||
|
"narHash": "sha256-n/DOfpKH1vkukuBnach91QBQId2dr5tkE7/7UrkV2zw=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "2c27ab2e60502d1ebb7cf38909de38663f762a79",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixpkgs-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs",
|
||||||
|
"rust-overlay": "rust-overlay"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-overlay": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1733020719,
|
||||||
|
"narHash": "sha256-Chv9+3zrf1DhdB9JyskjoV0vJbCQEgkVqrU3p4RPLv8=",
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"rev": "8e18f10703112e6c33e1c0d8b93e8305f6f0a75c",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
43
flake.nix
Normal file
43
flake.nix
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
{
|
||||||
|
description = "Advent of Code";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||||
|
rust-overlay = {
|
||||||
|
url = "github:oxalica/rust-overlay";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { nixpkgs, rust-overlay, ... }:
|
||||||
|
let
|
||||||
|
lib = nixpkgs.lib;
|
||||||
|
systems = ["x86_64-linux"];
|
||||||
|
|
||||||
|
forEachSystem = fn: lib.genAttrs systems (system:
|
||||||
|
let
|
||||||
|
overlays = [ (import rust-overlay) ];
|
||||||
|
pkgs = import nixpkgs { inherit system overlays; };
|
||||||
|
in fn { inherit pkgs; } );
|
||||||
|
|
||||||
|
mkRustToolchain = pkgs: pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
|
||||||
|
in {
|
||||||
|
devShells = forEachSystem ({ pkgs }: let
|
||||||
|
rustToolchain = mkRustToolchain pkgs;
|
||||||
|
rustNightlyToolchain = pkgs.rust-bin.selectLatestNightlyWith (t: t.minimal.override {
|
||||||
|
extensions = [ "rustfmt" ];
|
||||||
|
});
|
||||||
|
|
||||||
|
rustShell = pkgs.mkShell {
|
||||||
|
name = "AoC";
|
||||||
|
|
||||||
|
nativeBuildInputs = [
|
||||||
|
rustToolchain
|
||||||
|
rustNightlyToolchain
|
||||||
|
];
|
||||||
|
};
|
||||||
|
in {
|
||||||
|
default = rustShell;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
5
rust-toolchain.toml
Normal file
5
rust-toolchain.toml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "stable"
|
||||||
|
components = ["clippy", "rust-analyzer", "rust-src"]
|
||||||
|
targets = ["x86_64-unknown-linux-gnu"]
|
||||||
|
profile = "minimal" # includes rustc, cargo, and rust-std
|
18
rustfmt.toml
Normal file
18
rustfmt.toml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# Just enforce it here as well instead of relying on editorconfig alone
|
||||||
|
hard_tabs = false
|
||||||
|
tab_spaces = 4
|
||||||
|
newline_style = "Unix"
|
||||||
|
|
||||||
|
imports_granularity = "Module"
|
||||||
|
group_imports = "StdExternalCrate"
|
||||||
|
|
||||||
|
force_multiline_blocks = false
|
||||||
|
fn_single_line = false
|
||||||
|
comment_width = 100
|
||||||
|
wrap_comments = true
|
||||||
|
hex_literal_case = "Upper"
|
||||||
|
blank_lines_upper_bound = 2
|
||||||
|
overflow_delimited_expr = true
|
||||||
|
reorder_impl_items = true
|
Loading…
Reference in New Issue
Block a user