This commit is contained in:
sepia 2024-12-04 04:57:42 -06:00
parent d84eede42f
commit 18927efd84
10 changed files with 1531 additions and 2017 deletions

View file

@ -1,6 +1,7 @@
pub fn day1() {
let puzzle_input = std::fs::read_to_string("res/puzzle_1.txt").unwrap();
let (mut left, mut right): (Vec<u64>, Vec<u64>) = puzzle_input
let input = crate::input(1);
let (mut left, mut right): (Vec<u64>, Vec<u64>) = input
.lines()
.filter(|line| !line.is_empty())
.map(|line| {

View file

@ -1,9 +1,9 @@
const DAMPENING: bool = true;
pub fn day2() {
let puzzle_input = std::fs::read_to_string("res/puzzle_2.txt").unwrap();
let input = crate::input(2);
let lines: Vec<Vec<u64>> = puzzle_input
let lines: Vec<Vec<u64>> = input
.lines()
.filter(|line| !line.is_empty())
.map(|line| {

View file

@ -1,14 +1,13 @@
use std::fs;
use regex::Regex;
const READ_ALL: bool = false;
pub fn day3() {
let input_text = fs::read_to_string("res/puzzle_3.txt").unwrap();
let input = crate::input(3);
let mut total: u64 = 0;
let mut enabled = true;
for statement in Regex::new(r"(?:mul\((?<a>\d{1,3}),(?<b>\d{1,3})\))|(?<do>do\(\))|(?<dont>don't\(\))").unwrap().captures_iter(&input_text) {
for statement in Regex::new(r"(?:mul\((?<a>\d{1,3}),(?<b>\d{1,3})\))|(?<do>do\(\))|(?<dont>don't\(\))").unwrap().captures_iter(&input) {
if statement.name("do").is_some() {
enabled = true;
} else if statement.name("dont").is_some() {

53
src/day4.rs Normal file
View file

@ -0,0 +1,53 @@
const DIRECTIONS: [(i64, i64); 8] = [
(1, 0),
(-1, 0),
(0, 1),
(0, -1),
(1, 1),
(-1, -1),
(1, -1),
(-1, 1),
];
pub fn day4() {
let input = crate::input(4);
let grid: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();
let mut total: usize = 0;
for y in 0..grid.len() {
for x in 0..grid[y].len() {
total += DIRECTIONS
.iter()
.filter(|(dx, dy)| check_word("XMAS", &grid, x, y, *dx, *dy))
.count();
}
}
println!("Total: {}", total);
let mut total_crossed: usize = 0;
for y in 1..(grid.len() - 1) {
for x in 1..(grid[y].len() - 1) {
let a = check_word("MAS", &grid, x - 1, y - 1, 1, 1)
|| check_word("MAS", &grid, x + 1, y + 1, -1, -1);
let b = check_word("MAS", &grid, x - 1, y + 1, 1, -1)
|| check_word("MAS", &grid, x + 1, y - 1, -1, 1);
if a && b {
total_crossed += 1;
}
}
}
println!("Total Crossed: {}", total_crossed);
}
fn check_word(word: &str, grid: &Vec<Vec<char>>, x: usize, y: usize, dx: i64, dy: i64) -> bool {
for d in 0..word.len() {
let a = (x as i64 + (d as i64 * dx)) as usize;
let b = (y as i64 + (d as i64 * dy)) as usize;
if grid.get(b).map(|it| it.get(a).copied()) != Some(word.chars().nth(d)) {
return false;
}
}
true
}

View file

@ -1,7 +1,21 @@
mod day1;
mod day2;
mod day3;
mod day4;
fn main() {
day3::day3();
day4::day4();
}
pub fn input(day: u8) -> String {
dotenvy::dotenv().ok();
let token = std::env::var("AOC_TOKEN").expect("AOC_TOKEN must be set in .env file");
let client = reqwest::blocking::Client::new();
client.get(format!("https://adventofcode.com/2024/day/{}/input", day))
.header("cookie", format!("session={}", token))
.send()
.expect("Failed to get input")
.text()
.expect("Failed to read response text")
}