added 2024 part 2

This commit is contained in:
2025-06-05 22:47:13 +02:00
parent f1b41dc9c7
commit 7a88149ed9
38 changed files with 4600 additions and 0 deletions

38
2024/02/Kotlin/.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
2024/02/Kotlin/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

7
2024/02/Kotlin/.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/kotlin" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

6
2024/02/Kotlin/.idea/kotlinc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="2.0.0" />
</component>
</project>

14
2024/02/Kotlin/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
2024/02/Kotlin/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

88
2024/02/Kotlin/pom.xml Normal file
View File

@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.makhankov</groupId>
<artifactId>Kotlin</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>MainKt</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,64 @@
package ch.makhankov
import kotlin.math.absoluteValue
import kotlin.math.sign
fun main() {
val filenames = listOf("example.txt", "input.txt")
filenames.forEach { filename ->
val data = readFile(filename)
val dataRows = data?.split("\r\n")
val dataEntries = dataRows?.map { it.split(" ") }?.map { it.map { s -> s.toInt() } }
val result = dataEntries?.let { calculateSafeRecords(it) }
val resultDampened = dataEntries?.let { calculateSafeRecordsWithTolerance(it) }
println("Part 1: $result")
println("Part 2: $resultDampened")
}
}
fun readFile(filename: String): String? {
return object {}.javaClass.getResource("/$filename")?.readText()
}
fun diffFromList(list: List<Int>): List<Int> {
return list.dropLast(1).mapIndexed { index, i -> i - list[index + 1] }
}
fun sameSign(list: List<Int>): Boolean {
val temp = list[0]
return list.drop(1).all { it.sign == temp.sign }
}
fun inIncreaseRange(list: List<Int>, range: IntRange): Boolean {
return list.all { range.contains(it.absoluteValue) }
}
fun calculateSafeRecords(input: List<List<Int>>): Int {
val checkValidity = input.map { transformation(it) }
return checkValidity.count { it }
}
fun calculateSafeRecordsWithTolerance(input: List<List<Int>>): Int {
val checkValidity = input.map { transformation(it) }
var count = checkValidity.count { it }
checkValidity.forEachIndexed { index, value ->
if (!value) {
val record = input[index]
for ((rIndex, _) in record.withIndex()) {
val subList = record.filterIndexed { i, _ -> i != rIndex }
if (transformation(subList)) {
count = count.inc()
break
}
}
}
}
return count
}
fun transformation(input: List<Int>): Boolean{
val diffs = diffFromList(input)
val sameSign = sameSign(diffs)
val inRange = inIncreaseRange(diffs, 1..3)
return sameSign && inRange
}

View 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

File diff suppressed because it is too large Load Diff

1
2024/02/Rust/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
2024/02/Rust/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "day_2"
version = "0.1.0"

6
2024/02/Rust/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "day_2"
version = "0.1.0"
edition = "2021"
[dependencies]

View 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

1000
2024/02/Rust/src/input.txt Normal file

File diff suppressed because it is too large Load Diff

64
2024/02/Rust/src/main.rs Normal file
View File

@@ -0,0 +1,64 @@
use std::{fs, process};
fn main() {
let file_name = "./src/input.txt";
let reports: Vec<Vec<i32>>;
match fs::read_to_string(file_name) {
Ok(data) => {
reports = data
.split("\r\n")
.map(|report|
report
.split(" ")
.map(|level| level
.parse::<i32>().unwrap())
.collect())
.collect()
},
Err(error) => {
println!("Failed to read file {file_name}: {error}");
process::exit(1);
},
}
let levels_per_report = reports
.iter()
.map(|report| {
report
.windows(2)
.map(|slice| slice[0] - slice[1])
.collect::<Vec<i32>>()
})
.collect::<Vec<Vec<i32>>>();
let same_sign_reports = levels_per_report
.iter()
.map(|report| {
report
.iter()
.skip(1)
//.fold(init, f)
.all(|level| (*level >= 0) == (*report.first().unwrap() >= 0))
})
.collect::<Vec<bool>>();
let in_range_reports = levels_per_report
.iter()
.map(|report| {
report
.iter()
.all(|level| level.abs() >= 1 && level.abs() <= 3)
})
.collect::<Vec<bool>>();
let safe_reports = same_sign_reports
.iter()
.copied()
.zip(in_range_reports.iter().copied())
.map(|(l, r)| l && r)
.fold(0, |acc, report| acc + report as i32);
//.collect::<u32>();
//println!("Reports:\n{:?}", reports);
//println!("Levels Per Report:\n{:?}", levels_per_report);
//println!("Same Sign Reports:\n{:?}", same_sign_reports);
//println!("In Range Reports:\n{:?}", in_range_reports);
println!("Safe Reports:\n{:?}", safe_reports);
process::exit(0)
}