This commit is contained in:
James Hogge
2025-12-09 04:18:46 +00:00
commit b1dc693fbf
21 changed files with 8413 additions and 0 deletions

34
src/1-secret-entrance.rs Normal file
View File

@@ -0,0 +1,34 @@
use std::fs::File;
use std::io::{self, BufRead, BufReader};
const DIAL_SIZE: i64 = 100;
fn parse_turn(s: &str) -> i64 {
let (dir, num) = s.split_at(1);
let sign = match dir {
"L" => -1,
"R" => 1,
_ => panic!("Bad input direction"),
};
sign * num.parse::<i64>().unwrap()
}
fn main() -> Result<(), io::Error>{
let f = File::open("1-input.txt")?;
let br = BufReader::new(f);
let mut position = 50;
let mut zeroes = 0;
for line in br.lines() {
let line = line?;
let resulting_position = position + parse_turn(&line);
zeroes += (resulting_position / DIAL_SIZE).abs();
if position > 0 && resulting_position <= 0 {
zeroes += 1;
}
position = resulting_position.rem_euclid(DIAL_SIZE);
// println!("Move: {}, (P: {}, C: {})", line, position, zeroes);
}
println!("Result: {}", zeroes);
Ok(())
}

52
src/2-gift-shop.rs Normal file
View File

@@ -0,0 +1,52 @@
use std::{fs::File, io::{self, BufRead, BufReader}};
fn parse_range(s: &str) -> (i64, i64) {
let (low_s, high_s) = s.split_once('-').unwrap();
(low_s.parse().unwrap(), high_s.parse().unwrap())
}
fn invalid_in_range(low: i64, high: i64) -> i64 {
let mut total: i64 = 0;
for i in low..=high {
let len = i.ilog10() + 1;
for j in (2..=len).filter(|x| len % x == 0) {
let split = 10i64.pow(len / j);
let target = i % split;
let mut mut_i = i;
while mut_i > 0 && (mut_i % split) == target {
mut_i /= split;
}
if mut_i == 0 {
// println!("{} in {}-{}", i, low, high);
total += i;
break
}
}
}
total
}
fn main() -> Result<(), io::Error> {
let f = File::open("2-input.txt")?;
let reader = BufReader::new(f);
let mut ranges = reader.lines()
.next()
.unwrap()
.unwrap()
.split(",")
.map(parse_range)
.collect::<Vec<_>>();
ranges.sort_unstable();
let mut total: i64 = 0;
let mut last: i64 = 0;
for (low, high) in ranges {
let low = low.max(last);
if low <= high {
total += invalid_in_range(low, high);
}
last = high + 1;
}
println!("Result {}", total);
Ok(())
}

37
src/3-lobby.rs Normal file
View File

@@ -0,0 +1,37 @@
use std::{fs::File, io::{self, BufRead, BufReader}};
fn max_joltage(s: &str) -> u64 {
let values = s.chars()
.map(|x| x.to_digit(10).unwrap())
.collect::<Vec<_>>();
let max_idx = |xs: &[u32]| {
xs.iter()
.enumerate()
.fold(0, |max, (i, &x)| {
if x > xs[max] { i } else { max }
})
};
let (_, total) =
(0..12).rev().fold(
(0, 0u64),
|(start_idx, total), i| {
let next_idx = start_idx + max_idx(&values[start_idx..values.len() - i]);
(next_idx + 1, 10 * total + values[next_idx] as u64)
}
);
total
}
fn main() -> Result<(), io::Error> {
let f = File::open("3-input.txt")?;
let reader = BufReader::new(f);
let mut total: u64 = 0;
for line in reader.lines() {
total += max_joltage(line?.as_str()) as u64;
}
println!("Result: {}", total);
Ok(())
}

View File

@@ -0,0 +1,64 @@
use std::{fs::File, io::{self, BufRead, BufReader}};
const NEIGHBOURS: [(i32, i32); 8] = [
(-1, -1), (0, -1), (1, -1),
(-1, 0), (1, 0),
(-1, 1), (0, 1), (1, 1),
];
fn get_grid_cell(grid: &[Vec<bool>], x: i32, y: i32) -> bool {
let (Ok(ux), Ok(uy)) = (usize::try_from(x), usize::try_from(y)) else {
return false
};
grid.get(uy)
.and_then(|row| row.get(ux))
.copied()
.unwrap_or(false)
}
fn count_surrounding(grid: &[Vec<bool>], x: i32, y: i32) -> i32 {
NEIGHBOURS
.iter()
.map(|(dx, dy)| get_grid_cell(grid, x + dx, y + dy))
.filter(|&p| p)
.count() as i32
}
fn update_grid(grid: &[Vec<bool>]) -> Vec<Vec<bool>> {
let height = grid.len();
let width = grid[0].len();
(0..height).map(|y| {
(0..width).map(|x| {
let x = x as i32;
let y = y as i32;
get_grid_cell(grid, x, y) && (count_surrounding(grid, x, y) >= 4)
}).collect::<Vec<_>>()
}).collect::<Vec<_>>()
}
fn count_all_true(grid: &[Vec<bool>]) -> usize {
grid.iter().flatten().filter(|&&b| b).count()
}
fn main() -> Result<(), io::Error> {
let f = File::open("4-input.txt")?;
let reader = BufReader::new(f);
let original_grid = reader
.lines()
.collect::<Result<Vec<_>, _>>()?
.iter()
.map(|s| s.chars().map(|c| c == '@').collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut last_grid = original_grid.clone();
loop {
let next_grid = update_grid(&last_grid);
if next_grid == last_grid {
break;
}
last_grid = next_grid;
}
println!("Result: {}", count_all_true(&original_grid) - count_all_true(&last_grid));
Ok(())
}

49
src/5-cafeteria.rs Normal file
View File

@@ -0,0 +1,49 @@
use std::{cmp::max, fs::File, io::{self, BufRead, BufReader}};
fn parse_range(s: &str) -> (u64, u64) {
let (s1, s2) = s.split_once('-').unwrap();
(s1.parse().unwrap(), s2.parse().unwrap())
}
fn main() -> Result<(), io::Error> {
let f = File::open("5-input.txt")?;
let reader = BufReader::new(f);
let mut lines = reader.lines().map_while(Result::ok);
let mut ranges: Vec<_> = lines
.by_ref()
.take_while(|l| !l.is_empty())
.map(|l| parse_range(&l))
.collect();
ranges.sort_unstable();
let mut ids: Vec<u64> = lines
.map(|l| l.parse::<u64>().unwrap())
.collect();
ids.sort_unstable();
let mut total = 0;
// let mut range_idx = 0;
// let mut id_idx = 0;
// while id_idx < ids.len() && range_idx < ranges.len() {
// let (low, high) = ranges[range_idx];
// if ids[id_idx] < low {
// id_idx += 1;
// } else if ids[id_idx] <= high {
// id_idx += 1;
// total += 1;
// } else {
// range_idx += 1;
// }
// }
let mut max_seen = 0;
for (low, high) in ranges {
let new_low = max(low, max_seen + 1);
max_seen = max(high, max_seen);
total += 1 + max_seen - new_low;
}
println!("Result {}", total);
Ok(())
}