Skip to content

Commit

Permalink
feat: day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
tatupesonen committed Dec 4, 2024
1 parent 05b34ab commit dd16539
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 8 deletions.
31 changes: 31 additions & 0 deletions aoc2024/inputs/1/day1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

if(!file_exists("./input.txt")) {
echo "Missing input.txt\n";
exit(1);
}

$input = file_get_contents("./input.txt");

$left = [];
$right = [];
$split_on = " ";
$lines = explode("\n", $input);

foreach($lines as $line) {
$split = explode($split_on, $line);
$left[] = $split[0];
$right[] = $split[1];
}

sort($left);
sort($right);

$distances = [];
for($i = 0; $i < count($left); $i++) {
$distances[] = abs($left[$i] - $right[$i]);
}

var_dump(array_sum($distances));

?>
2 changes: 1 addition & 1 deletion aoc2024/inputs/1/test-input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
2 5
1 3
3 9
3 3
3 3
140 changes: 140 additions & 0 deletions aoc2024/inputs/4/input.txt

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions aoc2024/inputs/4/test-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
15 changes: 13 additions & 2 deletions aoc2024/src/days/day01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,27 @@ impl Solution for Problem {
#[cfg(test)]
mod tests {
const TEST_INPUT: &str = include_str!("../../inputs/1/test-input.txt");
const INPUT: &str = include_str!("../../inputs/1/input.txt");

use super::*;

#[test]
fn part1() {
fn part1_test() {
assert_eq!(Problem.part_one(TEST_INPUT), "11");
}

#[test]
fn part2() {
fn part2_test() {
assert_eq!(Problem.part_two(TEST_INPUT), "31");
}

#[test]
fn part1() {
assert_eq!(Problem.part_one(INPUT), "2066446");
}

#[test]
fn part2() {
assert_eq!(Problem.part_two(INPUT), "24931009");
}
}
15 changes: 13 additions & 2 deletions aoc2024/src/days/day02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,27 @@ impl Solution for Problem {
#[cfg(test)]
mod tests {
const TEST_INPUT: &str = include_str!("../../inputs/2/test-input.txt");
const INPUT: &str = include_str!("../../inputs/2/input.txt");

use super::*;

#[test]
fn part1() {
fn part1_test() {
assert_eq!(Problem.part_one(TEST_INPUT), "2");
}

#[test]
fn part2() {
fn part2_test() {
assert_eq!(Problem.part_two(TEST_INPUT), "5");
}

#[test]
fn part1() {
assert_eq!(Problem.part_one(INPUT), "326");
}

#[test]
fn part2() {
assert_eq!(Problem.part_two(INPUT), "381");
}
}
15 changes: 13 additions & 2 deletions aoc2024/src/days/day03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,27 @@ impl Solution for Problem {
mod tests {
const TEST_INPUT: &str = include_str!("../../inputs/3/test-input.txt");
const TEST_INPUT_2: &str = include_str!("../../inputs/3/test-input-2.txt");
const INPUT: &str = include_str!("../../inputs/3/input.txt");

use super::*;

#[test]
fn part1() {
fn part1_test() {
assert_eq!(Problem.part_one(TEST_INPUT), "161");
}

#[test]
fn part2() {
fn part2_test() {
assert_eq!(Problem.part_two(TEST_INPUT_2), "48");
}

#[test]
fn part1() {
assert_eq!(Problem.part_one(INPUT), "178886550");
}

#[test]
fn part2() {
assert_eq!(Problem.part_two(INPUT), "87163705");
}
}
136 changes: 136 additions & 0 deletions aoc2024/src/days/day04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use crate::Solution;

const SUB: &str = "XMAS";

pub struct Problem;
impl Solution for Problem {
fn part_one(&self, input: &str) -> String {
let mtx: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();
let mut permutations: Vec<String> = vec![];

let num_cols = mtx[0].len();
let num_rows = mtx.len();
// Rows
for row in input.lines() {
permutations.push(row.to_owned());
}

// Cols
for col in 0..num_cols {
let mut column = String::with_capacity(num_rows);
for row in 0..num_rows {
column.push(mtx[row][col]);
}
permutations.push(column.clone());
}

// Primary diagonals
for start in 0..(num_rows + num_cols - 1) {
let mut diag = String::new();
for row in 0..num_rows {
let col = start as isize - row as isize;
if col >= 0 && col < num_cols as isize {
diag.push(mtx[row][col as usize]);
}
}
permutations.push(diag.clone());
}

// Secondary diagonals
for start in 0..(num_rows + num_cols - 1) {
let mut diag = String::new();
for row in 0..num_rows {
let col = start as isize - row as isize;
if col >= 0 && col < num_cols as isize {
diag.push(mtx[row][num_cols - 1 - col as usize]);
}
}
permutations.push(diag.clone());
}

let mut count = 0;
for line in permutations {
count += line
.as_bytes()
.windows(SUB.len())
.filter(|&w| w == SUB.as_bytes())
.count();

let row_rev = line.chars().rev().collect::<String>();
count += row_rev
.as_bytes()
.windows(SUB.len())
.filter(|&w| w == SUB.as_bytes())
.count();
}

count.to_string()
}

fn part_two(&self, input: &str) -> String {
// thank god part 2 is WAY easier.
let mtx: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();

let num_cols = mtx[0].len();
let num_rows = mtx.len();
let mut a_coords = vec![];

for row in 1..(num_rows - 1) {
for col in 1..(num_cols - 1) {
if mtx[row][col] == 'A' {
a_coords.push((row, col))
}
}
}
// check original coords
let mut count = 0;
for a in a_coords {
// check [-1, -1] up left
// check [1, 1] down right
let ul = mtx[a.0 - 1][a.1 - 1];
let dr = mtx[a.0 + 1][a.1 + 1];

// check [-1, 1] up right
// check [1, -1] down left
let ur = mtx[a.0 - 1][a.1 + 1];
let dl = mtx[a.0 + 1][a.1 - 1];

let urdl = format!("{}{}", ur, dl);
let uldr = format!("{}{}", ul, dr);

if (urdl == "SM" || urdl == "MS") && (uldr == "SM" || uldr == "MS") {
count += 1;
}
}

count.to_string()
}
}

#[cfg(test)]
mod tests {
const TEST_INPUT: &str = include_str!("../../inputs/4/test-input.txt");
const INPUT: &str = include_str!("../../inputs/4/input.txt");

use super::*;

#[test]
fn part1_test() {
assert_eq!(Problem.part_one(TEST_INPUT), "18");
}

#[test]
fn part2_test() {
assert_eq!(Problem.part_two(TEST_INPUT), "9");
}

#[test]
fn part1() {
assert_eq!(Problem.part_one(INPUT), "2547");
}

#[test]
fn part2() {
assert_eq!(Problem.part_two(INPUT), "1939");
}
}
3 changes: 2 additions & 1 deletion aoc2024/src/days/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod day01;
pub mod day02;
pub mod day03;
pub mod day03;
pub mod day04;
1 change: 1 addition & 0 deletions aoc2024/src/dayselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn select_day(day: usize) -> Option<Box<dyn Solution>> {
1 => Some(Box::new(aoc2024::days::day01::Problem)),
2 => Some(Box::new(aoc2024::days::day02::Problem)),
3 => Some(Box::new(aoc2024::days::day03::Problem)),
4 => Some(Box::new(aoc2024::days::day04::Problem)),
_ => None,
}
}

0 comments on commit dd16539

Please sign in to comment.