Compare commits
5 Commits
8ae35afa9f
...
9fe58d5490
Author | SHA1 | Date |
---|---|---|
|
9fe58d5490 | |
|
f543fd930d | |
|
858e44bf8a | |
|
6160b15a1f | |
|
160145a983 |
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,59 @@
|
||||||
|
use std::u64;
|
||||||
|
|
||||||
|
|
||||||
|
fn is_increasing(levels: &Vec<u64>) -> bool {
|
||||||
|
let mut current = 0;
|
||||||
|
for item in levels {
|
||||||
|
if *item < current {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
current = *item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_decreasing(levels: &Vec<u64>) -> bool {
|
||||||
|
let mut current = u64::MAX;
|
||||||
|
for item in levels {
|
||||||
|
if *item > current {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
current = *item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_monotonous(levels: &Vec<u64>) -> bool {
|
||||||
|
return is_increasing(levels) || is_decreasing(levels);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_safe(levels: &Vec<u64>) -> bool {
|
||||||
|
if !is_monotonous(levels) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for i in 0..(levels.len() - 1) {
|
||||||
|
let j = i+1;
|
||||||
|
let a = levels[i];
|
||||||
|
let b = levels[j];
|
||||||
|
let diff = (a as i64 - b as i64).abs() as u64;
|
||||||
|
if diff < 1 || diff > 3 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut count = 0;
|
||||||
|
for line in std::io::stdin().lines() {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let levels: Vec<u64> = line.split(" ").map(|x| x.parse::<u64>().unwrap()).collect();
|
||||||
|
if is_safe(&levels) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("{}", count);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
regex = "1.11.1"
|
|
@ -0,0 +1,14 @@
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut sum: u32 = 0;
|
||||||
|
let re = Regex::new(r"mul\(([0-9]*),([0-9]*)\)").unwrap();
|
||||||
|
let hay = std::io::read_to_string(std::io::stdin()).unwrap();
|
||||||
|
for (_, [num1, num2]) in re.captures_iter(&hay).map(|c| c.extract()) {
|
||||||
|
let num1: u32 = num1.parse().unwrap();
|
||||||
|
let num2: u32 = num2.parse().unwrap();
|
||||||
|
sum += num1 * num2;
|
||||||
|
}
|
||||||
|
println!("{}", sum);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
regex = "1.11.1"
|
|
@ -0,0 +1,26 @@
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut sum: u32 = 0;
|
||||||
|
let re1 = Regex::new(r"(do\(\)|don't\(\)|mul\([0-9]*,[0-9]*\))").unwrap();
|
||||||
|
let re2 = Regex::new(r"mul\(([0-9]*),([0-9]*)\)").unwrap();
|
||||||
|
let hay = std::io::read_to_string(std::io::stdin()).unwrap();
|
||||||
|
let mut i_do = true;
|
||||||
|
for (_, [does]) in re1.captures_iter(&hay).map(|c| c.extract()) {
|
||||||
|
if does == "do()" {
|
||||||
|
i_do = true;
|
||||||
|
}
|
||||||
|
if does == "don't()" {
|
||||||
|
i_do = false;
|
||||||
|
}
|
||||||
|
if i_do {
|
||||||
|
for (_, [num1, num2]) in re2.captures_iter(does).map(|c| c.extract()) {
|
||||||
|
let num1: u32 = num1.parse().unwrap();
|
||||||
|
let num2: u32 = num2.parse().unwrap();
|
||||||
|
sum += num1 * num2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("{}", sum);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,96 @@
|
||||||
|
fn count_xmas(hay: Vec<Vec<char>>) -> u64 {
|
||||||
|
let target: Vec<char> = vec!['X', 'M', 'A', 'S'];
|
||||||
|
let mut count: u64 = 0;
|
||||||
|
for i in 0..hay.len() {
|
||||||
|
for j in 0..hay[i].len() {
|
||||||
|
// Top to bottom
|
||||||
|
let mut needle: Vec<char> = Vec::new();
|
||||||
|
for k in 0..target.len() {
|
||||||
|
if i + k >= hay.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let letter = hay[i+k][j];
|
||||||
|
needle.push(letter);
|
||||||
|
}
|
||||||
|
if needle.len() == target.len() {
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
needle.reverse();
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Left to right
|
||||||
|
let mut needle: Vec<char> = Vec::new();
|
||||||
|
for k in 0..target.len() {
|
||||||
|
if j + k >= hay[i].len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let letter = hay[i][j+k];
|
||||||
|
needle.push(letter);
|
||||||
|
}
|
||||||
|
if needle.len() == target.len() {
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
needle.reverse();
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Main Diagonal
|
||||||
|
let mut needle: Vec<char> = Vec::new();
|
||||||
|
for k in 0..target.len() {
|
||||||
|
if j + k >= hay[i].len() || i + k >= hay.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let letter = hay[i+k][j+k];
|
||||||
|
needle.push(letter);
|
||||||
|
}
|
||||||
|
if needle.len() == target.len() {
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
needle.reverse();
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Secondary diagonal
|
||||||
|
let mut needle: Vec<char> = Vec::new();
|
||||||
|
for k in 0..target.len() {
|
||||||
|
if j < k || i + k >= hay.len() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let letter = hay[i+k][j-k];
|
||||||
|
needle.push(letter);
|
||||||
|
}
|
||||||
|
if needle.len() == target.len() {
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
needle.reverse();
|
||||||
|
if needle == target {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut hay: Vec<Vec<char>> = Vec::new();
|
||||||
|
for line in std::io::stdin().lines() {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let mut row: Vec<char> = Vec::new();
|
||||||
|
for chr in line.chars() {
|
||||||
|
row.push(chr);
|
||||||
|
}
|
||||||
|
hay.push(row);
|
||||||
|
}
|
||||||
|
let c = count_xmas(hay);
|
||||||
|
println!("{}", c);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,38 @@
|
||||||
|
fn count_xmas(hay: Vec<Vec<char>>) -> u64 {
|
||||||
|
let mut count: u64 = 0;
|
||||||
|
let target: Vec<char> = vec!['M', 'A', 'S'];
|
||||||
|
for center_i in 1..(hay.len() - 1) {
|
||||||
|
for center_j in 1..(hay.len() - 1) {
|
||||||
|
let mut diag1: Vec<char> = Vec::new();
|
||||||
|
let mut diag2: Vec<char> = Vec::new();
|
||||||
|
for shift in 0..3 {
|
||||||
|
let shift: isize = shift as isize - 1;
|
||||||
|
diag1.push(hay[(center_i as isize + shift) as usize][(center_j as isize - shift) as usize]);
|
||||||
|
diag2.push(hay[(center_i as isize + shift) as usize][(center_j as isize + shift) as usize]);
|
||||||
|
}
|
||||||
|
let mut diag1_rev = diag1.clone();
|
||||||
|
let mut diag2_rev = diag2.clone();
|
||||||
|
diag1_rev.reverse();
|
||||||
|
diag2_rev.reverse();
|
||||||
|
if (diag1_rev == target || diag1 == target) && (diag2_rev == target || diag2 == target) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let mut hay: Vec<Vec<char>> = Vec::new();
|
||||||
|
for line in std::io::stdin().lines() {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let mut row: Vec<char> = Vec::new();
|
||||||
|
for chr in line.chars() {
|
||||||
|
row.push(chr);
|
||||||
|
}
|
||||||
|
hay.push(row);
|
||||||
|
}
|
||||||
|
let c = count_xmas(hay);
|
||||||
|
println!("{}", c);
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue