day 1 part 1

This commit is contained in:
endernon 2024-12-01 19:47:57 +00:00
commit 0c9f8224ec
4 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,23 @@
import json
file = open("list.txt","r")
l1 = []
l2 = []
for i in file:
a,b = i.strip().split(" ")
a = int(a)
b = int(b)
l1.append(a)
l2.append(b)
file.close()
l1.sort()
l2.sort()
with open('l1.json', 'w') as l1file:
json.dump(l1,l1file)
l1file.close()
with open('l2.json', 'w') as l2file:
json.dump(l2,l2file)
l2file.close()

102
2024/day1/part1/step2/Cargo.lock generated Normal file
View file

@ -0,0 +1,102 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "0.1.0"
dependencies = [
"json",
"serde_json",
]
[[package]]
name = "itoa"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "proc-macro2"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "serde"
version = "1.0.215"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.215"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"

View file

@ -0,0 +1,8 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
[dependencies]
json = "0.12.4"
serde_json = "1.0.133"

View file

@ -0,0 +1,27 @@
fn main() {
let mut list1: Vec<i32> = serde_json::from_reader(std::fs::File::open("l1.json").unwrap()).unwrap();
let mut list2: Vec<i32> = serde_json::from_reader(std::fs::File::open("l2.json").unwrap()).unwrap();
list1.sort();
list2.sort();
if list1.len() != list2.len() {
panic!("your lists are fucked")
}
let mut totaldiff: i64 = 0;
for i in 0..list1.len() {
let int1 = list1[i];
let int2 = list2[i];
if int1 > int2 {
totaldiff += (int1 - int2) as i64;
}
else if int1 < int2 {
totaldiff += (int2 - int1) as i64;
}
}
println!("{totaldiff}")
}