This commit is contained in:
Tyler Beckman 2024-12-11 00:43:40 -07:00
parent 8becab7e9e
commit e9d8b35d8c
Signed by: Ty
GPG key ID: 2813440C772555A4
7 changed files with 127 additions and 5 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
**/input.txt
**/example.txt
.direnv
node_modules
node_modules
target

7
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 = "aoc2024"
version = "0.1.0"

8
Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "aoc2024"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "d11p2"
path = "day11/part2.rs"

27
day11/part1.kts Executable file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env kotlin
import java.io.File
import java.util.LinkedList
import kotlin.math.log10
val stones = LinkedList(File("input.txt").readLines()[0].split(" ").map { it.toLong() })
for (i in 0..<25) {
val iter = stones.listIterator()
while (iter.hasNext()) {
val stone = iter.next()
if (stone == 0L) {
iter.set(1)
} else if ((log10(stone.toDouble()).toInt() + 1) % 2 == 0) {
val str = stone.toString()
iter.set(str.substring(0, str.length / 2).toLong())
iter.add(str.substring(str.length / 2, str.length).toLong())
} else {
iter.set(stone * 2024)
}
}
}
println("Result: ${stones.size}")

36
day11/part2.rs Normal file
View file

@ -0,0 +1,36 @@
use std::collections::HashMap;
fn main() {
let input = include_str!("input.txt");
let mut stones = input
.split(' ')
.map(|s| (s.parse::<u64>().unwrap(), 1u64))
.collect::<HashMap<u64, u64>>();
for _ in 1..=75 {
let mut new_stones = HashMap::<u64, u64>::new();
for stone in stones.keys() {
if *stone == 0 {
// Take all zeroes, and move them to ones
new_stones.insert(1, new_stones.get(&1).unwrap_or(&0) + stones[&0]);
} else {
let digits = stone.ilog10() + 1;
if digits % 2 == 0 {
let new_rock = stone / 10u64.pow(digits / 2);
new_stones.insert(new_rock, new_stones.get(&new_rock).unwrap_or(&0) + stones[stone]);
let new_rock = stone % 10u64.pow(digits / 2);
new_stones.insert(new_rock, new_stones.get(&new_rock).unwrap_or(&0) + stones[stone]);
} else {
let new_rock = stone * 2024;
new_stones.insert(new_rock, new_stones.get(&new_rock).unwrap_or(&0) + stones[stone]);
}
}
}
stones = new_stones;
}
println!("Result: {}", stones.into_values().sum::<u64>());
}

View file

@ -34,10 +34,45 @@
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1728538411,
"narHash": "sha256-f0SBJz1eZ2yOuKUr5CA9BHULGXVSn6miBuUWdTyhUhU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b69de56fac8c2b6f8fd27f2eca01dcda8e0a4221",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1733884434,
"narHash": "sha256-8GXR9kC07dyOIshAyfZhG11xfvBRSZzYghnZ2weOKJU=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "d0483df44ddf0fd1985f564abccbe568e020ddf2",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {

View file

@ -3,14 +3,22 @@
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.rust-overlay.url = "github:oxalica/rust-overlay";
outputs = { nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; in {
devShells.default = pkgs.mkShellNoCC {
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
}; in {
devShells.default = pkgs.mkShell {
packages = with pkgs; [
deno
kotlin
libsecret
(rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
extensions = [ "rust-src" "rustfmt" ];
targets = [ "x86_64-unknown-linux-gnu" ];
}))
];
};
});