Solve day01 challenge
This commit is contained in:
commit
78edb8e26c
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
/target
|
||||||
|
result
|
||||||
|
flamegraph.svg
|
||||||
|
.direnv
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day01"
|
||||||
|
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[workspace]
|
||||||
|
members = [ "day01" ]
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
panic = "abort"
|
||||||
|
strip = "symbols"
|
||||||
|
lto = true
|
||||||
|
codegen-units = 1
|
10
day01/Cargo.toml
Normal file
10
day01/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "day01"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[package.metadata.nix]
|
||||||
|
build = true
|
||||||
|
app = true
|
||||||
|
|
||||||
|
[dependencies]
|
2251
day01/input
Normal file
2251
day01/input
Normal file
File diff suppressed because it is too large
Load diff
14
day01/input.test
Normal file
14
day01/input.test
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000
|
87
day01/src/main.rs
Normal file
87
day01/src/main.rs
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
//! Find the elves carrying the most calories.
|
||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn either_calories_or_blank(inp: String) -> Option<usize> {
|
||||||
|
inp.parse().ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct TopThree {
|
||||||
|
first: usize,
|
||||||
|
second: usize,
|
||||||
|
third: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct TopCal {
|
||||||
|
top: TopThree,
|
||||||
|
curr_sum: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TopCal {
|
||||||
|
const fn push_line(self, line: Option<usize>) -> Self {
|
||||||
|
match line {
|
||||||
|
Some(cal) => TopCal {
|
||||||
|
curr_sum: self.curr_sum + cal,
|
||||||
|
..self
|
||||||
|
},
|
||||||
|
None => TopCal {
|
||||||
|
curr_sum: 0,
|
||||||
|
top: TopThree::push_sum(self.top, self.curr_sum),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TopThree {
|
||||||
|
const fn push_sum(self, sum: usize) -> Self {
|
||||||
|
let (first, second, third) = if sum > self.first {
|
||||||
|
(sum, self.first, self.second)
|
||||||
|
} else if sum > self.second {
|
||||||
|
(self.first, sum, self.second)
|
||||||
|
} else if sum > self.third {
|
||||||
|
(self.first, self.second, sum)
|
||||||
|
} else {
|
||||||
|
(self.first, self.second, self.third)
|
||||||
|
};
|
||||||
|
Self {
|
||||||
|
first,
|
||||||
|
second,
|
||||||
|
third,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fn total_sum(&self) -> usize {
|
||||||
|
self.first + self.second + self.third
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let file = ::std::env::args()
|
||||||
|
.skip(1)
|
||||||
|
.next()
|
||||||
|
.unwrap_or_else(|| String::from("./input"));
|
||||||
|
let file = BufReader::new(File::open(file).expect("Input file not found"));
|
||||||
|
let TopCal { top, .. } = file
|
||||||
|
.lines()
|
||||||
|
.map(Result::unwrap)
|
||||||
|
.map(either_calories_or_blank)
|
||||||
|
.collect();
|
||||||
|
println!(
|
||||||
|
"Max calories carried by a elves ({}, {}, {}): {}",
|
||||||
|
top.first,
|
||||||
|
top.second,
|
||||||
|
top.third,
|
||||||
|
top.total_sum()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromIterator<Option<usize>> for TopCal {
|
||||||
|
fn from_iter<T: IntoIterator<Item = Option<usize>>>(iter: T) -> Self {
|
||||||
|
let max = iter.into_iter().fold(Self::default(), TopCal::push_line);
|
||||||
|
// Make sure to read one extra empty line at the end
|
||||||
|
TopCal::push_line(max, None)
|
||||||
|
}
|
||||||
|
}
|
200
flake.lock
Normal file
200
flake.lock
Normal file
|
@ -0,0 +1,200 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"crane": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1661875961,
|
||||||
|
"narHash": "sha256-f1h/2c6Teeu1ofAHWzrS8TwBPcnN+EEu+z1sRVmMQTk=",
|
||||||
|
"owner": "ipetkov",
|
||||||
|
"repo": "crane",
|
||||||
|
"rev": "d9f394e4e20e97c2a60c3ad82c2b6ef99be19e24",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "ipetkov",
|
||||||
|
"repo": "crane",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"devshell": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1667210711,
|
||||||
|
"narHash": "sha256-IoErjXZAkzYWHEpQqwu/DeRNJGFdR7X2OGbkhMqMrpw=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "devshell",
|
||||||
|
"rev": "96a9dd12b8a447840cc246e17a47b81a4268bba7",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "devshell",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"dream2nix": {
|
||||||
|
"inputs": {
|
||||||
|
"alejandra": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"all-cabal-json": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"crane": "crane",
|
||||||
|
"devshell": [
|
||||||
|
"nixCargoIntegration",
|
||||||
|
"devshell"
|
||||||
|
],
|
||||||
|
"flake-parts": "flake-parts",
|
||||||
|
"flake-utils-pre-commit": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"ghc-utils": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"gomod2nix": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"mach-nix": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"nix-pypi-fetcher": "nix-pypi-fetcher",
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixCargoIntegration",
|
||||||
|
"nixpkgs"
|
||||||
|
],
|
||||||
|
"poetry2nix": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
],
|
||||||
|
"pre-commit-hooks": [
|
||||||
|
"nixCargoIntegration"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1669743839,
|
||||||
|
"narHash": "sha256-zxnaRaWfCJxy0JlORD4Kmtzd0pfpcGLnyaCIJY8OlIo=",
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "dream2nix",
|
||||||
|
"rev": "b6af93946130748f72671dfd2ab84a5aeaf1f191",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-community",
|
||||||
|
"repo": "dream2nix",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-parts": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs-lib": "nixpkgs-lib"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1668450977,
|
||||||
|
"narHash": "sha256-cfLhMhnvXn6x1vPm+Jow3RiFAUSCw/l1utktCw5rVA4=",
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "flake-parts",
|
||||||
|
"rev": "d591857e9d7dd9ddbfba0ea02b43b927c3c0f1fa",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "flake-parts",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nix-pypi-fetcher": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1669065297,
|
||||||
|
"narHash": "sha256-UStjXjNIuIm7SzMOWvuYWIHBkPUKQ8Id63BMJjnIDoA=",
|
||||||
|
"owner": "DavHau",
|
||||||
|
"repo": "nix-pypi-fetcher",
|
||||||
|
"rev": "a9885ac6a091576b5195d547ac743d45a2a615ac",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "DavHau",
|
||||||
|
"repo": "nix-pypi-fetcher",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixCargoIntegration": {
|
||||||
|
"inputs": {
|
||||||
|
"devshell": "devshell",
|
||||||
|
"dream2nix": "dream2nix",
|
||||||
|
"nixpkgs": "nixpkgs",
|
||||||
|
"rust-overlay": "rust-overlay"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1669788752,
|
||||||
|
"narHash": "sha256-2rFW93yI0oa78IMMa0PFk0h9oPo/GLzNTFOb9bb/yfo=",
|
||||||
|
"owner": "yusdacra",
|
||||||
|
"repo": "nix-cargo-integration",
|
||||||
|
"rev": "f65b42f7499e242f2af48153bb9baa621e9e45e6",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "yusdacra",
|
||||||
|
"repo": "nix-cargo-integration",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1669542132,
|
||||||
|
"narHash": "sha256-DRlg++NJAwPh8io3ExBJdNW7Djs3plVI5jgYQ+iXAZQ=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "a115bb9bd56831941be3776c8a94005867f316a7",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs-lib": {
|
||||||
|
"locked": {
|
||||||
|
"dir": "lib",
|
||||||
|
"lastModified": 1665349835,
|
||||||
|
"narHash": "sha256-UK4urM3iN80UXQ7EaOappDzcisYIuEURFRoGQ/yPkug=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "34c5293a71ffdb2fe054eb5288adc1882c1eb0b1",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"dir": "lib",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixCargoIntegration": "nixCargoIntegration"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"rust-overlay": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1669775522,
|
||||||
|
"narHash": "sha256-6xxGArBqssX38DdHpDoPcPvB/e79uXyQBwpBcaO/BwY=",
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"rev": "3158e47f6b85a288d12948aeb9a048e0ed4434d6",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "oxalica",
|
||||||
|
"repo": "rust-overlay",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
23
flake.nix
Normal file
23
flake.nix
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
nixCargoIntegration.url = "github:yusdacra/nix-cargo-integration";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = inputs:
|
||||||
|
inputs.nixCargoIntegration.lib.makeOutputs {
|
||||||
|
root = ./.;
|
||||||
|
config = common: {
|
||||||
|
shell = {
|
||||||
|
packages = with common.pkgs; [
|
||||||
|
rust-analyzer
|
||||||
|
lldb
|
||||||
|
treefmt
|
||||||
|
cargo-watch
|
||||||
|
cargo-flamegraph
|
||||||
|
pkg-config
|
||||||
|
hyperfine
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue