Day 11
This commit is contained in:
parent
8becab7e9e
commit
e9d8b35d8c
7 changed files with 127 additions and 5 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
**/input.txt
|
**/input.txt
|
||||||
**/example.txt
|
**/example.txt
|
||||||
.direnv
|
.direnv
|
||||||
node_modules
|
node_modules
|
||||||
|
target
|
||||||
|
|
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 = "aoc2024"
|
||||||
|
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal 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
27
day11/part1.kts
Executable 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
36
day11/part2.rs
Normal 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>());
|
||||||
|
}
|
37
flake.lock
37
flake.lock
|
@ -34,10 +34,45 @@
|
||||||
"type": "github"
|
"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": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"flake-utils": "flake-utils",
|
"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": {
|
"systems": {
|
||||||
|
|
14
flake.nix
14
flake.nix
|
@ -3,14 +3,22 @@
|
||||||
|
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
inputs.flake-utils.url = "github:numtide/flake-utils";
|
inputs.flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
inputs.rust-overlay.url = "github:oxalica/rust-overlay";
|
||||||
|
|
||||||
outputs = { nixpkgs, flake-utils, ... }:
|
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
|
||||||
flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; in {
|
flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {
|
||||||
devShells.default = pkgs.mkShellNoCC {
|
inherit system;
|
||||||
|
overlays = [ rust-overlay.overlays.default ];
|
||||||
|
}; in {
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
deno
|
deno
|
||||||
kotlin
|
kotlin
|
||||||
libsecret
|
libsecret
|
||||||
|
(rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
|
||||||
|
extensions = [ "rust-src" "rustfmt" ];
|
||||||
|
targets = [ "x86_64-unknown-linux-gnu" ];
|
||||||
|
}))
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue