Day1
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
**/target
|
||||
BIN
day1/c/day1
Executable file
BIN
day1/c/day1
Executable file
Binary file not shown.
64
day1/c/day1.c
Normal file
64
day1/c/day1.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LINE_MAX 64
|
||||
#define LIST_MAX 1024
|
||||
#define abs(a) (a) < 0 ? -(a) : (a);
|
||||
|
||||
int cmp(const void*, const void*);
|
||||
|
||||
int main() {
|
||||
char *p, *buf, c;
|
||||
int left, right, *left_list, *right_list, i = 0;
|
||||
|
||||
buf = calloc(LINE_MAX, sizeof(char));
|
||||
p = buf;
|
||||
left_list = calloc(LIST_MAX, sizeof(*left_list));
|
||||
right_list = calloc(LIST_MAX, sizeof(*right_list));
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
*p = '\0';
|
||||
sscanf(buf, "%i %i", &left, &right);
|
||||
left_list[i] = left;
|
||||
right_list[i] = right;
|
||||
i++;
|
||||
memset(buf, 0, LINE_MAX);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
qsort(left_list, i, sizeof(*left_list), cmp);
|
||||
qsort(right_list, i, sizeof(*right_list), cmp);
|
||||
|
||||
int sum = 0;
|
||||
for (int j = 0; j < i; j++) {
|
||||
sum += abs(left_list[j] - right_list[j]);
|
||||
}
|
||||
|
||||
int similarity = 0;
|
||||
|
||||
for (int j = 0; j < i; j++) {
|
||||
int count = 0;
|
||||
for (int k = 0; k < i; k++) {
|
||||
if (left_list[j] == right_list[k]) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
similarity += left_list[j] * count;
|
||||
}
|
||||
|
||||
free(left_list);
|
||||
free(right_list);
|
||||
|
||||
printf("%i\n", sum);
|
||||
printf("%i\n", similarity);
|
||||
}
|
||||
|
||||
int cmp(const void* a, const void* b) {
|
||||
return *(int*)a - *(int*)b;
|
||||
}
|
||||
1000
day1/input.txt
Normal file
1000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
25
day1/rust/Cargo.lock
generated
Normal file
25
day1/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
day1/rust/Cargo.toml
Normal file
7
day1/rust/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.13.0"
|
||||
40
day1/rust/src/main.rs
Normal file
40
day1/rust/src/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
let stdin = std::io::stdin();
|
||||
|
||||
let mut left_list = Vec::new();
|
||||
let mut right_list = Vec::new();
|
||||
|
||||
for line in stdin.lines() {
|
||||
let line = line.unwrap();
|
||||
|
||||
let (left, right) = line
|
||||
.split_whitespace()
|
||||
.map(|e| e.parse::<isize>().unwrap())
|
||||
.collect_tuple()
|
||||
.unwrap();
|
||||
|
||||
left_list.push(left);
|
||||
right_list.push(right);
|
||||
}
|
||||
|
||||
left_list.sort();
|
||||
right_list.sort();
|
||||
|
||||
let distance: isize = left_list
|
||||
.iter()
|
||||
.zip(&right_list)
|
||||
.map(|(left, right)| left - right)
|
||||
.map(|e| e.abs())
|
||||
.sum();
|
||||
|
||||
println!("{}", distance);
|
||||
|
||||
let similarity: usize = left_list
|
||||
.iter()
|
||||
.map(|e| usize::try_from(*e).unwrap() * right_list.iter().filter(|n| *n == e).count())
|
||||
.sum();
|
||||
|
||||
println!("{}", similarity);
|
||||
}
|
||||
6
day1/sample.txt
Normal file
6
day1/sample.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
Reference in New Issue
Block a user