File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ module main
2+
3+ import os
4+ import arrays
5+ import math
6+
7+ fn main () {
8+ input := os.read_file ('seed_map.input' )!
9+ .split ('\n\n ' )
10+ .map (it .split_into_lines ().map (it .trim_space ()))
11+ mut seeds := (input[0 ][0 ].split (': ' )[1 ]).split (' ' ).map (it .i64 ())
12+ for m in 1 .. 8 {
13+ maps := input[m][1 ..].map (it .split (' ' ).map (it .i64 ()))
14+ for i := 0 ; i < seeds.len; i++ {
15+ for line in maps {
16+ if seeds[i] > = line[1 ] && seeds[i] < = line[1 ] + line[2 ] {
17+ seeds[i] = line[0 ] + (seeds[i] - line[1 ])
18+ break
19+ }
20+ }
21+ }
22+ }
23+
24+ println ('Part 1: ${arrays.min(seeds)!} ' )
25+
26+ mut seeds_2 := (input[0 ][0 ].split (': ' )[1 ]).split (' ' ).map (it .u64 ())
27+ mut part_2_min := max_u64
28+ mut maps := [][][]u64 {}
29+ for i in 1 .. 8 {
30+ maps << input[i][1 ..].map (it .split (' ' ).map (it .u64 ()))
31+ }
32+ for i := 0 ; i < seeds_2 .len - 1 ; i + = 2 {
33+ end := seeds_2 [i] + seeds_2 [i + 1 ] - 1
34+ for start := seeds_2 [i]; start < = end; start++ {
35+ mut copy := start
36+ for mapx in maps {
37+ for line in mapx {
38+ if copy > = line[1 ] && copy < line[1 ] + line[2 ] {
39+ copy = line[0 ] + copy - line[1 ]
40+ break
41+ }
42+ }
43+ }
44+ part_2_min = math.min (part_2_ min, copy)
45+ }
46+ }
47+ println ('Part 2: ${part_2_min} ' )
48+ }
Original file line number Diff line number Diff line change 1+ seeds: 79 14 55 13
2+
3+ seed-to-soil map:
4+ 50 98 2
5+ 52 50 48
6+
7+ soil-to-fertilizer map:
8+ 0 15 37
9+ 37 52 2
10+ 39 0 15
11+
12+ fertilizer-to-water map:
13+ 49 53 8
14+ 0 11 42
15+ 42 0 7
16+ 57 7 4
17+
18+ water-to-light map:
19+ 88 18 7
20+ 18 25 70
21+
22+ light-to-temperature map:
23+ 45 77 23
24+ 81 45 19
25+ 68 64 13
26+
27+ temperature-to-humidity map:
28+ 0 69 1
29+ 1 0 69
30+
31+ humidity-to-location map:
32+ 60 56 37
33+ 56 93 4
Original file line number Diff line number Diff line change 1+ Part 1: 35
2+ Part 2: 46
You can’t perform that action at this time.
0 commit comments