my solutions to advent of code
aoc advent-of-code

part 1 day 3 2025 dont ask

aylac.top 65fd30cb f6320cb8

verified
Changed files
+117 -19
2015
18
rust
src
2025
+24 -19
2015/18/rust/src/main.rs
···
-
use std::mem::swap;
-
-
#[inline]
-
fn get_at(world: &Vec<u8>, size: usize, x: usize, y: usize) -> u8 {
-
// benefits from the integer overflow to simplify code
-
if x >= size || y >= size {
-
return 0;
-
};
-
// this is in known bounds
-
unsafe { *world.get_unchecked(y * size + x) }
-
}
+
use std::{iter::once, mem::swap};
fn generations(times: u32, mut world: Vec<u8>, size: usize, stuck: bool) -> Vec<u8> {
+
#[inline]
+
fn get_at(world: &Vec<u8>, size: usize, x: usize, y: usize) -> u8 {
+
// benefits from the integer overflow to simplify code
+
if x >= size || y >= size {
+
return 0;
+
};
+
// this is in known bounds
+
unsafe { *world.get_unchecked(y * size + x) }
+
}
+
let mut new_world = vec![0_u8; size * size];
let sizem = size - 1;
if stuck {
···
}
for _ in 0..times {
-
for yo in 0..size {
-
let ym = yo.wrapping_sub(1);
+
for yo in 1..=size {
+
let ym = yo - 1;
let yp = yo + 1;
-
for xo in 0..size {
-
let xm = xo.wrapping_sub(1);
+
for xo in 1..=size {
+
let xm = xo - 1;
let xp = xo + 1;
let was = get_at(&world, size, xo, yo) == 1;
···
fn main() {
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
-
let input: Vec<u8> = input
-
.replace("\n", "")
-
.chars()
-
.map(|v| (v == '#') as u8)
+
let input: Vec<u8> = once(".".repeat(size))
+
.chain(input.split("\n"))
+
.chain(".".repeat(size))
+
.map(|line| {
+
once(0)
+
.chain(line.chars().map(|v| (v == '#') as u8))
+
.chain(once(0))
+
})
+
.flatten()
.collect();
let part_1 = generations(100, input.clone(), size, false)
+20
2025/3/gleam/gleam.toml
···
+
name = "main"
+
version = "1.0.0"
+
+
# Fill out these fields if you intend to generate HTML documentation or publish
+
# your project to the Hex package manager.
+
#
+
# description = ""
+
# licences = ["Apache-2.0"]
+
# repository = { type = "github", user = "", repo = "" }
+
# links = [{ title = "Website", href = "" }]
+
#
+
# For a full reference of all the available options, you can have a look at
+
# https://gleam.run/writing-gleam/gleam-toml/.
+
+
[dependencies]
+
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
+
simplifile = ">= 2.3.0 and < 3.0.0"
+
+
[dev-dependencies]
+
gleeunit = ">= 1.0.0 and < 2.0.0"
+14
2025/3/gleam/manifest.toml
···
+
# This file was generated by Gleam
+
# You typically do not need to edit this file
+
+
packages = [
+
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
+
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
+
{ name = "gleeunit", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "CD701726CBCE5588B375D157B4391CFD0F2F134CD12D9B6998A395484DE05C58" },
+
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
+
]
+
+
[requirements]
+
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
+
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
+
simplifile = { version = ">= 2.3.0 and < 3.0.0" }
+59
2025/3/gleam/src/main.gleam
···
+
import gleam/float
+
import gleam/int
+
import gleam/io
+
import gleam/list
+
import gleam/result
+
import gleam/string
+
import simplifile as file
+
+
pub fn main() {
+
let assert Ok(input) = file.read(from: "../input.txt")
+
as "Input file not found"
+
let input =
+
input
+
|> string.trim
+
|> string.split("\n")
+
|> list.map(fn(bank) {
+
string.to_graphemes(bank)
+
|> list.map(fn(s) { int.parse(s) |> result.unwrap(0) })
+
})
+
+
input
+
|> list.fold(0, fn(acc, bank) {
+
let #(n, _) =
+
list.range(2, 1)
+
|> list.fold(#(0, bank), fn(acc, i) {
+
let #(number, bank) = acc
+
let bank_find = case i {
+
2 ->
+
bank
+
|> list.reverse
+
|> list.drop(1)
+
|> list.reverse
+
_ -> bank
+
}
+
let max =
+
bank_find
+
|> list.max(int.compare)
+
|> result.unwrap(list.last(bank) |> result.unwrap(0))
+
+
let max_loc =
+
bank
+
|> list.index_map(fn(n, i) { #(n, i) })
+
|> list.key_find(max)
+
|> result.unwrap(0)
+
#(
+
number
+
+ max
+
* case i {
+
2 -> 10
+
_ -> 1
+
},
+
list.drop(bank, max_loc + 1),
+
)
+
})
+
acc + n
+
})
+
|> int.to_string
+
|> io.println
+
}