Skip to content

Commit

Permalink
refactor to make shitty linter happy, thank you gianpaolo so much fun
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Chessa committed Dec 4, 2024
1 parent 75bacab commit a37eabb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 74 deletions.
49 changes: 23 additions & 26 deletions solutions/alessio/day01/day01.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)

Expand All @@ -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() {
Expand Down
38 changes: 20 additions & 18 deletions solutions/alessio/day02/day02.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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() {
Expand Down
47 changes: 27 additions & 20 deletions solutions/alessio/day03/day03.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -60,6 +67,6 @@ func main() {

lines := strings.Split(strings.Trim(string(data), "\r\n"), "\r\n")

// part1(lines)
part1(lines)
part2(lines)
}
21 changes: 11 additions & 10 deletions solutions/alessio/day04/day04.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -43,21 +43,21 @@ 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)
}
}
}

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
}

Expand All @@ -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")
Expand Down

0 comments on commit a37eabb

Please sign in to comment.