-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module main | ||
|
||
import math | ||
import os | ||
|
||
fn main() { | ||
lines := os.get_lines() | ||
|
||
mut col1 := []i64{} | ||
mut col2 := []i64{} | ||
|
||
for l in lines { | ||
a, b := l.split_once(' ') or { panic('Could not split a line ${l}') } | ||
col1 << a.parse_int(10, 32)! | ||
col2 << b.parse_int(10, 32)! | ||
} | ||
|
||
col1.sort() | ||
col2.sort() | ||
|
||
mut sum := i64(0) | ||
for i := 0; i < col1.len; i++ { | ||
sum += math.abs(col2[i] - col1[i]) | ||
} | ||
|
||
println(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module main | ||
|
||
import os | ||
import arrays | ||
|
||
fn main() { | ||
lines := os.get_lines() | ||
mut col1 := []i64{} | ||
mut col2 := []i64{} | ||
|
||
for l in lines { | ||
a, b := l.split_once(' ') or { panic('Could not split a line ${l}') } | ||
col1 << a.parse_int(10, 32)! | ||
col2 << b.parse_int(10, 32)! | ||
} | ||
|
||
counts := arrays.map_of_counts[i64](col2) | ||
|
||
mut sum := i64(0) | ||
for n in col1 { | ||
sum += n * counts[n] | ||
} | ||
|
||
println(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module main | ||
|
||
import os | ||
import arrays | ||
|
||
fn main() { | ||
input := os.get_lines() | ||
|
||
mut count := 0 | ||
for line in input { | ||
levels := line.split(' ').map(it.int()) | ||
diff := arrays.window(levels, size: 2).map(it[1] - it[0]) | ||
within_bounds := diff.all(it <= 3 && it >= -3) | ||
positive := diff.all(it > 0) | ||
negative := diff.all(it < 0) | ||
if within_bounds && (positive || negative) { | ||
count++ | ||
} | ||
} | ||
|
||
println(count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module main | ||
|
||
import os | ||
import arrays | ||
|
||
fn main() { | ||
input := os.get_lines() | ||
|
||
mut count := 0 | ||
for line in input { | ||
levels := line.split(' ').map(it.int()) | ||
for i := 0; i < levels.len; i++ { | ||
mut lvl := levels.clone() | ||
lvl.delete(i) | ||
diff := arrays.window(lvl, size: 2).map(it[1] - it[0]) | ||
within_bounds := diff.all(it <= 3 && it >= -3) | ||
positive := diff.all(it > 0) | ||
negative := diff.all(it < 0) | ||
if within_bounds && (positive || negative) { | ||
count++ | ||
break | ||
} | ||
} | ||
} | ||
|
||
println(count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module main | ||
|
||
import os | ||
import regex | ||
|
||
fn main() { | ||
input := os.get_raw_lines_joined() | ||
|
||
mut re := regex.regex_opt('mul\\(\\d+,\\d+\\)')! | ||
list := re.find_all_str(input) | ||
|
||
mut sum := 0 | ||
for l in list { | ||
a, b := l.trim('mul()').split_once(',')? | ||
sum += a.int() * b.int() | ||
} | ||
|
||
println(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module main | ||
|
||
import os | ||
import regex | ||
|
||
fn main() { | ||
input := os.get_raw_lines_joined() | ||
|
||
mut re := regex.regex_opt(r"(do\(\))|(don't\(\))|(mul\(\d+,\d+\))")! | ||
list := re.find_all_str(input) | ||
|
||
mut is_enabled := true | ||
mut sum := 0 | ||
for l in list { | ||
match l { | ||
'do()' { | ||
is_enabled = true | ||
continue | ||
} | ||
"don't()" { | ||
is_enabled = false | ||
continue | ||
} | ||
else {} | ||
} | ||
if is_enabled { | ||
a, b := l.trim('mul()').split_once(',')? | ||
sum += a.int() * b.int() | ||
} | ||
} | ||
|
||
println(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
31 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
161 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
48 |