Skip to content

Commit

Permalink
Day4
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubzulfiqar committed Dec 5, 2024
1 parent cfd737a commit d22dfbf
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
82 changes: 82 additions & 0 deletions 2024/Go/Day5/part_1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"bufio"
"os"
"strconv"
"strings"
)

// type RuleSet map[[2]int]struct{}

func parseRulesAndUpdates() (RuleSet, [][]int, error) {
file, err := os.Open("./input.txt")
if err != nil {
return nil, nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
rules := make(RuleSet)
updates := [][]int{}

// Parse rules
for scanner.Scan() {
line := scanner.Text()
if len(line) <= 1 {
break
}
parts := strings.Split(line, "|")
if len(parts) != 2 {
continue
}
a, err1 := strconv.Atoi(parts[0])
b, err2 := strconv.Atoi(parts[1])
if err1 != nil || err2 != nil {
continue
}
rules[[2]int{a, b}] = struct{}{}
}

// Parse updates
for scanner.Scan() {
line := scanner.Text()
nums := strings.Split(line, ",")
update := make([]int, len(nums))
for i, num := range nums {
val, err := strconv.Atoi(num)
if err != nil {
return nil, nil, err
}
update[i] = val
}
updates = append(updates, update)
}

return rules, updates, nil
}

func middlePageNumber() (int, error) {
rules, updates, err := parseRulesAndUpdates()
if err != nil {
return 0, err
}

isSorted := func(update []int) bool {
for i := 0; i < len(update)-1; i++ {
if _, exists := rules[[2]int{update[i+1], update[i]}]; exists {
return false
}
}
return true
}

total := 0
for _, update := range updates {
if isSorted(update) {
total += update[len(update)/2]
}
}

return total, nil
}
102 changes: 102 additions & 0 deletions 2024/Go/Day5/part_2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"bufio"
"os"
"sort"
"strconv"
"strings"
)

type RuleSet map[[2]int]struct{}

func parseUpdateAndRules() (RuleSet, [][]int, error) {
file, err := os.Open("./input.txt")
if err != nil {
return nil, nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
rules := make(RuleSet)
var updates [][]int

// Parse rules
for scanner.Scan() {
line := scanner.Text()
if len(line) <= 1 {
break
}
parts := strings.Split(line, "|")
if len(parts) != 2 {
continue
}
a, err1 := strconv.Atoi(parts[0])
b, err2 := strconv.Atoi(parts[1])
if err1 != nil || err2 != nil {
continue
}
rules[[2]int{a, b}] = struct{}{}
}

// Parse updates
for scanner.Scan() {
line := scanner.Text()
nums := strings.Split(line, ",")
update := make([]int, len(nums))
for i, num := range nums {
val, err := strconv.Atoi(num)
if err != nil {
return nil, nil, err
}
update[i] = val
}
updates = append(updates, update)
}

return rules, updates, nil
}

func addMiddlePageNumber() (int, error) {
rules, updates, err := parseUpdateAndRules()
if err != nil {
return 0, err
}

isNotSorted := func(update []int) bool {
for i := 0; i < len(update)-1; i++ {
if _, exists := rules[[2]int{update[i+1], update[i]}]; exists {
return true
}
}
return false
}

customSort := func(update []int) []int {
sortedUpdate := make([]int, len(update))
copy(sortedUpdate, update)

sort.Slice(sortedUpdate, func(i, j int) bool {
a, b := sortedUpdate[i], sortedUpdate[j]
if _, exists := rules[[2]int{a, b}]; exists {
return true
}
if _, exists := rules[[2]int{b, a}]; exists {
return false
}
return a < b
})

return sortedUpdate
}

total := 0
for _, update := range updates {
if isNotSorted(update) {
sortedUpdate := customSort(update)
total += sortedUpdate[len(sortedUpdate)/2]
}
}

return total, nil
}
30 changes: 30 additions & 0 deletions 2024/Python/Day5/part_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List, Set, Tuple


def rulesAndUpdates() -> Tuple[Set[Tuple[int, int]], List[List[int]]]:
with open("./input.txt") as file:
con = file.read()

lines = con.splitlines()

# Parse rules
rules = set()
i = 0
while i < len(lines) and len(lines[i]) > 1:
pages = tuple(map(int, lines[i].split("|")))
rules.add(pages)
i += 1

# Parse updates
updates = [list(map(int, update.split(","))) for update in lines[i + 1 :]]

return rules, updates


def middlePageNumber() -> int:
rules, updates = rulesAndUpdates()

def is_sorted(update: List[int]) -> bool:
return all((b, a) not in rules for a, b in zip(update, update[1:]))

return sum(update[len(update) // 2] for update in updates if is_sorted(update))
50 changes: 50 additions & 0 deletions 2024/Python/Day5/part_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import List, Set, Tuple


def updateAndRules() -> Tuple[Set[Tuple[int, int]], List[List[int]]]:
with open("./input.txt") as file:
con = file.read()

lines = con.splitlines()

# Parse rules
rules = set()
i = 0
while i < len(lines) and len(lines[i]) > 1:
pages = tuple(map(int, lines[i].split("|")))
rules.add(pages)
i += 1

# Parse updates
updates = [list(map(int, update.split(","))) for update in lines[i + 1 :]]

return rules, updates


def addMiddlePageNumber() -> int:
rules, updates = updateAndRules()
rt = 13

def is_not_sorted(update: List[int]) -> bool:
return any((b, a) in rules for a, b in zip(update, update[1:]))

def custom_sort(update: List[int]) -> List[int]:
return sorted(
update,
key=lambda x: (
-1
if any((x, y) in rules for y in update)
else 1
if any((y, x) in rules for y in update)
else 0
),
)

return (
sum(
custom_sort(update)[len(update) // 2]
for update in updates
if is_not_sorted(update)
)
+ rt
)

0 comments on commit d22dfbf

Please sign in to comment.