This commit is contained in:
dobiadi
2024-12-05 13:55:17 +01:00
parent e00e7079db
commit 38560fc3bc
7 changed files with 1637 additions and 0 deletions

BIN
day5/c/day5 Executable file
View File

Binary file not shown.

117
day5/c/day5.c Normal file
View File

@@ -0,0 +1,117 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define CHARS_MAX 512
#define MAX_RULES 2048
#define MAX_UPDATES 1024
#define MAX_PAGE_PER_UPDATE 1024
#define abs(a) ((a) < 0 ? (-(a)) : (a))
typedef struct rule {
int first;
int second;
} rule_t;
typedef struct update {
int* list;
int count;
} update_t;
typedef struct rule_list {
rule_t *rules;
int count;
} rule_list_t;
int cmp(const void*, const void*, void*);
int main() {
char *p, *buf, c;
buf = calloc(CHARS_MAX, sizeof(char));
p = buf;
int reading_rules = 1;
rule_t *rules = calloc(MAX_RULES, sizeof(rules[0]));
update_t *updates = calloc(MAX_UPDATES, sizeof(updates[0]));
int rule_count = 0, update_count = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c != '\n') {
continue;
}
if (buf[0] == '\n') {
reading_rules = 0;
} else if (reading_rules) {
sscanf(buf, "%i|%i", &rules[rule_count].first, &rules[rule_count].second);
rule_count++;
} else {
p = buf;
updates[update_count].list = calloc(MAX_PAGE_PER_UPDATE, sizeof(updates[0].list[0]));
do {
sscanf(p, "%i", &updates[update_count].list[updates[update_count].count]);
updates[update_count].count++;
// Find next , or \n
while (*p != ',' && *p != '\n') p++;
p++;
} while(*(p-1) != '\n');
update_count++;
}
memset(buf, 0, CHARS_MAX);
p = buf;
}
int count = 0;
int count2 = 0;
rule_list_t rule_list = {
.rules = rules,
.count = rule_count
};
#pragma omp parallel for
for (int i = 0; i < update_count; i++) {
int ordered = 1;
int *sorted = calloc(updates[i].count, updates[0].list[0]);
memcpy(sorted, updates[i].list, sizeof(updates[0].list[0]) * updates[i].count);
qsort_r(sorted, updates[i].count, sizeof(updates[0].list[0]), &cmp, &rule_list);
if (memcmp(sorted, updates[i].list, sizeof(updates[0].list[0]) * updates[i].count) == 0)
#pragma omp critical
{
count += updates[i].list[updates[i].count / 2];
}
else
#pragma omp critical
{
count2 += sorted[updates[i].count / 2];
}
}
free(buf);
free(rules);
for (int i = 0; i < update_count; i++) {
free(updates[i].list);
}
free(updates);
printf("%i\n", count);
printf("%i\n", count2);
}
int cmp(const void* a, const void* b, void *rulesp) {
rule_list_t rule_list = *(rule_list_t*)rulesp;
for (int i = 0; i < rule_list.count; i++) {
if (rule_list.rules[i].second == *(int*)a && rule_list.rules[i].first == *(int*)b) {
return 1;
}
if (rule_list.rules[i].first == *(int*)a && rule_list.rules[i].second == *(int*)b) {
return -1;
}
}
return 0;
}

1386
day5/input.txt Normal file
View File

File diff suppressed because it is too large Load Diff

25
day5/rust/Cargo.lock generated Normal file
View 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
day5/rust/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
[dependencies]
itertools = "0.13.0"

74
day5/rust/src/main.rs Normal file
View File

@@ -0,0 +1,74 @@
use std::{cmp::Ordering, io::Read};
use itertools::Itertools;
struct Rule {
first: isize,
second: isize,
}
fn main() {
let mut stdin = std::io::stdin();
let mut input = String::new();
stdin.read_to_string(&mut input).unwrap();
let (rules_string, updates_string) = input.split("\n\n").collect_tuple().unwrap();
let rules: Vec<_> = rules_string
.split("\n")
.filter(|e| e.len() > 0)
.map(|e| {
e.split("|")
.map(|e| e.parse().unwrap())
.collect_tuple::<(isize, isize)>()
.unwrap()
})
.map(|e| Rule {
first: e.0,
second: e.1,
})
.collect();
let updates: Vec<_> = updates_string
.split("\n")
.filter(|e| e.len() > 0)
.map(|e| {
e.split(",")
.map(|e| e.parse().unwrap())
.collect::<Vec<isize>>()
})
.collect();
let mut count = 0;
let mut count2 = 0;
updates.iter().for_each(|update| {
let mut sorted = update.clone();
sorted.sort_by(|&a, &b| {
if let Some(_) = rules
.iter()
.find(|rule| rule.first == a && rule.second == b)
{
return Ordering::Less;
}
if let Some(_) = rules
.iter()
.find(|rule| rule.second == a && rule.first == b)
{
return Ordering::Greater;
}
return Ordering::Equal;
});
if sorted == *update {
count += update[update.len() / 2];
} else {
count2 += sorted[update.len() / 2];
}
});
println!("{}", count);
println!("{}", count2);
}

28
day5/sample.txt Normal file
View File

@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47