2024 Advent of code day 1.

This commit is contained in:
Dan Chadwick 2024-12-01 12:19:49 -05:00
parent d16a32477b
commit 167ccb9145
6 changed files with 1250 additions and 0 deletions

View File

@ -1,5 +1,7 @@
use crate::Solution;
use std::fs;
use std::collections::HashMap;
use std::process;
#[allow(dead_code)]
#[derive(Debug)]
@ -21,6 +23,8 @@ pub fn run_day1_p1() -> Solution {
facing: 'N',
};
let mut visited_coords: HashMap<String, String> = HashMap::new();
for directive in directions {
let directive = directive.trim();
let split = split_first_char(directive).unwrap();
@ -28,6 +32,20 @@ pub fn run_day1_p1() -> Solution {
let amount = split.1.parse::<i32>().unwrap();
current_position = change_direction(&current_position, direction);
if current_position.facing == 'N' || current_position.facing == 'S' {
for point in current_position.y..amount {
let mut store_pt = String::new();
store_pt.push_str(&current_position.x.to_string());
store_pt.push_str(&point.to_string());
if visited_coords.contains_key(&store_pt) {
println!("{:?}", store_pt);
process::exit(1);
} else {
visited_coords.insert(store_pt, "visited".to_string());
}
}
}
match current_position.facing {
'N' => current_position.y = current_position.y + amount,
'S' => current_position.y = current_position.y - amount,
@ -35,8 +53,14 @@ pub fn run_day1_p1() -> Solution {
'W' => current_position.x = current_position.x - amount,
_ => current_position.x = current_position.x,
};
// log the cooordinates, check to see if has been visited.
println!("{:?}", current_position);
}
/*
* SOLUTION
*/
let answer = current_position.x + current_position.y.abs();
let solution = Solution {
day: 1,

135
2024/rust/Cargo.lock generated Normal file
View File

@ -0,0 +1,135 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent-of-code-2024"
version = "0.1.0"
dependencies = [
"tabled",
]
[[package]]
name = "bytecount"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "papergrid"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2b0f8def1f117e13c895f3eda65a7b5650688da29d6ad04635f61bc7b92eebd"
dependencies = [
"bytecount",
"fnv",
"unicode-width",
]
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "proc-macro-error2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802"
dependencies = [
"proc-macro-error-attr2",
"proc-macro2",
"quote",
"syn 2.0.90",
]
[[package]]
name = "proc-macro2"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
dependencies = [
"proc-macro2",
"unicode-ident",
]
[[package]]
name = "tabled"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6709222f3973137427ce50559cd564dc187a95b9cfe01613d2f4e93610e510a"
dependencies = [
"papergrid",
"tabled_derive",
]
[[package]]
name = "tabled_derive"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "931be476627d4c54070a1f3a9739ccbfec9b36b39815106a20cce2243bbcefe1"
dependencies = [
"heck",
"proc-macro-error2",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "unicode-width"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"

8
2024/rust/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "advent-of-code-2024"
version = "0.1.0"
edition = "2021"
authors = ["Dan Chadwick <dan@danchadwickdesign.com>"]
[dependencies]
tabled = "0.17.0"

1000
2024/rust/inputs/day1.txt Normal file

File diff suppressed because it is too large Load Diff

64
2024/rust/src/day1.rs Normal file
View File

@ -0,0 +1,64 @@
use crate::Solution;
use std::fs;
#[allow(dead_code)]
pub fn run_day_1_part_1() -> Solution {
let location_ids = fs::read_to_string("./inputs/day1.txt").expect("Unable to read file");
let location_lines = location_ids.split("\n");
let mut left_locations: Vec<i32> = vec!();
let mut right_locations: Vec<i32> = vec!();
for location_line in location_lines {
if location_line.len() < 1 {
continue;
}
let split_line = location_line.split(" ");
let collection: Vec<&str> = split_line.collect();
let left_int: i32 = collection[0].parse().unwrap();
let right_int: i32 = collection[1].parse().unwrap();
left_locations.push(left_int);
right_locations.push(right_int);
}
left_locations.sort();
right_locations.sort();
let llength = left_locations.len();
let rlength = right_locations.len();
if llength != rlength {
panic!("Vectors has unequal length");
}
let mut part_1_distance = 0;
let mut part_2_distance = 0;
for i in 0..1000 {
let mut diff = left_locations[i] - right_locations[i];
diff = diff.abs();
part_1_distance = part_1_distance + diff;
// for part 2, we need to check how many times left location appears in right
// then multiply the number, by the occurances count.
let lookup = right_locations.iter().filter(|&n| *n == left_locations[i]).count();
let value = left_locations[i] * lookup as i32;
part_2_distance = part_2_distance + value;
}
let mut answers = String::new();
let part_1 = "Part 1: ".to_string();
answers.push_str(&part_1);
answers.push_str(&part_1_distance.to_string());
let newline = "\n".to_string();
answers.push_str(&newline);
let part_2 = "Part 2: ".to_string();
answers.push_str(&part_2);
answers.push_str(&part_2_distance.to_string());
let solution = Solution {
day: 1,
answer: answers,
};
return solution;
}

19
2024/rust/src/main.rs Normal file
View File

@ -0,0 +1,19 @@
use tabled::{Table, Tabled};
pub mod day1;
#[derive(Tabled)]
pub struct Solution {
day: u64,
answer: String,
}
fn main() {
let mut rows = Vec::new();
let day1_p1 = day1::run_day_1_part_1();
rows.push(day1_p1);
let table = Table::new(rows);
print!("{}", table);
}