Compare commits
2 Commits
2e74bf7d37
...
7f0166c880
Author | SHA1 | Date |
---|---|---|
Thom Dickson | 7f0166c880 | |
Thom Dickson | 5e8e0ae3ef |
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,36 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let file = fs::read_to_string("day01.in").unwrap();
|
||||
|
||||
let mut nums: Vec<u32> = vec![];
|
||||
for line in file.split('\n') {
|
||||
if line != "" {
|
||||
nums.push(u32::from_str_radix(line, 10).unwrap());
|
||||
}
|
||||
}
|
||||
let count_a = count_inc(&nums);
|
||||
|
||||
let mut nums2: Vec<u32> = vec![];
|
||||
for (i, _) in nums.iter().enumerate() {
|
||||
if i + 2 < nums.len() {
|
||||
nums2.push(nums[i..i + 3].iter().sum());
|
||||
}
|
||||
}
|
||||
let count_b = count_inc(&nums2);
|
||||
|
||||
println!("Part 1: {}", count_a);
|
||||
println!("Part 2: {}", count_b);
|
||||
}
|
||||
|
||||
fn count_inc(input: &Vec<u32>) -> usize {
|
||||
let mut count = 0;
|
||||
let mut prev: u32 = 0;
|
||||
for num in input.iter() {
|
||||
if num > &prev && prev != 0 {
|
||||
count += 1;
|
||||
}
|
||||
prev = *num;
|
||||
}
|
||||
count
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,54 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let file = fs::read_to_string("day02.in").unwrap();
|
||||
println!("=== PART 1 ===");
|
||||
part1(&file);
|
||||
println!("=== PART 2 ===");
|
||||
part2(&file);
|
||||
}
|
||||
|
||||
fn part1(file: &String) {
|
||||
let mut pos = vec![0, 0];
|
||||
|
||||
let direction_key = |x| match x {
|
||||
"forward" => (1, 0),
|
||||
"down" => (0, 1),
|
||||
"up" => (0, -1),
|
||||
_ => (0, 0),
|
||||
};
|
||||
|
||||
for line in file
|
||||
.strip_suffix("\n")
|
||||
.unwrap()
|
||||
.split('\n')
|
||||
.map(|x| x.split(" ").collect::<Vec<&str>>())
|
||||
{
|
||||
pos[0] += direction_key(line[0]).0 * i32::from_str_radix(line[1], 10).unwrap();
|
||||
pos[1] += direction_key(line[0]).1 * i32::from_str_radix(line[1], 10).unwrap();
|
||||
}
|
||||
println!("Current position: {:?}", pos);
|
||||
println!("Product: {:?}", pos.iter().product::<i32>());
|
||||
}
|
||||
|
||||
fn part2(file: &String) {
|
||||
let mut pos = vec![0, 0, 0];
|
||||
|
||||
for line in file
|
||||
.strip_suffix("\n")
|
||||
.unwrap()
|
||||
.split('\n')
|
||||
.map(|x| x.split(" ").collect::<Vec<&str>>())
|
||||
{
|
||||
if line[0] == "down" {
|
||||
pos[2] += i32::from_str_radix(line[1], 10).unwrap();
|
||||
} else if line[0] == "up" {
|
||||
pos[2] -= i32::from_str_radix(line[1], 10).unwrap();
|
||||
} else if line[0] == "forward" {
|
||||
pos[0] += i32::from_str_radix(line[1], 10).unwrap();
|
||||
pos[1] += pos[2] * i32::from_str_radix(line[1], 10).unwrap();
|
||||
}
|
||||
}
|
||||
println!("Current position: {:?}", pos);
|
||||
println!("Product: {:?}", pos[0] * pos[1]);
|
||||
}
|
Loading…
Reference in New Issue