Day2
This commit is contained in:
BIN
day2/c/day2
Executable file
BIN
day2/c/day2
Executable file
Binary file not shown.
80
day2/c/day2.c
Normal file
80
day2/c/day2.c
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define CHARS_MAX 64
|
||||||
|
#define MAX_LEVELS 16
|
||||||
|
#define abs(a) ((a) < 0 ? (-(a)) : (a))
|
||||||
|
|
||||||
|
int is_safe(int *levels, int level_count);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char *p, *buf, c;
|
||||||
|
|
||||||
|
buf = calloc(CHARS_MAX, sizeof(char));
|
||||||
|
p = buf;
|
||||||
|
int safes1 = 0, safes2 = 0;
|
||||||
|
|
||||||
|
int levels[MAX_LEVELS], level_count = 0;
|
||||||
|
while ((c = getchar()) != EOF) {
|
||||||
|
*p++ = c;
|
||||||
|
if (c != '\n') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
p = buf;
|
||||||
|
int level = 0;
|
||||||
|
|
||||||
|
p--;
|
||||||
|
do {
|
||||||
|
p++;
|
||||||
|
sscanf(p, "%i", &levels[level_count]);
|
||||||
|
level_count++;
|
||||||
|
while (*p != ' ' && *p != '\n') p++;
|
||||||
|
} while(*p != '\n');
|
||||||
|
|
||||||
|
if (is_safe(levels, level_count)) {
|
||||||
|
safes1++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int new_levels[MAX_LEVELS];
|
||||||
|
for (int i = 0; i < level_count; i++) {
|
||||||
|
memcpy(new_levels, levels, level_count * sizeof(levels[0]));
|
||||||
|
for (int j = i; j < level_count - 1; j++) {
|
||||||
|
new_levels[j] = levels[j+1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_safe(new_levels, level_count - 1)) {
|
||||||
|
safes2++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(buf, 0, CHARS_MAX);
|
||||||
|
p = buf;
|
||||||
|
level_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
printf("%i\n", safes1);
|
||||||
|
printf("%i\n", safes2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_safe(int *levels, int level_count) {
|
||||||
|
int diff = 0, safe = 1;
|
||||||
|
for (int i = 0; i < level_count; i++) {
|
||||||
|
if (i != 0) {
|
||||||
|
if (abs(levels[i] - levels[i-1]) < 1 || abs(levels[i] - levels[i-1]) > 3) {
|
||||||
|
safe = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((levels[i] - levels[i-1]) * diff < 0) {
|
||||||
|
safe = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
diff = levels[i] - levels[i-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return safe;
|
||||||
|
}
|
||||||
1000
day2/input.txt
Normal file
1000
day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
25
day2/rust/Cargo.lock
generated
Normal file
25
day2/rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
|
]
|
||||||
7
day2/rust/Cargo.toml
Normal file
7
day2/rust/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "rust"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
itertools = "0.13.0"
|
||||||
48
day2/rust/src/main.rs
Normal file
48
day2/rust/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let stdin = std::io::stdin();
|
||||||
|
let mut input = Vec::new();
|
||||||
|
|
||||||
|
for line in stdin.lines() {
|
||||||
|
let line = line.unwrap();
|
||||||
|
|
||||||
|
let levels: Vec<_> = line
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|e| e.parse::<isize>().unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
input.push(levels);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", input.iter().filter(|e| is_safe(e)).count());
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
input
|
||||||
|
.iter()
|
||||||
|
.map(|levels| {
|
||||||
|
levels.iter().combinations(levels.len() - 1).any(|e| {
|
||||||
|
let combination: Vec<_> = e.into_iter().copied().collect();
|
||||||
|
is_safe(&combination)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.filter(|&e| e)
|
||||||
|
.count()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_safe(levels: &Vec<isize>) -> bool {
|
||||||
|
if levels.len() < 2 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let direction = levels[1] - levels[0];
|
||||||
|
|
||||||
|
return levels
|
||||||
|
.iter()
|
||||||
|
.tuple_windows()
|
||||||
|
.filter(|&(a, b)| (b - a).abs() < 1 || (b - a).abs() > 3 || (b - a) * direction < 0)
|
||||||
|
.count()
|
||||||
|
== 0;
|
||||||
|
}
|
||||||
6
day2/sample.txt
Normal file
6
day2/sample.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
7 6 4 2 1
|
||||||
|
1 2 7 8 9
|
||||||
|
9 7 6 2 1
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
|
1 3 6 7 9
|
||||||
Reference in New Issue
Block a user