Solution to Day 1

This commit is contained in:
2025-12-05 06:51:01 +01:00
parent ab4d3274a6
commit 717ea6bf12
7 changed files with 8933 additions and 4 deletions

View File

@@ -0,0 +1,74 @@
#!/usr/bin/env -S julia --project
module AdventOfCodeDayXX
function parse_part1(filename::String)
tokens = split(read(filename, String))
rotations = Vector{Tuple{Char,Int}}()
for t in tokens
if isempty(t)
continue
end
push!(rotations, (t[1], parse(Int, t[2:end])))
end
return rotations
end
function part1(filename::String)
rotations = parse_part1(filename)
starting = 50
counts = 0
for (dir, dist) in rotations
if dir == 'L'
starting = mod(starting - dist, 100)
else
starting = mod(starting + dist, 100)
end
if starting == 0
counts += 1
end
end
return counts
end
function part2(filename::String)
rotations = parse_part1(filename)
current = 50
counts = 0
for (dir, dist) in rotations
if dir == 'L'
gap = mod(100 - current, 100)
wraps = (gap + dist) ÷ 100
counts += abs(wraps)
current = mod(current - dist, 100)
else
wraps = (current + dist) ÷ 100
counts += abs(wraps)
current = mod(current + dist, 100)
end
end
return counts
end
function main()
demo_p1 = part1("demo.txt")
input_p1 = part1("input.txt")
demo_p2 = part2("demo.txt")
input_p2 = part2("input.txt")
println("Advent of Code — Day 01")
println("="^23)
println("Demo Results:")
println(" Part 1: ", demo_p1)
println(" Part 2: ", demo_p2)
println()
println("Input Results:")
println(" Part 1: ", input_p1)
println(" Part 2: ", input_p2)
println("="^23)
end
function __init__()
if abspath(PROGRAM_FILE) == @__FILE__
main()
end
end
end

View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

File diff suppressed because it is too large Load Diff

View File

@@ -18,10 +18,21 @@ module AdventOfCodeDayXX
end
function main()
@show part1("demo.txt")
@show part1("input.txt")
@show part2("demo.txt")
@show part2("input.txt")
demo_p1 = part1("demo.txt")
input_p1 = part1("input.txt")
demo_p2 = part2("demo.txt")
input_p2 = part2("input.txt")
println("Advent of Code — Day XX")
println("="^23)
println("Demo Results:")
println(" Part 1: ", demo_p1)
println(" Part 2: ", demo_p2)
println()
println("Input Results:")
println(" Part 1: ", input_p1)
println(" Part 2: ", input_p2)
println("="^23)
end
function __init__()

View File

@@ -0,0 +1,40 @@
with open("input.txt") as f:
rotations = [lines for lines in f.read().strip().split()]
starting = 50
counts = 0
for rotation in rotations:
current = starting
direction, distance = rotation[0], int(rotation[1:])
if direction == "L":
quotient, remainder = divmod(current - distance, 100)
starting = remainder
else: # "R":
quotient, remainder = divmod(current + int(distance), 100)
starting = remainder
if starting == 0:
counts += 1
print(f"There has been {counts} times")
current = 50
counts = 0
for rotation in rotations:
direction, distance = rotation[0], int(rotation[1:])
if direction == "L":
# how far you'd have to move forward to reach 0 from current
# Example: if current = 30, gap = 70.
gap = (100 - current) % 100
# If you move left by distance, that is the same as first moving
# forward by gap to land on 0, and then continuing forward by
# distance more (in that forward-frame of reference). So the total
# equivalent forward travel to count wraps is gap + distance
quotient, remainder = divmod(gap + distance, 100)
current = (current - distance) % 100
counts += abs(quotient)
else: # "R"
quotient, remainder = divmod(current + distance, 100)
current = remainder
counts += abs(quotient)
print(f"There has been {counts} times, corrected")

View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

File diff suppressed because it is too large Load Diff