aoc2023

advent of code 2023
Log | Files | Refs | Feed | README

Commit: f7381f4c7d14c3cc7b55a9a4495cadcf74e4d312
Parent: 7044de2c535ad1e651dc934bfbb835e203202059
Author: Randy Palamar
Date:   Mon, 11 Dec 2023 07:01:21 -0700

day 6

Using Haskell's list comprehensions for this almost feels like
cheating; it made this task completely trivial.

There is probably a faster way of doing this, particularly for
part 2, but I like the simplicity of this solution.

(Solutions are out of order and I'm falling behind but oh well).

Diffstat:
Aday6/day6.hs | 44++++++++++++++++++++++++++++++++++++++++++++
Aday6/test | 2++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/day6/day6.hs b/day6/day6.hs @@ -0,0 +1,44 @@ +-- AOC 2023: Day 6 - Wait For It +-- +-- Given a set of (time, distance) pairs, an acceleration of 1 +-- dist/time^2 (units don't matter), and the rule that while +-- accelerating you are not moving find the number of different ways +-- you can reach the distance (i.e. you accelerate for a some portion +-- of the time and then travel at the achieved velocity for the +-- remainder). +-- +-- Example input: +-- Time: 7 15 30 +-- Distance: 9 40 200 +-- Test Output: 288 +-- +-- Task 2: There is actually only one time and distance in the +-- example input with some random space in between. +-- Test Output: 71503 +-- +{-# LANGUAGE OverloadedStrings #-} + +import qualified Data.Text as T +import qualified Data.Text.IO as TI +import Text.Printf (printf) + +texttoint :: T.Text -> Int +texttoint t = read (T.unpack t) :: Int + +numlist :: T.Text -> [Int] +numlist = map texttoint . filter (/= "") . T.splitOn " " . T.strip + +winningwaits :: (Int, Int) -> Int +winningwaits (t, d) = length $ [x | x <- [1 .. (t - 1)], x * (t - x) > d] + +main = do + contents <- TI.getContents + let (t, d) = T.breakOn "\n" contents + let times = T.dropWhile (/= ' ') t + let dists = T.dropWhile (/= ' ') d + let pairs = zip (numlist times) (numlist dists) + let pairs2 = + ( texttoint $ T.filter (/= ' ') times + , texttoint $ T.filter (/= ' ') dists) + printf "Task 1: %8d\n" (foldl (*) 1 $ map winningwaits pairs) + printf "Task 2: %8d\n" (winningwaits pairs2) diff --git a/day6/test b/day6/test @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200