Add day 5, 6, 7
This commit is contained in:
1
2023/day5/day5/.gitignore
vendored
Normal file
1
2023/day5/day5/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
7
2023/day5/day5/Cargo.lock
generated
Normal file
7
2023/day5/day5/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
8
2023/day5/day5/Cargo.toml
Normal file
8
2023/day5/day5/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
249
2023/day5/day5/input.txt
Normal file
249
2023/day5/day5/input.txt
Normal file
@@ -0,0 +1,249 @@
|
||||
seeds: 763445965 78570222 1693788857 146680070 1157620425 535920936 3187993807 180072493 1047354752 20193861 2130924847 274042257 20816377 596708258 950268560 11451287 3503767450 182465951 3760349291 265669041
|
||||
|
||||
seed-to-soil map:
|
||||
0 1894195346 315486903
|
||||
1184603419 2977305241 40929361
|
||||
1225532780 597717 4698739
|
||||
1988113706 1603988885 78481073
|
||||
679195301 529385087 505408118
|
||||
1781158512 2285166785 39457705
|
||||
352613463 2324624490 326581838
|
||||
1820616217 1738931330 104130014
|
||||
2066594779 2671974456 78036460
|
||||
1288754536 1682469958 56461372
|
||||
1371340411 3442489267 409818101
|
||||
3341036988 1092718505 511270380
|
||||
315486903 1857068786 37126560
|
||||
1924746231 2209682249 49360033
|
||||
1345215908 2259042282 26124503
|
||||
2917167497 2651206328 20768128
|
||||
1230231519 1034793205 57925300
|
||||
2144631239 3421335965 21153302
|
||||
2689873172 2750010916 227294325
|
||||
1974106264 1843061344 14007442
|
||||
2165784541 5296456 524088631
|
||||
1288156819 0 597717
|
||||
2937935625 3018234602 403101363
|
||||
|
||||
soil-to-fertilizer map:
|
||||
3849355959 3101885274 170573538
|
||||
3006688391 3025445580 66663107
|
||||
1656285214 0 92188755
|
||||
2361256790 4033128077 180329908
|
||||
2073349245 3676064680 287907545
|
||||
3458323699 4213457985 81509311
|
||||
2607585697 2380345050 99923790
|
||||
2541586698 3332501375 61727497
|
||||
3789313396 3272458812 60042563
|
||||
1858319884 917612384 162600389
|
||||
404885474 510011966 407600418
|
||||
4019929497 3603127472 72937208
|
||||
812485892 1190058688 253881561
|
||||
2603314195 2073349245 4271502
|
||||
1469824760 92188755 186460454
|
||||
0 278649209 231362757
|
||||
1249323571 1443940249 220501189
|
||||
4285190709 3092108687 9776587
|
||||
1066367453 1817839802 182956118
|
||||
3257369783 2724171240 184379320
|
||||
2707509487 2480268840 111317034
|
||||
2818826521 2192483180 187861870
|
||||
1748473969 1080212773 109845915
|
||||
3441749103 3394228872 16574596
|
||||
3142507350 2077620747 114862433
|
||||
3539833010 2908550560 116895020
|
||||
231362757 1664441438 153398364
|
||||
384761121 2000795920 20124353
|
||||
3656728030 2591585874 132585366
|
||||
3073351498 3963972225 69155852
|
||||
4092866705 3410803468 192324004
|
||||
|
||||
fertilizer-to-water map:
|
||||
2289944373 962149121 35670423
|
||||
3187295333 3295892606 34862460
|
||||
3798694276 3187295333 48036047
|
||||
3633796172 3644673845 164898104
|
||||
3222157793 3909959418 311250910
|
||||
922447685 2248438960 142418952
|
||||
648510907 2390857912 2187794
|
||||
3533408703 3809571949 100387469
|
||||
650698701 1840321280 271748984
|
||||
464371310 553340958 30176545
|
||||
3907291549 3330755066 313918779
|
||||
2791296727 461310335 25110510
|
||||
397451197 486420845 66920113
|
||||
3846730323 3235331380 60561226
|
||||
2575399801 2393045706 96151163
|
||||
1064866637 583517503 106955439
|
||||
1308190772 822897256 139251865
|
||||
0 236670371 224639964
|
||||
2671550964 0 119745763
|
||||
1171822076 2112070264 136368696
|
||||
224639964 690472942 132424314
|
||||
494547855 2489196869 153963052
|
||||
2498862112 160132682 76537689
|
||||
1447442637 997819544 842501736
|
||||
357064278 119745763 40386919
|
||||
2325614796 2643159921 173247316
|
||||
|
||||
water-to-light map:
|
||||
3181220930 3836164803 159006567
|
||||
1405924027 2473049234 39930303
|
||||
1275636734 1411161599 10830099
|
||||
3831476263 1580605379 266491658
|
||||
1570444205 1294782382 116379217
|
||||
3133505036 2271845049 47715894
|
||||
1305612481 2512979537 100311546
|
||||
1286466833 1275636734 19145648
|
||||
167024508 145118817 64336111
|
||||
92584517 615923894 40913901
|
||||
2227655152 2098041405 130620812
|
||||
2181794752 2613291083 34326669
|
||||
433451288 209454928 64553504
|
||||
4223684323 2228662217 43182832
|
||||
3527642272 3532330812 303833991
|
||||
2855274869 2647617752 119788915
|
||||
4266867155 2319560943 28100141
|
||||
231360619 1167459710 18705935
|
||||
2136463524 3995171370 45331228
|
||||
2978600831 1421991698 154904205
|
||||
4097967921 2825698355 78160170
|
||||
0 489813287 92584517
|
||||
1686823422 3023927616 328136942
|
||||
2014960364 2903858525 116532044
|
||||
1512152517 2767406667 58291688
|
||||
395185371 1129193793 38265917
|
||||
498004792 274008432 215804855
|
||||
2975063784 3020390569 3537047
|
||||
3340227497 3352064558 180266254
|
||||
3524203227 4040502598 3439045
|
||||
713809647 656837795 472355998
|
||||
1445854330 2395217316 66298187
|
||||
3520493751 1576895903 3709476
|
||||
4176128091 2347661084 47556232
|
||||
2216121421 2461515503 11533731
|
||||
2604330501 1847097037 250944368
|
||||
2358275964 4043941643 246054537
|
||||
133498418 582397804 33526090
|
||||
2131492408 4289996180 4971116
|
||||
250066554 0 145118817
|
||||
|
||||
light-to-temperature map:
|
||||
1212130960 1395832798 58636484
|
||||
542019293 812004214 195851408
|
||||
1706861165 2688952641 3198757
|
||||
3097592274 3018951608 116723898
|
||||
1148165381 3135675506 2936711
|
||||
938881022 300892136 26815610
|
||||
2884354982 3318488806 103553821
|
||||
789335095 421331130 149545927
|
||||
3270968199 1036156523 69989778
|
||||
1142047574 1030038716 6117807
|
||||
1710059922 1367448764 28384034
|
||||
3961474833 1257923390 109525374
|
||||
300892136 570877057 241127157
|
||||
2564647329 3422042627 200129932
|
||||
3534803741 2001828127 16273450
|
||||
1738443956 1454469282 486329977
|
||||
3717055162 2018101577 244419671
|
||||
4071000207 2781125469 223967089
|
||||
1030038716 3005092558 13859050
|
||||
737870701 358179343 20992797
|
||||
3388483069 4209077325 84901810
|
||||
3551077191 3622172559 62988195
|
||||
3614065386 2769635501 11489968
|
||||
3215304333 3262824940 55663866
|
||||
2278270544 2395185346 286376785
|
||||
1151102092 1940799259 61028868
|
||||
3625555354 4044566844 91499808
|
||||
2987908803 1106146301 109683471
|
||||
1134657064 2681562131 7390510
|
||||
2236365744 2353280546 41904800
|
||||
1270767444 3685160754 311880998
|
||||
1043897766 2262521248 90759298
|
||||
2806870879 2692151398 77484103
|
||||
3340957977 3997041752 47525092
|
||||
3214316172 4293979135 988161
|
||||
2764777261 1215829772 42093618
|
||||
965696632 379172140 42158990
|
||||
3473384879 4147658463 61418862
|
||||
2224773933 4136066652 11591811
|
||||
1582648442 3201806221 61018719
|
||||
758863498 327707746 30471597
|
||||
1643667161 3138612217 63194004
|
||||
|
||||
temperature-to-humidity map:
|
||||
3893671309 2897869925 193525827
|
||||
1232495182 999263760 191357681
|
||||
2440090529 1477966061 163000786
|
||||
3554817869 2745268041 3258240
|
||||
2845194314 3091395752 30017739
|
||||
4087197136 1397536284 33207741
|
||||
131252236 827398049 11627380
|
||||
2183328852 2476037434 27127608
|
||||
2603091315 2503165042 242102999
|
||||
2875212053 1778930312 348370034
|
||||
1427151141 891672290 77843721
|
||||
1642958327 3928570372 200739395
|
||||
3610489285 3219006125 283182024
|
||||
3277129360 3580618829 128344865
|
||||
2233175686 1190621441 206914843
|
||||
891672290 4129309767 165657529
|
||||
0 205218291 35419402
|
||||
3584280491 3551111757 26208794
|
||||
4247745260 1430744025 47222036
|
||||
2210456460 3502188149 21171921
|
||||
1843697722 2136406304 209010485
|
||||
1423852863 3577320551 3298278
|
||||
348097907 336470527 490927522
|
||||
1057329819 3708963694 175165363
|
||||
2231628381 3549564452 1547305
|
||||
3268023402 2127300346 9105958
|
||||
142879616 0 205218291
|
||||
3558076109 3523360070 26204382
|
||||
2052708207 2345416789 130620645
|
||||
4120404877 969516011 29747749
|
||||
35419402 240637693 95832834
|
||||
4150152626 3121413491 97592634
|
||||
3223582087 3884129057 44441315
|
||||
1504994862 1640966847 137963465
|
||||
3405474225 2748526281 149343644
|
||||
|
||||
humidity-to-location map:
|
||||
3554305993 3441523152 94289982
|
||||
4052786034 3015714705 242181262
|
||||
491965155 895492593 72049890
|
||||
2437667299 3726944956 38441480
|
||||
0 387558480 141314689
|
||||
3811971022 3765386436 101972362
|
||||
3648595975 3535813134 51583790
|
||||
1318420824 1500487572 135197760
|
||||
230758172 30039204 163596268
|
||||
3097413876 4221249785 73717511
|
||||
3041068351 3257895967 56345525
|
||||
1454254795 641271247 181430537
|
||||
394354440 1139828795 97610715
|
||||
3215016925 2618366793 339289068
|
||||
827848339 193635472 113349295
|
||||
150184459 306984767 80573713
|
||||
1778801395 2099048921 519317872
|
||||
1176891800 1379491771 111489820
|
||||
1288381620 0 30039204
|
||||
3171131387 3891749067 43885538
|
||||
564015045 528873169 112398078
|
||||
3913943384 3935634605 11560990
|
||||
2767014161 3947195595 274054190
|
||||
941197634 822701784 72790809
|
||||
1453618584 1499851361 636211
|
||||
2298119267 3587396924 139548032
|
||||
3700179765 3867358798 24390269
|
||||
3742730647 1778801395 11181531
|
||||
1013988443 967542483 20851096
|
||||
141314689 1490981591 8869770
|
||||
3925504374 3314241492 127281660
|
||||
2476108779 2088802749 10246172
|
||||
676413123 988393579 151435216
|
||||
3753912178 2957655861 58058844
|
||||
2486354951 1789982926 280659210
|
||||
3724570034 2070642136 18160613
|
||||
1034839539 1237439510 142052261
|
110
2023/day5/day5/script.py
Normal file
110
2023/day5/day5/script.py
Normal file
@@ -0,0 +1,110 @@
|
||||
from collections import defaultdict
|
||||
from multiprocessing import Pool
|
||||
from dataclasses import dataclass
|
||||
|
||||
def parse_file(data):
|
||||
d = defaultdict(dict)
|
||||
targets = []
|
||||
|
||||
for section in data.split("\n\n")[1:]:
|
||||
lines = section.split("\n")
|
||||
|
||||
parts = lines[0].split("-")
|
||||
source = parts[0]
|
||||
target = parts[2].split(" ")[0]
|
||||
|
||||
d[source][target] = []
|
||||
targets.append(target)
|
||||
|
||||
for line in lines[1:]:
|
||||
target_start, src_start, length = map(int, line.split(" "))
|
||||
d[source][target].append((target_start, src_start, length))
|
||||
return (d, targets)
|
||||
|
||||
def split_seeds(data):
|
||||
seeds = list(map(int, data.split("\n")[0].split(": ")[1].split(" ")))
|
||||
return seeds
|
||||
|
||||
def find_minimum(data_dict, targets, data):
|
||||
seeds = split_seeds(data)
|
||||
min_location = 9000000000 # Integer max size
|
||||
for seed in seeds:
|
||||
current = "seed"
|
||||
value = seed
|
||||
|
||||
for trgt in targets:
|
||||
for trg_start, src_start, length in data_dict[current][trgt]:
|
||||
if src_start <= value and value < src_start + length:
|
||||
value = trg_start + value - src_start
|
||||
break
|
||||
|
||||
current = trgt
|
||||
|
||||
min_location = min(min_location, value)
|
||||
return min_location
|
||||
|
||||
def part2(data):
|
||||
global seed_ranges
|
||||
global maps
|
||||
|
||||
plain_seeds = split_seeds(data)
|
||||
seed_ranges = []
|
||||
for i in range(0, len(plain_seeds), 2):
|
||||
seed_ranges.append(RangePart2(plain_seeds[i], plain_seeds[i + 1]))
|
||||
|
||||
maps = []
|
||||
|
||||
for section in data.split("\n\n")[1:]:
|
||||
lines = section.split("\n")
|
||||
|
||||
m = []
|
||||
for line in lines[1:]:
|
||||
trg_start, src_start, length = map(int, line.split(" "))
|
||||
m.append((RangePart2(trg_start, length), src_start))
|
||||
|
||||
maps.append(m)
|
||||
|
||||
maps = maps[::-1]
|
||||
|
||||
with Pool(23) as pool:
|
||||
for location in pool.imap(check_part2, range(int(1e9)), int(1e6)):
|
||||
if location is not None:
|
||||
print(location)
|
||||
return
|
||||
|
||||
|
||||
@dataclass
|
||||
class RangePart2:
|
||||
start: int
|
||||
length: int
|
||||
|
||||
def contains(self, value):
|
||||
return self.start <= value and value < self.start + self.length
|
||||
|
||||
|
||||
def check_part2(location):
|
||||
global seed_ranges
|
||||
global maps
|
||||
|
||||
value = location
|
||||
for m in maps:
|
||||
for trg_range, src_start in m:
|
||||
if trg_range.contains(value):
|
||||
value = src_start + (value - trg_range.start)
|
||||
break
|
||||
|
||||
for sr in seed_ranges:
|
||||
if sr.contains(value):
|
||||
return location
|
||||
|
||||
return None
|
||||
|
||||
|
||||
|
||||
f = open("input.txt", "r")
|
||||
data = f.read().strip()
|
||||
|
||||
data_dict, targets = parse_file(data)
|
||||
print("Part 1:", find_minimum(data_dict, targets, data))
|
||||
print("Part 2:")
|
||||
part2(data)
|
3
2023/day5/day5/src/main.rs
Normal file
3
2023/day5/day5/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
1
2023/day6/day6/.gitignore
vendored
Normal file
1
2023/day6/day6/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
54
2023/day6/day6/Cargo.lock
generated
Normal file
54
2023/day6/day6/Cargo.lock
generated
Normal file
@@ -0,0 +1,54 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day6"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
9
2023/day6/day6/Cargo.toml
Normal file
9
2023/day6/day6/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day6"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1.10.2"
|
3
2023/day6/day6/input.txt
Normal file
3
2023/day6/day6/input.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
Time: 63 78 94 68
|
||||
Distance: 411 1274 2047 1035
|
||||
|
43
2023/day6/day6/src/main.rs
Normal file
43
2023/day6/day6/src/main.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
// Open the file
|
||||
let file = File::open("input.txt").expect("Failed to open file");
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut lines = reader.lines().map(|l| l.unwrap());
|
||||
|
||||
let line1 = lines.next().unwrap();
|
||||
let line2 = lines.next().unwrap();
|
||||
|
||||
let times: Vec<i64> = line1.split(' ').filter_map(|s| s.parse().ok()).collect();
|
||||
|
||||
let distances: Vec<i64> = line2.split(' ').filter_map(|s| s.parse().ok()).collect();
|
||||
|
||||
println!("Part 1: {}", calc(times, distances));
|
||||
|
||||
println!("Part 2: {}", calc(vec![63789468], vec![411127420471035]));
|
||||
}
|
||||
|
||||
fn calc(times: Vec<i64>, distances: Vec<i64>) -> i32 {
|
||||
let mut out = 1;
|
||||
|
||||
for (&t, &d) in times.iter().zip(distances.iter()) {
|
||||
let mut count = 0;
|
||||
|
||||
for speed in 1..t {
|
||||
let total = (t - speed) * speed;
|
||||
|
||||
if total > d {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
out *= count;
|
||||
}
|
||||
|
||||
out
|
||||
}
|
1
2023/day7/day7/.gitignore
vendored
Normal file
1
2023/day7/day7/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
7
2023/day7/day7/Cargo.lock
generated
Normal file
7
2023/day7/day7/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day7"
|
||||
version = "0.1.0"
|
8
2023/day7/day7/Cargo.toml
Normal file
8
2023/day7/day7/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day7"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1001
2023/day7/day7/input.txt
Normal file
1001
2023/day7/day7/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
148
2023/day7/day7/src/main.rs
Normal file
148
2023/day7/day7/src/main.rs
Normal file
@@ -0,0 +1,148 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
fn get_score(item: (&str, i32)) -> (i32, Vec<i32>) {
|
||||
let (hand, bid) = item;
|
||||
let mut counts: HashMap<char, i32> = HashMap::new();
|
||||
for c in hand.chars() {
|
||||
*counts.entry(c).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let mut values: Vec<i32> = Vec::new();
|
||||
for c in hand.chars() {
|
||||
values.push(match c {
|
||||
'2' => 0,
|
||||
'3' => 1,
|
||||
'4' => 2,
|
||||
'5' => 3,
|
||||
'6' => 4,
|
||||
'7' => 5,
|
||||
'8' => 6,
|
||||
'9' => 7,
|
||||
'T' => 8,
|
||||
'J' => 9,
|
||||
'Q' => 10,
|
||||
'K' => 11,
|
||||
'A' => 12,
|
||||
_ => 0,
|
||||
});
|
||||
}
|
||||
|
||||
let mut counts_vec: Vec<i32> = counts.values().cloned().collect();
|
||||
counts_vec.sort();
|
||||
|
||||
let type_value = if counts_vec.contains(&5) {
|
||||
10
|
||||
} else if counts_vec.contains(&4) {
|
||||
9
|
||||
} else if counts_vec.contains(&3) {
|
||||
if counts_vec.contains(&2) {
|
||||
8
|
||||
} else {
|
||||
7
|
||||
}
|
||||
} else if counts_vec.contains(&2) {
|
||||
if counts_vec.iter().filter(|&&x| x == 2).count() == 2 {
|
||||
6
|
||||
} else {
|
||||
5
|
||||
}
|
||||
} else {
|
||||
4
|
||||
};
|
||||
(type_value, values)
|
||||
}
|
||||
|
||||
fn get_score_part2(item: (&str, i32)) -> (i32, Vec<i32>) {
|
||||
let (hand, bid) = item;
|
||||
|
||||
let counts: Vec<i32> = {
|
||||
let mut count_map: HashMap<char, i32> = HashMap::new();
|
||||
for c in hand.replace('J', "").chars() {
|
||||
*count_map.entry(c).or_insert(0) += 1;
|
||||
}
|
||||
let mut counts: Vec<i32> = count_map.values().cloned().collect();
|
||||
counts.sort();
|
||||
if let Some(last_count) = counts.last_mut() {
|
||||
*last_count += hand.matches('J').count() as i32;
|
||||
} else {
|
||||
counts.push(5);
|
||||
}
|
||||
counts
|
||||
};
|
||||
|
||||
let values: Vec<i32> = hand
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
'J' => 0,
|
||||
'2' => 1,
|
||||
'3' => 2,
|
||||
'4' => 3,
|
||||
'5' => 4,
|
||||
'6' => 5,
|
||||
'7' => 6,
|
||||
'8' => 7,
|
||||
'9' => 8,
|
||||
'T' => 9,
|
||||
'Q' => 10,
|
||||
'K' => 11,
|
||||
'A' => 12,
|
||||
_ => 0,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut counts_vec: Vec<i32> = counts.clone();
|
||||
counts_vec.sort();
|
||||
|
||||
let type_value = if counts.contains(&5) {
|
||||
10
|
||||
} else if counts.contains(&4) {
|
||||
9
|
||||
} else if counts.contains(&3) {
|
||||
if counts.contains(&2) {
|
||||
8
|
||||
} else {
|
||||
7
|
||||
}
|
||||
} else if counts.contains(&2) {
|
||||
if counts.iter().filter(|&&x| x == 2).count() == 2 {
|
||||
6
|
||||
} else {
|
||||
5
|
||||
}
|
||||
} else {
|
||||
4
|
||||
};
|
||||
|
||||
(type_value, values)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file = File::open("input.txt").expect("Failed to open file");
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut hands: Vec<(String, i32)> = Vec::new();
|
||||
let lines = reader.lines().map(|l| l.unwrap());
|
||||
|
||||
for line in lines {
|
||||
let mut iter = line.split_whitespace();
|
||||
if let (Some(hand), Some(bid)) = (iter.next(), iter.next()) {
|
||||
hands.push((hand.to_string(), bid.parse().unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
// Part 1
|
||||
//hands.sort_by_key(|(hand, bid)| get_score((&hand.clone(), *bid)));
|
||||
|
||||
// Part 2
|
||||
hands.sort_by_key(|(hand, bid)| get_score_part2((&hand.clone(), *bid)));
|
||||
|
||||
let mut total = 0;
|
||||
for (i, (_, bid)) in hands.iter().enumerate() {
|
||||
total += bid * (i as i32 + 1);
|
||||
}
|
||||
|
||||
println!("Total: {}", total);
|
||||
}
|
||||
|
Reference in New Issue
Block a user