File tree Expand file tree Collapse file tree 12 files changed +159
-0
lines changed
Expand file tree Collapse file tree 12 files changed +159
-0
lines changed Original file line number Diff line number Diff line change 1+ module main
2+
3+ import math
4+ import os
5+
6+ fn main () {
7+ lines := os.get_lines ()
8+
9+ mut col1 := []i64 {}
10+ mut col2 := []i64 {}
11+
12+ for l in lines {
13+ a , b := l.split_once (' ' ) or { panic ('Could not split a line ${l} ' ) }
14+ col1 << a.parse_int (10 , 32 )!
15+ col2 << b.parse_int (10 , 32 )!
16+ }
17+
18+ col1 .sort ()
19+ col2 .sort ()
20+
21+ mut sum := i64 (0 )
22+ for i := 0 ; i < col1 .len; i++ {
23+ sum + = math.abs (col2 [i] - col1 [i])
24+ }
25+
26+ println (sum)
27+ }
Original file line number Diff line number Diff line change 1+ module main
2+
3+ import os
4+ import arrays
5+
6+ fn main () {
7+ lines := os.get_lines ()
8+ mut col1 := []i64 {}
9+ mut col2 := []i64 {}
10+
11+ for l in lines {
12+ a , b := l.split_once (' ' ) or { panic ('Could not split a line ${l} ' ) }
13+ col1 << a.parse_int (10 , 32 )!
14+ col2 << b.parse_int (10 , 32 )!
15+ }
16+
17+ counts := arrays.map_of_counts[i64 ](col2 )
18+
19+ mut sum := i64 (0 )
20+ for n in col1 {
21+ sum + = n * counts[n]
22+ }
23+
24+ println (sum)
25+ }
Original file line number Diff line number Diff line change 1+ module main
2+
3+ import os
4+ import arrays
5+
6+ fn main () {
7+ input := os.get_lines ()
8+
9+ mut count := 0
10+ for line in input {
11+ levels := line.split (' ' ).map (it .int ())
12+ diff := arrays.window (levels, size: 2 ).map (it [1 ] - it [0 ])
13+ within_bounds := diff.all (it < = 3 && it > = - 3 )
14+ positive := diff.all (it > 0 )
15+ negative := diff.all (it < 0 )
16+ if within_bounds && (positive || negative) {
17+ count++
18+ }
19+ }
20+
21+ println (count)
22+ }
Original file line number Diff line number Diff line change 1+ module main
2+
3+ import os
4+ import arrays
5+
6+ fn main () {
7+ input := os.get_lines ()
8+
9+ mut count := 0
10+ for line in input {
11+ levels := line.split (' ' ).map (it .int ())
12+ for i := 0 ; i < levels.len; i++ {
13+ mut lvl := levels.clone ()
14+ lvl.delete (i)
15+ diff := arrays.window (lvl, size: 2 ).map (it [1 ] - it [0 ])
16+ within_bounds := diff.all (it < = 3 && it > = - 3 )
17+ positive := diff.all (it > 0 )
18+ negative := diff.all (it < 0 )
19+ if within_bounds && (positive || negative) {
20+ count++
21+ break
22+ }
23+ }
24+ }
25+
26+ println (count)
27+ }
Original file line number Diff line number Diff line change 1+ module main
2+
3+ import os
4+ import regex
5+
6+ fn main () {
7+ input := os.get_raw_lines_joined ()
8+
9+ mut re := regex.regex_opt ('mul\\ (\\ d+,\\ d+\\ )' )!
10+ list := re.find_all_str (input)
11+
12+ mut sum := 0
13+ for l in list {
14+ a , b := l.trim ('mul()' ).split_once (',' )?
15+ sum + = a.int () * b.int ()
16+ }
17+
18+ println (sum)
19+ }
Original file line number Diff line number Diff line change 1+ module main
2+
3+ import os
4+ import regex
5+
6+ fn main () {
7+ input := os.get_raw_lines_joined ()
8+
9+ mut re := regex.regex_opt (r "(do\(\))|(don't\(\))|(mul\(\d+,\d+\))" )!
10+ list := re.find_all_str (input)
11+
12+ mut is_enabled := true
13+ mut sum := 0
14+ for l in list {
15+ match l {
16+ 'do()' {
17+ is_enabled = true
18+ continue
19+ }
20+ "don't()" {
21+ is_enabled = false
22+ continue
23+ }
24+ else {}
25+ }
26+ if is_enabled {
27+ a , b := l.trim ('mul()' ).split_once (',' )?
28+ sum + = a.int () * b.int ()
29+ }
30+ }
31+
32+ println (sum)
33+ }
Original file line number Diff line number Diff line change 1+ 11
Original file line number Diff line number Diff line change 1+ 31
Original file line number Diff line number Diff line change 1+ 2
Original file line number Diff line number Diff line change 1+ 4
You can’t perform that action at this time.
0 commit comments