From a37eabba79cb8481ffab7b2dddaa779e2a5b30aa Mon Sep 17 00:00:00 2001 From: Alessio Chessa Date: Wed, 4 Dec 2024 13:36:16 +0100 Subject: [PATCH] refactor to make shitty linter happy, thank you gianpaolo so much fun --- solutions/alessio/day01/day01.go | 49 +++++++++++++++----------------- solutions/alessio/day02/day02.go | 38 +++++++++++++------------ solutions/alessio/day03/day03.go | 47 +++++++++++++++++------------- solutions/alessio/day04/day04.go | 21 +++++++------- 4 files changed, 81 insertions(+), 74 deletions(-) diff --git a/solutions/alessio/day01/day01.go b/solutions/alessio/day01/day01.go index 97f22d0..3dbf49a 100644 --- a/solutions/alessio/day01/day01.go +++ b/solutions/alessio/day01/day01.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "sort" - "strconv" "strings" ) @@ -22,50 +21,48 @@ func abs(x int) int { } func part1(lines []string) { - var first_list []int - var second_list []int - sum_diff := 0 + var left []int + var right []int + sumDiff := 0 for _, l := range lines { - vals := strings.Split(l, " ") - first, err := strconv.Atoi(vals[0]) + var first, second int + _, err := fmt.Sscanf(l, "%d %d", &first, &second) check(err) - second, err := strconv.Atoi(vals[1]) - check(err) - first_list = append(first_list, first) - second_list = append(second_list, second) + left = append(left, first) + right = append(right, second) } - sort.Ints(first_list) - sort.Ints(second_list) + sort.Ints(left) + sort.Ints(right) for i := range len(lines) { - sum_diff += abs(second_list[i] - first_list[i]) + sumDiff += abs(right[i] - left[i]) } - fmt.Println(sum_diff) + _, err := fmt.Println(sumDiff) + check(err) } func part2(lines []string) { - first_cnt := make(map[int]int) - second_cnt := make(map[int]int) + leftCnt := make(map[int]int) + rightCnt := make(map[int]int) for _, l := range lines { - vals := strings.Split(l, " ") - first, err := strconv.Atoi(vals[0]) + var first, second int + _, err := fmt.Sscanf(l, "%d %d", &first, &second) check(err) - second, err := strconv.Atoi(vals[1]) - check(err) - first_cnt[first] += 1 - second_cnt[second] += 1 + leftCnt[first] += 1 + rightCnt[second] += 1 } - sim_score := 0 + simScore := 0 - for k, v := range first_cnt { - sim_score += k * v * second_cnt[k] + for k, v := range leftCnt { + simScore += k * v * rightCnt[k] } - fmt.Println(sim_score) + _, err := fmt.Println(simScore) + check(err) } func main() { diff --git a/solutions/alessio/day02/day02.go b/solutions/alessio/day02/day02.go index 51fe411..f5bf436 100644 --- a/solutions/alessio/day02/day02.go +++ b/solutions/alessio/day02/day02.go @@ -14,13 +14,13 @@ func check(e error) { } } -func check_if_safe(levels []int) bool { - is_incr := levels[1]-levels[0] > 0 +func checkIfSafe(levels []int) bool { + isIncr := levels[1]-levels[0] > 0 for i := range len(levels) - 1 { diff := levels[i+1] - levels[i] - if is_incr && (diff < 1 || diff > 3) { + if isIncr && (diff < 1 || diff > 3) { return false - } else if !is_incr && (diff > -1 || diff < -3) { + } else if !isIncr && (diff > -1 || diff < -3) { return false } } @@ -31,50 +31,52 @@ func part1(lines []string) { safes := 0 for _, l := range lines { - levels_str := strings.Split(l, " ") + levelsStr := strings.Split(l, " ") var levels []int - for i := range len(levels_str) { - lvl, err := strconv.Atoi(levels_str[i]) + for i := range len(levelsStr) { + lvl, err := strconv.Atoi(levelsStr[i]) check(err) levels = append(levels, lvl) } - if check_if_safe(levels) { + if checkIfSafe(levels) { safes += 1 } } - fmt.Println(safes) + _, err := fmt.Println(safes) + check(err) } func part2(lines []string) { safes := 0 for _, l := range lines { - levels_str := strings.Split(l, " ") + levelsStr := strings.Split(l, " ") var levels []int - for i := range len(levels_str) { - lvl, err := strconv.Atoi(levels_str[i]) + for i := range len(levelsStr) { + lvl, err := strconv.Atoi(levelsStr[i]) check(err) levels = append(levels, lvl) } - is_safe := check_if_safe(levels) + isSafe := checkIfSafe(levels) i := 0 - for !is_safe && i < len(levels) { - levels_slice := slices.Concat(levels[:i], levels[i+1:]) // basically splice on i-th element - is_safe = check_if_safe(levels_slice) + for !isSafe && i < len(levels) { + levelsSlice := slices.Concat(levels[:i], levels[i+1:]) // basically splice on i-th element + isSafe = checkIfSafe(levelsSlice) i++ } - if is_safe { + if isSafe { safes += 1 } } - fmt.Println(safes) + _, err := fmt.Println(safes) + check(err) } func main() { diff --git a/solutions/alessio/day03/day03.go b/solutions/alessio/day03/day03.go index 301021a..9c0d927 100644 --- a/solutions/alessio/day03/day03.go +++ b/solutions/alessio/day03/day03.go @@ -14,44 +14,51 @@ func check(e error) { } func part1(lines []string) { - r, _ := regexp.Compile(`mul\(\d{1,3},\d{1,3}\)`) + r, err := regexp.Compile(`mul\(\d{1,3},\d{1,3}\)`) + check(err) sum := 0 for _, l := range lines { for _, s := range r.FindAllString(l, -1) { var a int var b int - fmt.Sscanf(s, "mul(%d,%d)", &a, &b) + _, err := fmt.Sscanf(s, "mul(%d,%d)", &a, &b) + check(err) sum += a * b } } - fmt.Println(sum) + _, err = fmt.Println(sum) + check(err) } func part2(lines []string) { mul_regexp := `(mul\(\d{1,3},\d{1,3}\))|(do\(\))|(don\'t\(\))` - // do_regexp := `do` - // dont_regexp := `don't` - r, _ := regexp.Compile(mul_regexp) + r, err := regexp.Compile(mul_regexp) + check(err) sum := 0 - is_enabled := true + isEnabled := true for _, l := range lines { commands := r.FindAllString(l, -1) - fmt.Println(commands) + _, err := fmt.Println(commands) + check(err) for _, cmd := range commands { - if cmd == "do()" { - is_enabled = true - } else if cmd == "don't()" { - is_enabled = false - } else if is_enabled { - var a int - var b int - fmt.Sscanf(cmd, "mul(%d,%d)", &a, &b) - fmt.Println(a, b) - sum += a * b + switch cmd { + case "do()": + isEnabled = true + case "don't()": + isEnabled = false + default: + if isEnabled { + var a int + var b int + _, err := fmt.Sscanf(cmd, "mul(%d,%d)", &a, &b) + check(err) + sum += a * b + } } } } - fmt.Println(sum) + _, err = fmt.Println(sum) + check(err) } func main() { @@ -60,6 +67,6 @@ func main() { lines := strings.Split(strings.Trim(string(data), "\r\n"), "\r\n") - // part1(lines) + part1(lines) part2(lines) } diff --git a/solutions/alessio/day04/day04.go b/solutions/alessio/day04/day04.go index 555c5de..d68ffac 100644 --- a/solutions/alessio/day04/day04.go +++ b/solutions/alessio/day04/day04.go @@ -12,7 +12,7 @@ func check(e error) { } } -func search_word(r int, c int, mat []string, rows int, cols int) int { +func searchWord(r int, c int, mat []string, rows int, cols int) int { dirs := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}} letters := "MAS" @@ -43,7 +43,7 @@ func part1(lines []string) { for r := range rows { for c := range cols { if lines[r][c] == 'X' { - cnt += search_word(r, c, lines, rows, cols) + cnt += searchWord(r, c, lines, rows, cols) } } } @@ -51,13 +51,13 @@ func part1(lines []string) { fmt.Println(cnt) } -func search_x_mas(r int, c int, mat []string) int { - diag_1_1 := mat[r-1][c-1] == 'M' && mat[r+1][c+1] == 'S' - diag_1_2 := mat[r-1][c-1] == 'S' && mat[r+1][c+1] == 'M' - diag_2_1 := mat[r-1][c+1] == 'M' && mat[r+1][c-1] == 'S' - diag_2_2 := mat[r-1][c+1] == 'S' && mat[r+1][c-1] == 'M' +func searchXMas(r int, c int, mat []string) int { + diag11 := mat[r-1][c-1] == 'M' && mat[r+1][c+1] == 'S' + diag12 := mat[r-1][c-1] == 'S' && mat[r+1][c+1] == 'M' + diag21 := mat[r-1][c+1] == 'M' && mat[r+1][c-1] == 'S' + diag22 := mat[r-1][c+1] == 'S' && mat[r+1][c-1] == 'M' - if (diag_1_1 || diag_1_2) && (diag_2_1 || diag_2_2) { + if (diag11 || diag12) && (diag21 || diag22) { return 1 } @@ -72,12 +72,13 @@ func part2(lines []string) { for r := 1; r < rows-1; r++ { for c := 1; c < cols-1; c++ { if lines[r][c] == 'A' { - cnt += search_x_mas(r, c, lines) + cnt += searchXMas(r, c, lines) } } } - fmt.Println(cnt) + _, err := fmt.Println(cnt) + check(err) } func main() { data, err := os.ReadFile("./input04.txt")