advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

commit 108438460de286f5e39fbc26d44b49d3ffb83492
parent 258db97c1f7f77c79882a6c832478d757cbd5ca7
Author: bsandro <email@bsandro.tech>
Date:   Sun,  4 Dec 2022 06:21:22 +0000

Day 4 fixed typo, simplified solution

Diffstat:
Mday04/main.go | 29++++++++++-------------------
1 file changed, 10 insertions(+), 19 deletions(-)

diff --git a/day04/main.go b/day04/main.go @@ -9,7 +9,7 @@ import ( func main() { if len(os.Args) > 1 { - day03(os.Args[1]) + day04(os.Args[1]) } else if len(os.Args) == 1 { fmt.Printf("usage: %s inputfile.txt\n", os.Args[0]) } else { @@ -17,7 +17,7 @@ func main() { } } -func day03(input_file string) { +func day04(input_file string) { fmt.Printf("day 04 input filename: %s\n", input_file) input, err := os.Open(input_file) if err != nil { @@ -28,11 +28,16 @@ func day03(input_file string) { overlaps1 := 0 overlaps2 := 0 for scanner.Scan() { - o1, o2 := checkOverlap(scanner.Text()) - if o1 { + in := scanner.Text() + var p1, p2 [2]int + parsed, err := fmt.Sscanf(in, "%d-%d,%d-%d", &p1[0], &p1[1], &p2[0], &p2[1]) + if err != nil || parsed != 4 { + log.Fatal("Invalid input string", in, err) + } + if (p1[0] >= p2[0] && p1[1] <= p2[1]) || (p2[0] >= p1[0] && p2[1] <= p1[1]) { overlaps1++ } - if o2 { + if (p1[0] >= p2[0] && p1[0] <= p2[1]) || (p2[0] >= p1[0] && p2[0] <= p1[1]) { overlaps2++ } } @@ -43,17 +48,3 @@ func day03(input_file string) { fmt.Println("Part 1:", overlaps1) fmt.Println("Part 2:", overlaps2) } - -func checkOverlap(in string) (bool, bool) { - var p1, p2 [2]int - parsed, err := fmt.Sscanf(in, "%d-%d,%d-%d", &p1[0], &p1[1], &p2[0], &p2[1]) - if err != nil { - log.Fatal(err) - } - if parsed != 4 { - log.Fatal("Invalid input string", in) - } - overlap1 := (p1[0] >= p2[0] && p1[1] <= p2[1]) || (p2[0] >= p1[0] && p2[1] <= p1[1]) - overlap2 := (p1[0] >= p2[0] && p1[0] <= p2[1]) || (p2[0] >= p1[0] && p2[0] <= p1[1]) - return overlap1, overlap2 -}