Skip to content

Commit

Permalink
cleanup - 2021 - day 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mariamihai committed Dec 11, 2023
1 parent bbf7d9e commit 7c7a7ca
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 3,278 deletions.
46 changes: 10 additions & 36 deletions 2021/day1/day1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@ package day1

import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"github.com/mariamihai/advent-of-code/util"
)

func Problem1() {
file, err := os.Open("./day1/input2.txt")
if err != nil {
log.Fatal(err)
}

defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatal(err)
}
}(file)

scanner := bufio.NewScanner(file)
func Problem1(filename string) int {
file := util.ReadFile(filename)
defer util.CloseFile()(file)

var count = 0
var previousValue = 10000

hasDepthIncreased := func(currentDepth, previousValue int) bool { return currentDepth > previousValue }

scanner := bufio.NewScanner(file)
for scanner.Scan() {
currentDepth := readLine(scanner.Text())
currentDepth := util.StringToInt(scanner.Text())

if hasDepthIncreased(currentDepth, previousValue) {
count++
Expand All @@ -36,22 +25,7 @@ func Problem1() {
previousValue = currentDepth
}

fmt.Printf("Part 1 - Number of larger measurements: %d\n", count)

if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}

func readLine(line string) int {
currentDepth, err := strconv.Atoi(line)
if err != nil {
log.Fatal("Error during conversion")
}

return currentDepth
}
util.Boom(scanner.Err())

func hasDepthIncreased(currentDepth, previousValue int) bool {
return currentDepth > previousValue
return count
}
29 changes: 29 additions & 0 deletions 2021/day1/day1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package day1

import "testing"

func TestProblem1(t *testing.T) {
tests := []struct {
description string
filename string
expected int
}{
{
description: "Should validate PROBLEM 1 against provided EXAMPLE",
filename: "example1.txt",
expected: 7,
},
{
description: "Should validate PROBLEM 1 against provided INPUT",
filename: "input1.txt",
expected: 1298,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
if actual := Problem1(tt.filename); actual != tt.expected {
t.Errorf("Problem1() = %v, want %v", actual, tt.expected)
}
})
}
}
File renamed without changes.
Loading

0 comments on commit 7c7a7ca

Please sign in to comment.