-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05b34ab
commit dd16539
Showing
10 changed files
with
360 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
2 5 | ||
1 3 | ||
3 9 | ||
3 3 | ||
3 3 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
MMMSXXMASM | ||
MSAMXMSMSA | ||
AMXSXMAAMM | ||
MSAMASMSMX | ||
XMASAMXAMM | ||
XXAMMXXAMA | ||
SMSMSASXSS | ||
SAXAMASAAA | ||
MAMMMXMMMM | ||
MXMXAXMASX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters