added 2024 part 2
This commit is contained in:
6
2024/01/Go/example.txt
Normal file
6
2024/01/Go/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
3
2024/01/Go/go.mod
Normal file
3
2024/01/Go/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch/timofey
|
||||
|
||||
go 1.23.4
|
||||
1000
2024/01/Go/input.txt
Normal file
1000
2024/01/Go/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
51
2024/01/Go/main.go
Normal file
51
2024/01/Go/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func sumArray(list []int) int {
|
||||
result := 0
|
||||
for _, item := range list{
|
||||
result += item
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
data, err := os.ReadFile("./input.txt")
|
||||
check(err)
|
||||
//fmt.Println(string(data))
|
||||
dataRows := strings.Split(string(data), "\r\n")
|
||||
var leftList []int
|
||||
var rightList []int
|
||||
var diffList []int
|
||||
//fmt.Println(dataRows)
|
||||
for _, row := range dataRows {
|
||||
l, _ := strconv.Atoi(strings.Split(row, " ")[0])
|
||||
r, _ := strconv.Atoi(strings.Split(row, " ")[1])
|
||||
leftList = append(leftList, l)
|
||||
rightList = append(rightList, r)
|
||||
}
|
||||
//fmt.Println(rightList)
|
||||
slices.Sort(leftList)
|
||||
slices.Sort(rightList)
|
||||
//fmt.Println(leftList)
|
||||
//fmt.Println(rightList)
|
||||
for idx, l := range leftList {
|
||||
diffList = append(diffList, int(math.Abs(float64(rightList[idx] - l))))
|
||||
}
|
||||
//fmt.Println(diffList)
|
||||
fmt.Println(sumArray(diffList))
|
||||
}
|
||||
1
2024/01/Rust/.gitignore
vendored
Normal file
1
2024/01/Rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
2024/01/Rust/Cargo.lock
generated
Normal file
7
2024/01/Rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day_1"
|
||||
version = "0.1.0"
|
||||
6
2024/01/Rust/Cargo.toml
Normal file
6
2024/01/Rust/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day_1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
6
2024/01/Rust/src/example.txt
Normal file
6
2024/01/Rust/src/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
1000
2024/01/Rust/src/input.txt
Normal file
1000
2024/01/Rust/src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
42
2024/01/Rust/src/main.rs
Normal file
42
2024/01/Rust/src/main.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
fn main() {
|
||||
let file_name = "./src/example.txt";
|
||||
let input = fs::read_to_string(file_name)
|
||||
.expect("File was not able to be read");
|
||||
let input_list:Vec<&str> = input.split("\r\n").collect();
|
||||
let mut left_list: Vec<u32> = Vec::new();
|
||||
let mut right_list: Vec<u32> = Vec::new();
|
||||
input_list.into_iter().for_each(|row|{
|
||||
let row_list:Vec<&str> = row.split(" ").collect();
|
||||
left_list.push(row_list[0].parse().expect("Error Number conversion"));
|
||||
right_list.push(row_list[1].parse().expect("Error Number conversion"));
|
||||
});
|
||||
left_list.sort();
|
||||
right_list.sort();
|
||||
let frequency_list = right_list
|
||||
.iter()
|
||||
.copied()
|
||||
.fold(HashMap::new(), |mut map, val|{
|
||||
map.entry(val)
|
||||
.and_modify(|frq| *frq+=1)
|
||||
.or_insert(1);
|
||||
map
|
||||
});
|
||||
let result = left_list
|
||||
.iter()
|
||||
.zip(right_list.iter())
|
||||
.map(|(l, r)| i32::abs(*r as i32 - *l as i32))
|
||||
//.collect::<Vec<i32>>();
|
||||
.sum::<i32>();
|
||||
let result_2 = left_list
|
||||
.iter()
|
||||
.map(|v| v * frequency_list.get(v).unwrap_or(&0))
|
||||
//.collect::<Vec<u32>>();
|
||||
.sum::<u32>();
|
||||
println!("Content:\n{:#?}", left_list);
|
||||
//println!("Content:\n{:#?}", right_list);
|
||||
println!("Content:\n{:#?}", frequency_list.clone());
|
||||
println!("Content:\n{:#?}", result);
|
||||
println!("Content:\n{:#?}", result_2);
|
||||
}
|
||||
Reference in New Issue
Block a user