first 2 days

main
Midnight 1 year ago
commit 419206c9bd

1
.gitignore vendored

@ -0,0 +1 @@
/target

7
Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rustadventofcode"
version = "0.1.0"

@ -0,0 +1,8 @@
[package]
name = "rustadventofcode"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,48 @@
use std::fs;
pub fn step1() -> i32 {
let data = collect_data();
let elfdata = organise_data(data);
let highest = return_highest(elfdata);
return highest;
}
pub fn step2() -> i32 {
let data = collect_data();
let mut elfdata = organise_data(data);
elfdata.sort_by(|a, b| b.cmp(a));
let total = add_together(vec![elfdata[0], elfdata[1], elfdata[2]]);
return total;
}
fn add_together(data: Vec<i32>) -> i32 {
return data.iter().sum();
}
fn collect_data() -> Vec<String> {
let data = fs::read_to_string("src/day1/input.txt").expect("Unable to read file");
let eachline: Vec<String> = data.split("\n").map(str::to_string).collect();
return eachline;
}
fn organise_data(data: Vec<String>) -> Vec<i32> {
let mut elfscores: Vec<i32> = Vec::new();
let mut amount: i32 = 0;
for line in data {
if line.len() < 1 {
if amount > 0 {
elfscores.push(amount);
amount = 0;
}
continue;
}
amount += line.parse::<i32>().unwrap();
}
return elfscores;
}
fn return_highest(data: Vec<i32>) -> i32 {
return *data.iter().max().unwrap();
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,81 @@
use std::fs;
fn collect_data() -> Vec<String> {
let data = fs::read_to_string("src/day2/input.txt").expect("Unable to read file");
let eachline: Vec<String> = data.split("\n").map(str::to_string).collect();
return eachline;
}
fn get_points_for_match(mat: &str) -> i32 {
let items: Vec<String> = mat.split(" ").map(str::to_string).collect();
let mut points = 0;
if items[1] == "X" {
points += 1;
} else if items[1] == "Y" {
points += 2;
} else if items[1] == "Z" {
points += 3;
}
if (items[0] == "C" && items[1] == "X")
|| (items[0] == "B" && items[1] == "Z")
|| (items[0] == "A" && items[1] == "Y")
{
points += 6
} else if (items[0] == "A" && items[1] == "Z")
|| (items[0] == "C" && items[1] == "Y")
|| (items[0] == "B" && items[1] == "X")
{
points += 0;
} else {
points += 3;
}
return points;
}
fn get_points_for_match_2(mat: &str) -> i32 {
let items: Vec<String> = mat.split(" ").map(str::to_string).collect();
let mut points = 0;
if items[1] == "Y" {
points += 3
} else if items[1] == "Z" {
points += 6;
}
if items[0] == "A" {
points += 2;
} else if items[0] == "B" {
points += 3;
} else if items[0] == "C" {
points += 1;
}
return points;
}
pub fn step1() -> i32 {
let items = collect_data();
let mut total = 0;
for item in items {
total += get_points_for_match(&item);
}
return total;
}
pub fn step2() -> i32 {
let items = collect_data();
let mut total = 0;
for item in items {
total += get_points_for_match_2(&item);
}
return total;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,13 @@
mod day1;
mod day2;
fn main() {
println!(
"The answer to day 1 task 1 is: {}",
day1::step1().to_string()
);
println!("The answer to day 1 task 2 is: {}", day1::step2());
println!("The answer to day 2 task 1 is: {}", day2::step1());
println!("The answer to day 2 task 2 is: {}", day2::step2());
}
Loading…
Cancel
Save