diff --git a/.gitignore b/.gitignore index 3c5b90b..563dad8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/input.txt **/example.txt .direnv -node_modules \ No newline at end of file +node_modules +target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5d41a28 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..57c7c47 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc2024" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "d11p2" +path = "day11/part2.rs" diff --git a/day11/part1.kts b/day11/part1.kts new file mode 100755 index 0000000..48f0bdb --- /dev/null +++ b/day11/part1.kts @@ -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}") \ No newline at end of file diff --git a/day11/part2.rs b/day11/part2.rs new file mode 100644 index 0000000..8adc992 --- /dev/null +++ b/day11/part2.rs @@ -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::().unwrap(), 1u64)) + .collect::>(); + + for _ in 1..=75 { + let mut new_stones = HashMap::::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::()); +} diff --git a/flake.lock b/flake.lock index 1dbaa68..a632061 100644 --- a/flake.lock +++ b/flake.lock @@ -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": { diff --git a/flake.nix b/flake.nix index a1546c3..a00c340 100644 --- a/flake.nix +++ b/flake.nix @@ -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" ]; + })) ]; }; });